How to resolve this error - too many queueable jobs added to the queue 2?

2.1K    Asked by ClaudineTippins in Salesforce , Asked on Apr 17, 2023

I've been looking for similar questions, but none of them applies to my scenario, so I'd be grateful if you can explain to me why my logic is failing, and what I can do in order to fix it.

I want to run every month a logic on Account records (>50k) : BATCHS

This logic will create X events on each account : Event Trigger For new Events, I need to sync the data with other system : @future method So, since Batchs cannot call other future methods, I decided to implement the @future logic inside a Queueable class.

Here is a snippet:

Batch
global class BATCH_GeneradorEventos implements Database.Batchable
{
       ...
       global void execute(Database.BatchableContext BC, List scope)
       {
       ...
          insert evs;
       }
       ...
}
Event trigger
ID jobID = System.enqueueJob(new EventQueueableJob(Trigger.newMap.keySet()));
Queuable class
public class EventQueueableJob implements Queueable, Database.AllowsCallouts
{
    ...
    public void execute(QueueableContext context) 
    {
      // future methods and logic
    }
}
The error that I'm getting is:
"Too many queueable jobs added to the queue: 2"

and it's giving me for every SerialBatch.

Is it because I'm doing an enqueueJob for every iteration of the Batch? If so, how can I fix/remodel it?

Answered by Ishii Watanabe

To resolve this error - too many queueable jobs added to the queue 2, check out this use case-


Given: Batch job execute() method that updates two records A and B

Given a Process Builder with two decision blocks / action group pairs that

execute upon changes to records A and B, respectively.

That is decision block 1 applies to record A an  decision block 2 applies to record B.

Given that the Process Builder action groups do either DML or call invocable apex. Underlying apex code then does a System.enqueueJob(...) Although Process Builder is bulkified, it is not bulkified across action groups. Thus, even if the action groups invoke the same invocable method or DML the same SobjectType, you will end up with two separate System.enqueueJob calls. Bottom line, code defensively before executing System.enqueueJob(..) as you might be surprised over time the context in which it is invoked. For example, in our use case, we had good unit tests that made sure that service X enqueued the job as expected; but no unit tests that checked to see if the service was called twice from a batchable context.



Your Answer

Answer (1)

The error "too many queueable jobs added to the queue" typically occurs in systems that utilize a queuing mechanism for background or asynchronous processing, such as in Salesforce with its Queueable Apex jobs. This error indicates that you are adding more Queueable Apex jobs to the queue than the system's governor limits allow.


Here are some steps you can take to resolve this error:

Reduce the Number of Queueable Jobs: Review your code to see if you can optimize it to reduce the number of Queueable Apex jobs being added to the queue. Are there any unnecessary or redundant jobs being queued? Can you combine multiple jobs into one to reduce the overall load on the queue?

Check for Recursive Queueable Calls: Make sure that your Queueable Apex jobs are not calling themselves recursively, as this can quickly escalate the number of jobs in the queue and lead to this error.

Implement Throttling Mechanisms: Implement mechanisms in your code to throttle the number of Queueable Apex jobs being added to the queue within a certain time period. You can use custom logic to control when and how many jobs are queued based on factors such as system load or available resources.

Optimize Job Execution: Review the execution time of your Queueable Apex jobs and optimize them for efficiency. This can help reduce the likelihood of hitting governor limits related to asynchronous processing.

Monitor and Tune: Continuously monitor the performance of your system and tune your code as necessary to prevent hitting governor limits. Keep an eye on the number of jobs in the queue and adjust your code accordingly.

Consider Alternative Solutions: Depending on your specific use case, consider alternative solutions such as Batch Apex or Scheduled Apex if they better suit your requirements and help avoid hitting governor limits.

By following these steps and carefully managing the number of Queueable Apex jobs being added to the queue, you should be able to resolve the "too many queueable jobs added to the queue" error in your Salesforce org.

4 Days

Interviews

Parent Categories