Relax
Relax copied to clipboard
Relax runs jobs multiple times before completing
I have a few classes that extend relax.BatchableProcessStep and are created through JobScheduler.CreateOneTimeJob() with a start time of Datetime.now().addMinutes(-5).
After a few minutes, the job kicks off, and I can see through the dev console that it's running.
However, I find that one minute after the batch has started, the same batch class has been spawned again with the exact same parameters. Sometimes it's 1 extra copy, other times it's 2 extras.
There's no error taking place in the batch classes (I've surrounded them with try/catch), and the complete() method is being used at all exit points.
Attaching two files:
- Listing of Apex Jobs, see "UpdateItemStatusBatch":
- Output from the jobs that has been sent into Slack. Exactly the same output from the batch running twice.
I looked through the code, and wonder if this is what's happening:
- Job kicks off and spawns the batch
- In the meantime, the job scheduler runs again and sees that the job is still "Queued" (I've never seen it have a status other than "Queued" or "Completed"), that the next run time is before now and runs it again.
- By the time the job scheduler runs again, both instances have been completed.
So...
- Is there a reason why the job status never gets updated?
- Alternatively, if #1 is working as expected, can JobScheduler.GetNextRunTimeForJob() check if it's a one-time job and has already been created? In which case, it doesn't return a run time.
FWIW, I tried setting up the code on my own sandbox, but ran into a ton of errors while deploying it. Else I would have tried to fix it myself.
It also doesn't make a difference if the job is to be deleted after it is run.