0

I want to schedule a job every 15 mins, for now I have created a scheduler class and using 4 statements to schedule it, but then again there is no guarantee that a job might complete itself in 15 mins and there might be the need to abort the next one while the first one is still being executed. I was planning on aborting the second job in execute method of Schedular using Cron Trigger to get the list of job if it's still executing and then abort the next acquired job with the help of job name.Something like this:

List<CronTrigger> cronTriggerList =[SELECT CronJobDetail.Name, CronExpression, OwnerId FROM CronTrigger WHERE CronJobDetail.JobType = '7' and  State='EXECUTING' and CronJobDetail.Name like :Name+'%'];
            if(!cronTriggerList.isEmpty() && cronTriggerList.size()>0)
            {
                 List<CronTrigger> cronTriggerList01 =[SELECT id,CronJobDetail.Name, CronExpression, OwnerId FROM CronTrigger WHERE CronJobDetail.JobType = '7' and State='ACQUIRED' and CronJobDetail.Name like :Name+'%'];
                 for(CronTrigger ct:cronTriggerList01)
                  {
                     System.abortJob(ct.id);
                   }
             }

My question does it make more sense to schedule the job in finish method of a batch rather than aborting it from execute method in Scheduler?

1 Answer 1

2

I have several batch jobs that I used to schedule. For example, one job had to be executed 10 times per hour, so I scheduled it at 02, 08, 14, ... minutes after the hour.

Usually the time between those executions was long enough, so that no overlap could occur. Sometimes overlap did occur, but in this case that was not disastrous.

Then a disaster happened: the performance of the external system, with which the batch job communicated, degraded severily, and when I got at work in the morning, hundreds of batch jobs were waiting to be executed.

So I changed the batch jobs that had a high frequency of exection (10 x per hour) to a single batch job that schedules the next one in its finish method. That way the job can never overlap with another job and there is no chance of creating a backlog of jobs. I think it is a cleaner solution than working with abort.

The only drawback - as far as I can tell - is that if something goes terribly wrong, the finish method might not be executed and no batch jobs are executed at all.

2
  • 1
    I'd say that "something going terribly wrong" would be unlikely. Recall that start, (each call to) execute and finish are invoked in isolated transactions so finish should still be called. Now, your finish code could violate governor limits at which point the scheduling of the next invocation could easily be rolled back and you are left with no batch running at all.
    – Phil W
    Commented Mar 31, 2021 at 15:47
  • You could consider using Queueables for this instead. Once the Transaction Finalizer GAs this would be a robust way to handle things (albeit without the easy System.scheduleBatch "pause" between executions).
    – Phil W
    Commented Mar 31, 2021 at 15:49

You must log in to answer this question.

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