4

Suppose I have a batch running that is processing millions of records(that would take hours), then I deploy a code during execution that changes the batch logic, will remaining record get processed with old logic or new logic?

I kinda have a feeling salesforce maintains an old version of the code before the start of async apex to make sure start, execute, finish executes on the same piece of code at the time of start.

On our prod , there was faulty code that from queueable used to queue itself using System.enqueueJob(this); , now after deployment, we removed that logic, but still old jobs kept getting in infinite execution loop for days, eating our AsyncApexLimit.

Is there any documentation of this? Or someone can confirm this behavior?

3
  • Pretty sure either you won't be able to deploy (cannot modify scheduled job) or your @future methods would run as they were before the deploy, but I don't have any references for this. This would be an excellent question to ask the salesforce devs at conferences such as dreamforce. Commented Jul 19, 2019 at 14:20
  • 1
    There's an option to allow deployments to go through even if Apex jobs are in progress, but I don't know very much about the underlying platform behavior.
    – David Reed
    Commented Jul 19, 2019 at 14:48
  • A safe option is to abort the job, deploy code, submit job again. I’m not aware of how the platform handles modified code while jobs are running, but doesn’t sound like a recipe for success while the behavior is not well documented.
    – Doug Ayers
    Commented Jul 20, 2019 at 18:08

1 Answer 1

2

OK, lucky for you I happened to have a dev environment with a million Asset so I could launch a job, have it run, then change the code and (deploy) the changed code to see if the batchable uses the 'new" logic.

And the answer is ....

YES, deploying a change in logic to the batchable's execute() will pick up the changes after the deployment while the job is running.

Proof point:

  • Batchable (original version) published a platform event on each execute() that was subscribed by a NOP trigger
  • Batchable (new version) did not publish the platform event

After deploying the new version, the query:

select position, tip, name from EventBusSubscriber where name ='MyNopTrigger'

stopped incrementing the values of Tip and Position, indicating that publishing had stopped

What I did not try:

  • Changing the start() method to select a different set of records - I sincerely doubt this would have an effect as the Apex Jobs monitoring page did not restart the query upon deploying the new version
  • Changing the finish() method to do something different than the original. I suspect that since the execute() did show the behavior change, by the time finish() executed, it to (as a separate transaction) would show the behavior change of a new version.

Code used:

public with sharing class SfseInflightSchedulable implements System.schedulable, Database.Batchable<SObject> {
    public void execute(SchedulableContext sc) {
        Database.executeBatch(new SfseInflightSchedulable(),10);
    }

    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator([Select Id from Asset]);
    }    
    public void execute(Database.BatchableContext bc, List<Object> assets) {
        //  start by running to publish Platform Event that can be monitored
        EventBus.publish(new MyPlatformEvent__e());

        //  after batch is running, redeploy by commenting out the publish
    }

    public void finish(Database.BatchableContext bc) {
    }


}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .