SFDC Apex Flex Queue Limit

Posted by ExiaHuang on April 3, 2017

Exception message

1
2
3
4
5
Apex script unhandled exception by user/organization: xxxx_user_id/xxxx_org_id

Scheduled job 'xxxxxx_schedule_class' threw unhandled exception.

caused by: System.AsyncException: You've exceeded the limit of 100 jobs in the flex queue for org xxxx_org_id. Wait for some of your batch jobs to finish before adding more. To monitor and reorder jobs, use the Apex Flex Queue page in Setup.

Reason of the Error

Holding Batch Jobs in the Apex Flex Queue

With the Apex flex queue, you can submit up to 100 batch jobs.

The outcome of Database.executeBatch is as follows.

  • The batch job is placed in the Apex flex queue, and its status is set to Holding.
  • If the Apex flex queue has the maximum number of 100 jobs, Database.executeBatch throws a LimitException and doesn’t add the job to the queue.

Count the flex Queue Size

1
Integer flexQueueSize = [SELECT COUNT() FROM AsyncApexJob WHERE Status = 'Holding' FOR UPDATE];

How to stop job by using apex?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
List<AsyncApexJob> jobs = [SELECT 
    Id,
    CreatedDate,
    CreatedById,
    JobType,
    ApexClassId,
    Status,
    JobItemsProcessed,
    TotalJobItems,
    NumberOfErrors,
    CompletedDate,
    MethodName,
    ExtendedStatus,
    ParentJobId,
    LastProcessed,
    LastProcessedOffset
 FROM AsyncApexJob
where ApexClass.name= 'bacth class name'
and Status = 'Holding'];
for(AsyncApexJob job : jobs){
    System.debug('stop job : ' + job.id + ', ApexClassId= ' + job.ApexClassId);
    System.abortJob(job.id);
}

AsyncApexJob status, ジョブ状況

status 状況 説明
Holding    
Queued キュー ジョブは実行待ちです。
Preparing 準備中 ジョブの start メソッドが呼び出されました。この状況は、レコードのバッチサイズに応じて数分かかることがあります。
Processing 処理中 ジョブは処理中です。
Aborted 中止 ジョブはユーザによって中止されました。
Completed 完了 ジョブはエラーあり/なしで完了しました。
Failed 失敗 ジョブでシステム障害が発生しました。

AsyncApexJob JobType, ジョブ種別

status 状況 説明
Future    
SharingRecalculation    
ScheduledApex    
BatchApex    
BatchApexWorker    
TestRequest    
TestWorker    
ApexToken    
Queueable