3

I'm building a system that will be calling executeBatch on quite a few batch apex jobs. I want to make sure that I haven't already hit the limit on the max number of jobs queued up. I believe Salesforce has something called a FlexQueue, but I'm not able to find an API for v38.0. Is there another way I can get the total number of jobs queued/running, as well as the upper limit possible?

Also, will jobs scheduled with System.schedule go to the same job queue?

2 Answers 2

6

You can easily get the details in apex

Integer enqueuedJobs = [SELECT COUNT() FROM AsyncApexJob WHERE JobType='BatchApex' AND Status IN ('Processing','Preparing','Queued')] ;

You can handle this in your code

//check if there are 5 active batch jobs
//In some cases, might need to add "Status='Queued' " to the criteria
if ([SELECT count() FROM AsyncApexJob WHERE JobType='BatchApex' AND (Status = 'Processing' OR Status = 'Preparing')] < 5){ 
   Database.executeBatch(batchClassInstance);
} else {
   //schedule this same schedulable class again in 30 mins
   nameOfYourSchedulableClass sc = new nameOfYourSchedulableClass();
   Datetime dt = Datetime.now().addMinutes(30);  // i.e. 30 mins
   String timeForScheduler = dt.format('s m H d M \'?\' yyyy');
   Id schedId = System.Schedule('MatrixRetry'+timeForScheduler,timeForScheduler,sc);
}

Now with Apex Flex queue we can submit 100 batch for preocessing and they will be sitting in queue with status "Holding". Once we have available resource then it will start 5 processing them (5 parallel at a time) with status "Queued" and "Processing"

Reference

6
  • 1
    Okay, thanks... Do scheduledApex and BatchApex share the same upper limit of 100? Also, is the limit actually 100?
    – coldspeed
    Jan 18, 2017 at 9:54
  • @shiva yes same limit forboth. The 100 limit is for where you can schedult to run them in future. At any time the upper limit is only 5 for now. Jan 18, 2017 at 9:56
  • This is confusing. Doesn't executeBatch() also queue jobs for execution? Meaning, the 100 limit should also apply to jobs that have been dispatched, right...? I've read that if a job is to be executed, but no resources are available, it is put into the FlexQueue with status = 'Holding'.
    – coldspeed
    Jan 18, 2017 at 9:59
  • @Shiva yes they will be in Holding state and run in future when resource available but ata moment only 5 jobs will be executed in parallel. I hop this make you clear. Jan 18, 2017 at 10:07
  • Am almost clear. So if a job has the status 'Queued', then where is it sitting? Also, could you put this info in your main answer so I can mark it accepted?
    – coldspeed
    Jan 18, 2017 at 10:12
1

Uisng below piece of code you can retrieve currently running jobs

List<AsyncApexJob> batchJobs = [
    SELECT ApexClassId 
    FROM AsyncApexJob 
    WHERE 
        JobType = 'BatchApex' AND 
        Status = 'Processing' // OR Status = 'Queued'ADD ANY OTHER CONDITION THAT YOU WANT
];

Using above piece of code you can check how many of them are in Processing or Queued status based on the List<AsyncApexJob> you can do your processing.

3
  • Thank you. What about if the status is Holding?
    – coldspeed
    Jan 18, 2017 at 10:01
  • add a condition as Status = 'Holding'
    – RCS
    Jan 18, 2017 at 10:02
  • I'm asking if it's applicable or not. Is the limit 100, or is it 5?
    – coldspeed
    Jan 18, 2017 at 10:04

You must log in to answer this question.

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