apex scheduler- what if scenarios?

2019-08-22 04:40发布

问题:

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' Scenarios. 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 scenarios that can realistically cause my jobs to not terminate? I thought of a scenario 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.

回答1:

  1. Any error in a batch will stop that batch from finishing correctly.
  2. I suggest you do some kind of check to know that you processed everything. What I usually use for batches is to mark somehow the records that need processing to distinguish them from the ones that don't. When I finish processing a batch, I mark the records as processed. Then you can check in the finish() method if any record still needs processing and reschedule the batch to run again.