2

I have a batch job that reschedules itself on completion so that it can essentially run indefinitely. Within its finish method it calls system.scheduleBatch to start itself again after a defined interval.

This works well, and is backed up by a Schedulable implementation that periodically checks both CronJobDetail and AsyncApexJob to resurrect it if required.

The problem occurs after refreshing a sandbox from production where the recurring batch and scheduled job are running.

After refreshing a sandbox it was found that the batch job wasn't running. However, attempting to call system.scheduleBatch with the standard jobName gives an error that a job with the name is already scheduled.

XyzBatchOrderSynchronization batch = new XyzBatchOrderSynchronization();
string jobName = 'XYZ Batch Order Synchronization';
string jobId = system.scheduleBatch(batch, jobName, 3, 1);

System.AsyncException: The Apex job named "XYZ Batch Order Synchronization" is already scheduled for execution.

A query against CronJobDetail with the job name returns a result: (Job Type 7 is Scheduled Apex)

Select Id,Name,JobType from CronJobDetail where Name = 'XYZ Batch Order Synchronization'

enter image description here

However, a query against CronTrigger with that CronJobDetailId returns no results:

Select Id,CronJobDetailId from CronTrigger where CronJobDetailId = '08a5000000E07EzAAJ'

Without the CronTrigger Id I can't use System.abort(jobId) to clear the old job.

How can I clear out the rogue scheduled batch job that came via the sandbox refresh process?

The customer is raising a support case, but it seems odd that I can't get to it. I guess I could modify the code to make the job name unique/configurable, but it seems odd to leave the old scheduled batch lying around not running.

  • Not only does it not run, it also takes up one of your concurrently scheduled classes. – sfdcfox Jul 21 '15 at 22:09
3

You can't fix this. If it happens, it happens, and you need to submit a case. Basically, one of the tables on the backend gets out of sync because the snapshot happened at just the wrong time. This results in a job with no detail record associated to it. As far as I know, the only way to fix this is to manipulate tables we don't have direct access to... so you need support. Last year I ended up having to submit about 4 or 5 such cases, because we'd spin up two sandboxes (dev, QA), and end up with one or the other stuck in limbo. Of course, there's ways around this, like using random job names, but you'll still be penalized against the system-wide limits for how many jobs you can have scheduled at once. About the only way you can almost certain it won't happen is to stop all scheduled jobs before attempting to copy your sandbox, and waiting until the copy completes. This is often impractical, so simply submitting the case is usually your best option.

  • I suspected this might be the case. I did suggest deactivating the scheduled job before performing the refresh. The customer wasn't keen on the idea of not having the process running in production for the duration of the sandbox refresh. Did support ever show any interest in getting this fixed long term? – Daniel Ballinger Jul 21 '15 at 22:13
  • @DanielBallinger As far as I know... No. The problem is they have lots of Known Issues, and this has a Known Workaround, so they're apparently not too keen on the idea of wasting time on this. I might ask around and see if anyone knows. It'd be nice to have it fixed. – sfdcfox Jul 21 '15 at 22:27
  • It would be nice to get it up on the known issues board at the very least (I couldn't find any existing references around CronJobDetail or CronTrigger). Do you have any similar case numbers that my customer can reference in their support case? It might speed up the resolution. Thanks. – Daniel Ballinger Jul 21 '15 at 22:35
  • 1
    @DanielBallinger Sorry, I'm no longer where I was, so I don't have access to those cases any more. I usually had a subject of something like "Please delete phantom jobs" with a body along the lines of "We are unable to schedule job XYZ, because it says that job is already scheduled. Unfortunately, we can't query this job or see it in the scheduled jobs list, so we can't abort it to restart it. Please delete any phantom jobs we have." Of course, grant login access, and you're good. We had premier support, so they usually had it cleared in <1 hour. – sfdcfox Jul 21 '15 at 22:38
  • The customer went through the support process with case #12282309. As you said, the cleared they "phantom batch" in each affected sandbox and it started working again. They confirmed it was a known bug, but didn't provide a public known issue reference. – Daniel Ballinger Jul 22 '15 at 19:40

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.