简体   繁体   中英

Hangfire jobs (with dependencies) scheduling

I am using Hangfire to execute a number of jobs that need to run scheduled, in a certain order.

Can not use CRON, as I do not know how long each will take.

This is what I need to achieve, as efficient as possible: A and B start at the same time, C starts after B completed and D starts after all completed

A ===>        |
              |
B ======>|    |
C        |===>|
              |
D             |===>



var Aid= BackgroundJob.Enqueue(() => A());
var Bid= BackgroundJob.Enqueue(() => B());

var Cid= BackgroundJob.Enqueue(Bid, () => C());

How could I start the job D after A and C are finished?

Are there other alternatives than BackgroundJobs? I started with RecurringJobs but found out they can not have dependencies to one-another. Custom solutions/ workarounds might be helpful as well.

not sure if these suggestions will work, but I would recommend looking into using BatchJobs and find a way to nest them correctly.

The other way would be to use a method of your own that would check the status of the parent job before executing. If the parent job is not done yet, requeue the method for execution after 30 seconds or something.

To Check Job State:

IStorageConnection connection = JobStorage.Current.GetConnection();
JobData jobData = connection.GetJobData(jobId);
string stateName = jobData.State;

In your methods, you would add another variable that passes in the parent ID or List of Parent's IDs. This way you can check if all or any parents have succeeded before you execute. if any of the Parents ID shows not Failed or Succeeded, you would requeue that same job.

var Aid= BackgroundJob.Enqueue(() => A(new List<int> {}));
var Bid= BackgroundJob.Enqueue(() => B(new List<int> {}));

var Cid= BackgroundJob.Enqueue(() => C(new List<int> {Bid}));
var Did = BackgroundJob.Enqueue(() => D(new List<int> {Aid, Cid}));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM