Hangfire not running tasks although they are in database
Asked Answered
M

3

8

I'm not sure what's going on with Hangfire but calling BackgroundJob.Enqueue() adds a job to the database as "Scheduled" but nothing seems to happen. The method code does not seem to run. In the debugger no break points get hit and in my logs from inside job nothing gets printed.

 JobStorage.Current = new SqlServerStorage("DefaultConnection");
Malindamalinde answered 22/10, 2014 at 6:16 Comment(0)
G
6

I had a similar issue. In my case the problem was that my dependency injection wasn't properly wired up.

Only after I went to Hangfire Dashboard -> Scheduled Jobs and clicked on my job to see more details, I could see the exact exception messages i.e. "Couldn't create interface 'some_interface'".

Check if your jobs are initialised properly and that there are no errors in the 'Scheduled' part of Hangfire Dashboard.

Glaciate answered 24/10, 2014 at 14:46 Comment(4)
What were you using for DI? We're running into the same problem w/ StructureMap and wondering how you went about fixing it.Alroi
I was using Ninject. I never solved the underlying problem, but slightly changed the structure of my application. I now use hangfire to call jobs within WCF service where I've got my DI wired up.Glaciate
This helped me find my issue as well, which was a database error (string would be truncated). Not really sure why the job wouldnt go into the 'failed' bucket.Pock
It's probably because by default Hangfire re-shedules the jobs to be executed again. Only after they fail certain number of times (10, I think), they move to failed bucket.Glaciate
J
0

Good day to you. It may possible that the Hangfire server is not running due to that schedule recurring job may not running

To setup the server, please follow the following steps:

To setup the Hangfire server you need to implement HangfireBootstrapper class by implementing the IRegisteredObject interface

After the implementation of the class you need to start and stop the Hangfire server in global.asax or startup file in the application.

For a more detailed implementation please refer this link

Jagannath answered 2/8, 2018 at 14:1 Comment(2)
Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. See here for instructions how to write better "link-based" answers. Thanks!Schmid
Thanks Thom, you did wonderful editing, your editing is appreciatedJagannath
M
0

Yhe link provided by Ashish wasn't giving me enough information. I had to call both the AddHangfire() and the AddHangfireServer(). the builder is the WebApplicationBuilder.

When configuring services:

builder.Services.AddHangfire(config => config
    .UseSimpleAssemblyNameTypeSerializer()
    .UseRecommendedSerializerSettings()
    .UseSqlServerStorage(builder.Configuration.GetConnectionString("Default"))
);
builder.Services.AddHangfireServer();

In the Hangfire dashboard, you can see if a task failed. For me it was missing an HttpClient injection, which I fixed with:

builder.Services.AddHttpClient();
Midget answered 24/5 at 13:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.