# Schedule job after parent job executes succesfully

**URL:** <https://discuss.hangfire.io/t/schedule-job-after-parent-job-executes-succesfully/2771>\
**Category:** question\
**Created:** [December 21, 2016, 3:10pm UTC](https://discuss.hangfire.io/t/schedule-job-after-parent-job-executes-succesfully/2771 "2016-12-21T15:10:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bftanase](https://avatars.discourse-cdn.com/v4/letter/b/c67d28/32.png) [@bftanase](https://discuss.hangfire.io/u/bftanase)\
**Post date:** [December 21, 2016, 3:10pm UTC](https://discuss.hangfire.io/t/schedule-job-after-parent-job-executes-succesfully/2771/1 "2016-12-21T15:10:36Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Emyr](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/emyr/32/826_2.png) [@Emyr](https://discuss.hangfire.io/u/Emyr)\
**Post date:** [December 21, 2016, 3:27pm UTC](https://discuss.hangfire.io/t/schedule-job-after-parent-job-executes-succesfully/2771/2 "2016-12-21T15:27:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bftanase](https://avatars.discourse-cdn.com/v4/letter/b/c67d28/32.png) [@bftanase](https://discuss.hangfire.io/u/bftanase)\
**Post date:** [December 21, 2016, 3:33pm UTC](https://discuss.hangfire.io/t/schedule-job-after-parent-job-executes-succesfully/2771/3 "2016-12-21T15:33:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Emyr](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/emyr/32/826_2.png) [@Emyr](https://discuss.hangfire.io/u/Emyr)\
**Post date:** [December 21, 2016, 3:41pm UTC](https://discuss.hangfire.io/t/schedule-job-after-parent-job-executes-succesfully/2771/4 "2016-12-21T15:41:02Z")

</div>

`BackgroundJob.ContinueWith(parentJobId, () => BackgroundJob.Schedule(()=>DoStuff(),`_TimeSpan or DateTimeOffset_`) );`

---

<div class="post-metadata">

**Author:** ![bftanase](https://avatars.discourse-cdn.com/v4/letter/b/c67d28/32.png) [@bftanase](https://discuss.hangfire.io/u/bftanase)\
**Post date:** [December 21, 2016, 3:57pm UTC](https://discuss.hangfire.io/t/schedule-job-after-parent-job-executes-succesfully/2771/5 "2016-12-21T15:57:59Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![bftanase](https://avatars.discourse-cdn.com/v4/letter/b/c67d28/32.png) [@bftanase](https://discuss.hangfire.io/u/bftanase)\
**Post date:** [December 22, 2016, 9:14am UTC](https://discuss.hangfire.io/t/schedule-job-after-parent-job-executes-succesfully/2771/6 "2016-12-22T09:14:44Z")

</div>

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));
```
