function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
nivyajnivyaj 

Apex Scheduler- how to manage failures?

I'm looking into using apex scheduler to run a batch job. We can run it whenever we want, say 3 am when traffic is low, but I'm trying to anticipate some 'Oh-Crap' Scenerios. Can you guys help answer these questions for me?

 

Setup:

When the user modifies a territory 100k's of records can get updated.

 

 

Questions:

  1. What are some scenerios that can realistically cause my jobs to not terminate? I thought of a scenerio of- when a user is editing a contact which happens to be in one of the batches is being run.

  2. What are some good practices for dealing with jobs that don't terminate? I was thinking about scheduling another batch job 2 hours after this one, but I don't see my boss liking that. I thought about maybe doing something in the finish() method- but don't know what exactly.

Thanks,

sfdcfoxsfdcfox

1) Jobs always terminate, assuming they start. They may finish gracefully, or be user aborted, but they will run to completion. The only major issue is if they never start, which is pretty much limited to trying to update more than 50,000,000 records in a single batch. You do need to be aware of governor exceptions though. This can cause a batch of 200 records to fail to update while the rest of them update correctly. You will not be able to ever have a scenario where a user is editing a contact that is part of the batch, because one of three conditions will happen: (a) the user will be told to reload because their view is inconsistent with the database data, or (b) the user's work will be sideswiped by the batch process (potentially), or (c) the batch process would step over that record and get to it next time (as in, 24 hours later, for example). It depends on exact;y when the edit occurs.

 

2) There's no point in running a second batch, because in the (rare) condition that a record is missed, it will be updated on the next pass anyways. You might choose to use a date field to identify when the last successful update on the record happened through a batch process, perhaps, but that's really about all you'd want to do. Batches are transactionally isolated from all other edits, so you can even run the batch at noon if you wanted to with very little mishap (see 1). Obviously, not disrupting the user base is why most jobs are run late at night, but the choice is yours.