Background Jobs In the same queue being executed in the same time?


#1

I have tasks that have to be run automatically one by one by calling webapi interface. However , when I use below code to add multiple jobs in the same queue in a very short interval and then check them out in the dashboard , I check every job in the same queue,the status are the same which is processing ,doesn’t work one by one as expected . And after a while ,the status changed to succeed .However ,all my tasks doesn’t completed noramally. Can anyone give me some advice ?

BackgroundJob.Enqueue(() => MyTask(inputParm));

public class Startup
{
public void Configuration(IAppBuilder app)
{

        GlobalConfiguration.Configuration.UseSqlServerStorage("HangFireConnectionStr");

        var Dashboardoptions = new DashboardOptions
        {
             Authorization = new[] { new hangfireAuthorization() }
        };
        app.UseHangfireDashboard("/hangfire", Dashboardoptions);
       
        var Options=   new BackgroundJobServerOptions
        {
            Queues = new[] { "cnv","snv","mito", "recurring", "default" }, 
            WorkerCount = Environment.ProcessorCount * 2 
        };

        app.UseHangfireServer(Options);

#2

Hangfire works using multiple worker (as you can see on this line) :

WorkerCount = Environment.ProcessorCount * 2

That means that your jobs won’t get run one after the other, but will run in parallel.

If you want to run them one by one, here’s two solution i can come up on top of my head :

  1. Make the worker count 1 (That way only 1 job is run at a time. BUT i’m not so sure that they will run in the order that they were queued in. Maybe you could ask another question on the topic or search online.)
  2. At the end of the first job, enqueue the next one (best thing to do in my opinion. The only issue with this one is that your jobs become tighly coupled to one another)