5

I have a use-case where I need to execute some logic in async mode and I found that using Queueable Interface I can achieve that. But when I am trying to the use the Queuable Interface, some issues are occurring with the Queueable feature calls.

1) When I am trying to enqueue the same class for multiple time, then it's throwing below error :

Maximum stack depth has been reached.

2) When I change the logic, and tried to execute the Enqueue job from a future method, then it started throwing below error :

First error: Too many queueable jobs added to the queue: 2  

Below is the code I am trying to execute in Queueable Class:

public class SecondQueueableApex implements Queueable {

public Integer counter;

public SecondQueueableApex (Integer counter) {
   this.counter= counter;
}

public void execute(QueueableContext context) {
    Account a = new Account(Name='Aaccount'+counter,Phone='(415) 555-1212');
    insert a;      
}

}

@future(callout=true)
public static void executeFuture() {
    for(Integer counter=1;counter<10;counter++) {
        System.enqueueJob(new SecondQueueableApex(counter));
    }

}

These limitations are not mentioned anywhere in the salesforce documentation. Can anyone please help me understanding these issues ?

  • I have an open question about this myself. The problem seems to be that you can't chain Queueable and callout at the same time. I'm not sure if this is documented, but it seems you should simply use batchable instead. – sfdcfox Jul 27 '15 at 12:54
3

This will actually a bug in the code.

I can see this line -

System.enqueueJob(new SecondQueueableApex(counter));  

I assume this is calling itself, due to which it starts getting into stack.

This will not be limitation of Queueable interface, but more about the how it is being called.

Current limit for number of such chained jobs can be 5. Check "Queueable Apex Limits" in this reference

If you can add the complete class of Queueable interface, I can check further if still facing issues.

Update 1 From comment -

Initially Sf added this limitation where you can have maximum of 2 Queueable jobs. This limitation has been removed now. I strongly feel that this is because of your org not having proper updated Queueable interface features. Try logging a case or trying on an org from different continent. So suppose if you are on ap1 POD, some how try on a org with different POD like eu1 or na1 etc

Do let me know if it helps .

Thanks,

Ray

  • I have updated the Question with the code I am trying to execute. – S.Sharma Jul 27 '15 at 9:49
  • 2
    Got it. initially Sf added this limitation where you can have maximum of 2 Queueable jobs. This limitation has been removed now. I strongly feel that this is because of your org not having proper updated Queueable interface features. Try logging a case or trying on an org from different continent. So suppose if you are on ap1 POD, some how try on a org with different POD like eu1 or na1 etc. – Kaushik Ray Jul 27 '15 at 9:52
  • 1
    Actually this error "Too many queueable jobs added to the queue: 2" is only occurring when I am trying to call the Queueable interface code from the future method. If I directly call it from developer console, it doesn't give me any error. – S.Sharma Jul 27 '15 at 11:30
  • 1
    Guys, I have the same problem. It looks like if queueqble called from async method (future or batch chunk) it has such limit. Is there any method to check the context of execution (immediate or async ) in order to prevent invocation of queueable? – Yury Bondarau Sep 12 '15 at 13:30
0

It's right there in the documentation. You can't call multiple queueable from queueable.

When chaining jobs, you can add only one job from an executing job with System.enqueueJob, which means that only one child job can exist for each parent queueable job. Starting multiple child jobs from the same queueable job isn’t supported.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_queueing_jobs.htm#!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.