0

I have written a scheduled class which will run after every 1 min. From Scheduled Job I am calling a queueable class. Also, for rescheduling after 1 min, I have a platform event which will fire after commit.

Most of the time this job runs fine scheduling after every 1 min but suddenly after some days, it gets aborted and did not got rescheduled. Please let me know do you find any issue with the approach provided below:

public class CSC_SendCaseDataSCH implements Schedulable {

/**
Description: Call the queueable class so that ordered case data can be sent
**/
public void execute(SchedulableContext SC) {
    List<Transaction_History__c> transactionHistoryList = Database.query('Select Id From Transaction_History__c Where isSynced__c = false AND (sObject__c = \'Case\' OR sObject__c = \'CaseComment\' OR sObject__c = \'EmailMessage\') limit 1');
    
    if(transactionHistoryList.size() > 0){
        List<AsyncApexJob> asyncApexJobList = new List<AsyncApexJob>([SELECT Id from AsyncApexJob Where ApexClass.Name like 'CSC_SendCaseDataQueueableAsync' And (Status = 'Holding' OR Status = 'Preparing' OR Status = 'Processing' OR Status = 'Queued')]);
        if(asyncApexJobList.size() == 0){
            System.enqueueJob(new CSC_SendCaseDataQueueableAsync());
            if(!Test.isRunningTest()){
                System.abortJob(sc.getTriggerId());
            }
        }else{
            Reschedule_Job__e rescheduleJobEvent = new Reschedule_Job__e(Class_Name__c = 'CSC_SendCaseDataSCH', Specific_Minutes__c = Integer.valueOf(Label.Reschedule_CSC_SendCaseDataQueueableAsync));
            EventBus.publish(rescheduleJobEvent);
            if(!Test.isRunningTest()){
                System.abortJob(sc.getTriggerId());
            }
        }
    }else{
        Reschedule_Job__e rescheduleJobEvent = new Reschedule_Job__e(Class_Name__c = 'CSC_SendCaseDataSCH', Specific_Minutes__c = Integer.valueOf(Label.Reschedule_CSC_SendCaseDataQueueableAsync));
        EventBus.publish(rescheduleJobEvent);
        if(!Test.isRunningTest()){
            System.abortJob(sc.getTriggerId());
        }
    }
}

}

1 Answer 1

1

This may happen when there are exceeded limit of apex jobs in hold status. When it happens, the scheduler cannot be scheduled again (put in the queue), because the queue is already full.

This may happen during business hours of your salesforce instance. (It was the reason in my same case)

The only approach here so that the job is not aborted completely (just not fired several times a day) is to create 60 schedulers firing each every 1 hour but in different minutes. For example, AWS managed package does the same.

2
  • We can have only 100 scheduled jobs. If i used 60 of them then there wont be much limit left for others.
    – Sukruti
    Commented Oct 20, 2023 at 9:14
  • That's true, that's why we left it as it was in our case (I just check sometimes the apex jobs queue, if it's full, run the job manually, then after tough high-load just reschedule the job). Commented Oct 20, 2023 at 13:02

You must log in to answer this question.

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