Relax icon indicating copy to clipboard operation
Relax copied to clipboard

Relax runs jobs multiple times before completing

Open jeremyhiggs opened this issue 8 years ago • 1 comments

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": screen shot 2016-12-16 at 16 07 38
  • Output from the jobs that has been sent into Slack. Exactly the same output from the batch running twice. screen shot 2016-12-16 at 22 11 15

I looked through the code, and wonder if this is what's happening:

  1. Job kicks off and spawns the batch
  2. 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.
  3. By the time the job scheduler runs again, both instances have been completed.

So...

  1. Is there a reason why the job status never gets updated?
  2. 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.

jeremyhiggs avatar Dec 16 '16 17:12 jeremyhiggs

It also doesn't make a difference if the job is to be deleted after it is run.

jeremyhiggs avatar Dec 16 '16 21:12 jeremyhiggs