1

we have batch which is scheduled to run every 5 minutes. This batch was working well for few months and then on 7th of November it didn't run for 24 hours and 5 minutes. After that, it is running well again.

On Sunday, 4 November 2018 there was time changing 1 hour backward. I am thinking about, if there can be some connection between these events.

My question is, if anyone else faced similar issue this week, or it was only in our situation.

To schedule batch every 5 minutes we use this String which we pass to system.schedule after system.abortJob...

public static string GetCron(){

    Datetime RightNow = datetime.now();
    RightNow = RightNow.addMinutes(5);
    String strSec = string.valueof(RightNow.second());
    String strMin = string.valueof(RightNow.minute());
    String strHour = string.valueof(RightNow.hour());

    String strDay = string.valueof(date.today().day());
    String strMonth = string.valueof(date.today().month());
    String strYear = string.valueof(date.today().year());

    String sch = strSec+' '+strMin+' '+strHour+' * * ? *';
    system.debug(logginglevel.ERROR, 'CRON_EXP ' + sch);
    return sch;
}
0

I haven't run into your particular issue, but following my Salesforce mantra of "If it's broken, good luck getting salesforce to fix it, just work around it" you might consider switching to a sleep pattern.

Instead of scheduling the batch to run every five minutes, in the finish method of you're batch, schedule it to run again in a few minutes with the System.scheduleBatch() method (docs). It saves you from worrying about any timezone non-sense or dealing with cron strings. You can even carry over state from the current batch if you want.

// schedule a new instance 
public void finish(Database.BatchableContext BC) {
    System.scheduleBatch(new MyBatch(), 'MyBatch_' + DateTime.now().format(), 5);
}

// re-run the current batch with any state you have
public void finish(Database.BatchableContext BC) {
    System.scheduleBatch(this, 'MyBatch_' + DateTime.now().format(), 5);
}

To be safe it may also be worth having a scheduled process check that the batch job is queued in the rare chance you get an exception during he finish method or other platform issue that prevents a batch from successfully scheduling the next one.

| improve this answer | |
  • Thanks a lot for answer. Right now everything is working well, so hope nothing similar will happen in future. – Karol Macák Nov 12 '18 at 7:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.