3

I am wrapping my head around how to use Apex Queuable as a queue and not as a chain.

As I understand all the code samples out there, the next job is enqueued after the work of the previous one is done. But that is a chain and not a queue.

I need something where I get the job ids upfront when I enqueue my jobs so I can later monitor their execution.

List<Id> jobIds = new List<Id>();

jobIds.add( System.enqueueJob(new FirstRunThis() );
jobIds.add( System.enqueueJob(new ThenThis() );
jobIds.add( System.enqueueJob(new AndThisOneLast() );

...

public List<AsyncApexJob> pollForStatus(List<Id> jobIds) {
    return [SELECT Id, ApexClass.Name, Status 
            AsyncApexJob WHERE Id IN :jobIds ];
}

How can I use Queuable with all its benefits but get some handles for UI polling upfront? If you have code samples, I will send extra love.

5
  • I think Platform events are the best option. Depending on your use case, you maybe can utilize ChangeDataCapture to not create PlatformEvent just for that
    – ytiq
    Mar 17, 2021 at 10:45
  • as a side note would recommend github.com/ytiq/apex-queueable-wrapper to ease up chaining or some other solutions to handle potential Limits
    – ytiq
    Mar 17, 2021 at 10:47
  • the next job is enqueued after the work of the previous one is done not always the case, from what I remember Salesforce can run up to 5 jobs simultaneously depending on your Salesforce Edition. Here is the answer from sfdxfox on it salesforce.stackexchange.com/questions/165430/…
    – ytiq
    Mar 17, 2021 at 10:53
  • also found this option for easier chaining salesforce.stackexchange.com/questions/188774/…
    – ytiq
    Mar 17, 2021 at 11:27
  • A queue which runs jobs in parallel when you say enqueue() is just a crappy design.Especially if it fails when you do this more than 5 times. Mar 18, 2021 at 22:21

1 Answer 1

4

Publish Platform Events in the Queueables and monitor them by subscribing to those.

OK, this adds complications, the biggest being that in the Apex the code runs under a single "service" user. But server-side subscription is done via the familiar trigger handler pattern and client-side subscription has a ready made lightning-emp-api component.

So no polling needed, and the Platform Event(s) can carry whatever information you like.

(And if you have lots of logic based on job ids, then the Platform Event could just carry that, being published when the Queueable starts, and being stored somewhere in the data model by an Apex subscriber.)

PS

To clarify on the "service" user, though Platform Events might be published via many different users, the Apex subscription runs under a single user. So for example, DML will be labelled as done by that single user. By default the user is the "Automated Process" user, but you can now nominate a different user; see Configure the User and Batch Size for Your Platform Event Trigger.

3
  • 1
    Note that Spring '21 adds the ability to configure trigger-based platform event subscribers to execute with a defined scope size and as a specific user. See the release notes for details. That said, we haven't managed to get this working in our managed package development at this stage. Also note that platform event metadata has specific limitations around data types and do not support related lists.
    – Phil W
    Mar 17, 2021 at 13:08
  • 2
    Hi @PhilW, I'll clarify that in the answer: it is still a single user rather than the various users that published the Platform Event. I do have that working now for one of our products, but had to do the setup via the Tooling API in a setup page as this is not yet supported AFAIK in the standard Setup UI.
    – Keith C
    Mar 17, 2021 at 14:17
  • Also, be aware that you have a hard limit of 250,000 platform events published per hour in the org, across all publishers. Depending on how you use PEs elsewhere, using them to log queueable success may push you towards that limit. Jul 27, 2021 at 16:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .