Detect if Apex Flex Queue is enabled in an Org

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.

Apex Flex Queue auto activation

Answer

Building on the approach recommended by @Amit

/**
* @author Scott Covert
* @date 8/3/2015
* @description Salesforce Utility Class
*/
public with sharing class SFUtils {
    /** 
    * @author Scott Covert
    * @date 8/3/2015
    * @description Determines if the current org has the Apex Flex Queue enabled
    * @return Boolean A boolean value representing whether or not the Apex Flex Queue is enabled for the current org
    */
    public static Boolean detectApexFlexQueue() {
        String sfURL = System.URL.getSalesforceBaseURL().getHost();
        PageReference pageRef = new PageReference('https://'+ sfURL +'/apexpages/setup/viewApexFlexQueue.apexp?retURL=%2Fui%2Fsetup%2FSetup%3Fsetupid%3DJobs&setupid=ApexFlexQueue');
        String pageRefBody;
        if (Test.isRunningTest()){
            pageRefBody = '';
        }
        else{
            Blob pageRefBlob = pageRef.getContent();
            pageRefBody = pageRefBlob.toString();
        }
        // Count # of times 'setupHighlightLeaf' appears in body
        Integer highlightLeafMatches = pageRefBody.countMatches('setupHighlightLeaf');
        // In orgs without the Apex Flex Queue enabled there should be no highlighted leafs
        // meaning there should only be one match from the HTMLTreeNode initialization
        if (highlightLeafMatches<=1){
            // Apex Flex Queue is not enabled
            return false;
        }
        else {
            // Apex Flex Queue is enabled
            return true;
        }
    }

}

Attribution
Source : Link , Question Author : Daniel Ballinger , Answer Author : Scott Covert

Leave a Comment