We have an Apex class that automatically schedule a Job to be run 10 min after the request is done. In some cases, this job doesn't run at all and since we cannot schedule another job (business requirement) we wait at least 3 hours to see if the job is executed.
After that time, we want to abort the job.
In order to know if a job is scheduled we are using the following SOQL:
List<AsyncApexJob> jobs = new List<AsyncApexJob>(
[SELECT Id, Status, CreatedDate FROM AsyncApexJob WHERE ApexClass.Name = 'ConnectUpdateBatch' AND Status NOT IN ('Aborted', 'Completed', 'Failed')]
);
Using the Created Date, we know if the job is waiting for execution for more that 3 hours
Now our question is: how we can abort the job using Apex during the same verification process?