I have a managed package that starts a batch job.
Right now, before attempting to start the batch it does a check like:
integer batchJobLimit = 5;
integer currentBatchCount = [SELECT count()
FROM AsyncApexJob
WHERE JobType='BatchApex' AND (Status='Processing' OR Status='Preparing')];
// Maybe Status='Queued' as well
if (currentBatchCount < batchJobLimit) {
XyzBatchUploadOrderAttachment batchJob = new XyzBatchUploadOrderAttachment(parameters);
integer batchSize = 1;
Database.executeBatch(batchJob, batchSize);
} else {
// Contingency plan to either reschedule starting the batch or give up in disgust
}
The same pattern appears in Apex Batch job limits not clear Queued vs Scheduled and How to avoid hitting the concurrent Batch Apex limit with error: "Attempted to schedule too many concurrent batch jobs in this org"?
If the Apex Flex Queue Critical Update has been installed in the org running that managed package, then the batch job limit of 5 doesn't need to be enforced. Instead I could check if I'm going to hit the 100 queued jobs with the 'Holding' status limit.
How can I tell if the Apex Flex Queue is available in apex?
Maybe I just need to make the limit enforced by the code configurable.
Note that, as at Summer '15, the Apex Flex Queue isn't currently active in all orgs. It is a critical update, with an Auto-Activation date of 5/22/2015, that doesn't appear to auto activate.