Skip to content

[apex] New Rule: Queueable Should Attach Finalizer #5302

@mitchspano

Description

@mitchspano

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:new-ruleProposal to add a new built-in rule

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions