Saturday, January 27, 2024

Provide a detailed explanation for the error "Maximum stack depth has been reached"?

When chaining jobs with System.enqueueJob, you can add only one job from an executing job. Only one child job can exist for each parent queueable job as shown in below example. Starting multiple child jobs from the same queueable job isn’t supported.

public class SampleQueueableJob implements Queueable {

    public void execute(QueueableContext context) {

        system.debug('Queueable job called');

       System.enqueueJob(new SampleQueueableJob2() );

    }

}

Because no limit is enforced on the depth of chained jobs, you can chain one job to another. You can 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. The maximum number of jobs in the chain is 5, including the initial parent queueable job. If you try to chain more than 5 jobs in developer edition you will get the error Maximum stack depth has been reached.

Let us understand this with an example in developer edition org.

We will call SampleQueueableJob2 from SampleQueueableJob, SampleQueueableJob3 from SampleQueueableJob2, SampleQueueableJob4 from SampleQueueableJob3, SampleQueueableJob5 from SampleQueueableJob4.

public class SampleQueueableJob implements Queueable {

    public void execute(QueueableContext context) {

        system.debug('Queueable job called');

        System.enqueueJob(new SampleQueueableJob2() );

    }

}


public class SampleQueueableJob2 implements Queueable {

    public void execute(QueueableContext context) {

        system.debug('Queueable job called');

        System.enqueueJob(new SampleQueueableJob3() );

    }

}


public class SampleQueueableJob3 implements Queueable {

    public void execute(QueueableContext context) {

        system.debug('Queueable job called');

        System.enqueueJob(new SampleQueueableJob4() );

    }

}


public class SampleQueueableJob4 implements Queueable {

    public void execute(QueueableContext context) {

        system.debug('Queueable job called');

        System.enqueueJob(new SampleQueueableJob5() );

    }

}


public class SampleQueueableJob5 implements Queueable {

    public void execute(QueueableContext context) {

        system.debug('Queueable job called');

    }

}

Let us try to call SampleQueueableJob from developer edition.

We can see the jobs are successfully executed.

Maximum stack depth has been reached

Maximum stack depth has been reached


Let us now try to call SampleQueueableJob6 from SampleQueueableJob5.

Update the class SampleQueueableJob5 to call SampleQueueableJob6.


public class SampleQueueableJob5 implements Queueable {

    public void execute(QueueableContext context) {

        system.debug('Queueable job called');

        System.enqueueJob(new SampleQueueableJob6() );

    }

}


public class SampleQueueableJob6 implements Queueable {

    public void execute(QueueableContext context) {

        system.debug('Queueable job called');

 

    }

}


Let us try to call SampleQueueableJob from developer edition. 

We can now see the error "Maximum stack depth has been reached". This is because for Developer Edition and Trial organizations, the maximum stack depth for chained jobs is 5.

Maximum stack depth has been reached



No comments:

Post a Comment