Schedule job after parent job executes succesfully

Hi everyone,

I’m wondering if such a feature is supported somehow. Consider this scenario:

I’m importing a large CSV file to a database in ImportCSV job.
At the same time, I want to schedule a job to be executed at a later time to do something with the data I’m importing, let’s call it ProcessUsers. This job should be executed only if the parent job (ImportCSV) has been completed successfully AND the scheduled date has occurred or passed.

The Continuations API doesn’t seem to support this, unless I’m missing something.

Any thoughts?

Thank you!

Have the continuewith action be to trigger a delayed background job?

Hi Emyr,

How to do this exactly?

BackgroundJob.ContinueWith(parentJobId, () => DoDelayedBackgroundJob());

will execute the job as soon as parentJob is completed. I don’t understand how to schedule the delayed job with a continuation.

BackgroundJob.ContinueWith(parentJobId, () => BackgroundJob.Schedule(()=>DoStuff(),TimeSpan or DateTimeOffset) );

1 Like

That’s an interesting approach but it doesn’t seem to work:

Failed
Can not change the state to 'Enqueued': target method was not found.

Newtonsoft.Json.JsonSerializationException

Unable to find a constructor to use for type System.Linq.Expressions.Expression`1[System.Func`1[System.Threading.Tasks.Task]]. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute

I guess it can’t serialize the parameters passed to the Schedule method…

After exploring the source code a bit I figured out a nice solution.

The idea is to use the underlying BackgroundClientJob object which exposes some nice extra features:

    var hangfireClient = new BackgroundJobClient();
    hangfireClient.ContinueWith(parentJobId,
          () =>
                DoDelayedBackgroundJob(),
                new ScheduledState(executionDate));