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());
}
}
}
}