10

I'm trying to chain queueable interfaces within apex class, but receiving this error when it gets executed:

Too many queueable jobs added to the queue: 2

Here's a summary of the code:

public without sharing class UserProcess implements Queueable{

    public UserProcess () {

    }

    public void execute(QueueableContext context) {

        //Do stuff here
        //Chain Another Queueable Interface
        System.enqueueJob(new RankPermissionProcess(userRank));
     }

}
3
  • Sean, can you provide some information about how this class gets invoked.
    – manjit5190
    Sep 18, 2015 at 3:05
  • Hi I'm calling the First Queueable Interface from a Trigger using Id userUpdateJobId = System.enqueueJob(new UserProcess());
    – sfdx
    Sep 18, 2015 at 4:07
  • Your issue can be connected with undocumented limit for invocation of System.enqueueJob from batch context. See the comment here - salesforce.stackexchange.com/questions/57367/… Nov 9, 2015 at 21:50

2 Answers 2

17

Since you are calling the first queable from a trigger and the limit shows

Number of queueable jobs added to the queue: 1 out of 1

I believe what is happening is

  • You save a record and the trigger runs.
  • It enqueues your first job.
  • The first job runs.
  • The //do stuff (At line 1 in code below) performs a DML or a workflow update is triggered which causes the same trigger to fire again.
  • This trigger again enqueues a job.
  • Now the execution returns to line 3 which again tries to enqueue and fails.
public without sharing class UserProcess implements Queueable{
    public UserProcess () {
    }
    public void execute(QueueableContext context) {
        1 //Do stuff here
        2 //Chain Another Queueable Interface
        3 System.enqueueJob(new RankPermissionProcess(userRank));
     }
}

Quoted from Release notes: Queueable

Queueable Apex Limits

The execution of a queued job counts once against the shared limit for asynchronous Apex method executions.

You can add up to 50 jobs to the queue with System.enqueueJob in a single transaction.

The maximum stack depth for chained jobs is two, which means that you can link a job only once and have a maximum of two jobs in the chain.

When chaining jobs, you can add only one job from an executing job with System.enqueueJob.

You can use the Limits.getQueueableJobs() to check whether a job is already added.

Also you can debug a line before enqueuing job in a trigger and check how many times debug line appeared in the Log

1
  • 1
    This is right! Seems that the Trigger is executing twice and queueing the same class in the a single transaction. I had to check the Limits.getQueueableJobs() before I schedule the queueable class again. If( Limits.getQueueableJobs() == 0) { System.enqueueJob(new RankPermissionProcess(userRank)); }
    – sfdx
    Sep 20, 2015 at 21:14
4

I tried the below example and it works fine:

QueueableExampleOne

public class QueueableExampleOne implements Queueable{

    public void execute(QueueableContext context){
        System.debug(System.LoggingLevel.ERROR,'From queueable example one');
        System.enqueueJob(new QueuableExampleTwo());
    }

}

QueuableExampleTwo

public class QueuableExampleTwo implements Queueable{

    public void execute(QueueableContext ctxt){
        System.debug(System.LoggingLevel.ERROR,'From QueueableContext example two');
    }

}

I started the QueuebleExampleOne Job from developer console using the below code:

System.enqueueJob(new QueueableExampleOne());

And it worked fine and both the Queueable jobs execute methods executed successfully.

I am sure you might have read the below points from the documentation but posting here for the sake of complete answer.

  • No limit is enforced on the depth of chained jobs, which means that you can chain one job to another job and repeat this process with
    each new child job to link it to a new child job. For Developer
    Edition and Trial organizations, the maximum stack depth for chained
    jobs is 5, which means that you can chain jobs four times and the
    maximum number of jobs in the chain is 5, including the initial
    parent queueable job.
  • 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.
4
  • I'm checking the limits on the debug logs and it's showing this limit. I thought there is no limit on the depth of chained jobs. Number of DML rows: 2 out of 10000 Maximum CPU time: 0 out of 60000 Maximum heap size: 0 out of 12000000 Number of callouts: 0 out of 0 Number of Email Invocations: 0 out of 10 Number of future calls: 0 out of 50 Number of queueable jobs added to the queue: 1 out of 1 ******* CLOSE TO LIMIT Number of Mobile Apex push calls: 0 out of 10
    – sfdx
    Sep 18, 2015 at 2:27
  • 1
    Do you have any other schedule or batch jobs in the same transaction? Because mine shows : Number of queueable jobs added to the queue: 1 out of 50
    – javanoob
    Sep 18, 2015 at 2:33
  • I already checked Setup>Jobs>Apex Jobs and there are no other BatchClass that was queued on the Jobs
    – sfdx
    Sep 18, 2015 at 4:10
  • The parent job can enqueue up to 50 jobs per transaction, but a child's job can enqueue only one job till the dept of 5. Jun 15, 2020 at 16:40

You must log in to answer this question.

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