asp.net-corehangfire

Hangfire enqueued job not running


I have a job that is getting enqueued, but not executing.

BackgroundJob.Enqueue<MasterDataMessageConsumerJob>(nameof(MasterDataMessageConsumerJob).ToLower(), x => x.ExecuteAsync(CancellationToken.None));

I can see this job queued in the dashboard, but it never runs.

However if I use the same code via RecurringJob.AddOrUpdate the job is added and executed just fine for example;

RecurringJob.AddOrUpdate<MasterDataMessageConsumerJob>(nameof(MasterDataMessageConsumerJob), x  => x.ExecuteAsync(CancellationToken.None), Cron.Minutely())

This is in ASP.NET Core and I've implemented the server as follows directly in program.cs

        builder.Services.AddHangfire(configuration => configuration
            .UseSimpleAssemblyNameTypeSerializer()
            .UseRecommendedSerializerSettings()
            .UseSqlServerStorage(builder.Configuration.GetConnectionString("MyDbServer")));

        // Add the processing server as IHostedService
        builder.Services.AddHangfireServer();

        // configure the DI for generic data and master data services
        ConfigureDIServices(builder.Services);

        var app = builder.Build();

        app.UseHangfireDashboard("/dashboard");

        // run the master data message consumer hourly, which will internally enqueue the continuation jobs
        BackgroundJob.Enqueue<MasterDataMessageConsumerJob>(nameof(MasterDataMessageConsumerJob).ToLower() + "2",
                                                            x => x.ExecuteAsync(CancellationToken.None));

Can anyone see what I have missed?


Update

I also have the same problem when calling into a static Console.Writeline. It is queued but not run, so this gives me more confidence that I must have set something up wrong.

BackgroundJob.Enqueue("masterdatamessageconsumerjob42 ", () => Console.WriteLine("Write something here"));

example of a queued job that is not executed


Solution

  • I found an answer that worked for me - remove the "QueueName" parameter nameof(MasterDataMessageConsumerJob).ToLower() + "2" from the Enqueue method.

    BackgroundJob.Enqueue<MasterDataMessageConsumerJob>(x => x.ExecuteAsync(CancellationToken.None));
    

    Perhaps this is a pro feature? I couldn't find anything in the docs about it.