Proposed Rule Name: Queueable Should Attach Finalizer
Proposed Category: Best Practices
Description: We want to warn the author of Apex whenever they write a class implementing the Queueable interface and they fail to attach a Finalizer. We should explain that the lack of a Finalizer provides no handling capabilities should the Queueable action fail.
Code Sample:
// Improper code, does not call `System.attachFinalizer` within the `execute` method.
public class UserUpdater implements Queueable {
private List<User> usersToUpdate;
public UserUpdater(List<User> usersToUpdate) {
this.usersToUpdate = usersToUpdate;
}
public void execute(QueueableContext context) {
update usersToUpdate;
}
}
// Properly attaches a `Finalizer`
public class UserUpdater implements Queueable, Finalizer {
private List<User> usersToUpdate;
public UserUpdater(List<User> usersToUpdate) {
this.usersToUpdate = usersToUpdate;
}
public void execute(QueueableContext context) {
System.attachFinalizer(this);
update usersToUpdate;
}
public void execute(FinalizerContext ctx) {
if (ctx.getResult() == ParentJobResult.SUCCESS) {
// Handle success
} else {
// Handle failure
}
}
}
Possible Properties:
- Should this rule be customizable via properties? - No, this is a straightforward rule with no properties.
Proposed Rule Name: Queueable Should Attach Finalizer
Proposed Category: Best Practices
Description: We want to warn the author of Apex whenever they write a class implementing the
Queueableinterface and they fail to attach aFinalizer. We should explain that the lack of aFinalizerprovides no handling capabilities should theQueueableaction fail.Code Sample:
Possible Properties: