Place processing in another process

Hangfire is web farm friendly, meaning that if you run multiple applications that use the same queue system (e.g. the same SQL Server database) then they will work together, with each application able to pick up and process scheduled jobs.

In order to achieve what you would like to do, you should be able to create a console app and a web application, ensuring that they both initialize Hangfire using the same storage as per the docs (http://docs.hangfire.io/en/latest/quickstart.html#configuration).

Then, add an additional queue to your console app, lets call it “webjobs” (http://docs.hangfire.io/en/latest/advfeatures.html#multiple-queues-processing). This will mean that both the console app and the web app will have the standard “default” queue, and the console app will have the extra “webjobs” queue. By default, jobs will run in the “default” queue and will be picked up by either the web app or the console app. Jobs added to the “webjobs” queue (by decorating the method with [Queue(“webjobs”)]) will only be able to run in the console app.

Add your jobs in your console app, ensuring that you use the extra string parameter to name them. By setting the cron job to run on February 31st, it should not execute on a scheduled basis.

RecurringJob.AddOrUpdate(“some-id”, () => Console.WriteLine(), “0 0 31 2 *”);

From the web app, trigger the job manually using the job name:

RecurringJob.Trigger(“some-id”);

You do not need to use recurring jobs, but it is important that the console application has access to the job method. Using recurring jobs, you can simply define the jobs in the console app and then trigger them by name. If you are using other job types, such as fire and forget, the job method will need to be available to both the web app and the console app, for this reason, I suggest you put all your jobs in a shared class library.

猜你喜欢

转载自blog.csdn.net/u010178308/article/details/86109056
今日推荐