Search code examples
c#hangfire

Recurring job in hangfire is not fired


My recurring job in Hangfire is not being triggered regardless of what Schedule I put in. I have tried using BackgroundJob just to make sure that something is working, and it does. I have also checked the database and the "hash" table is being populated correctly with the scheduled jobs.

Here is the code I am working with:

try
{
    using(var server = new BackgroundJobServer(serverOptions,storage))
    {
        Log("Hangfire server started");
        RecurringJob.AddOrUpdate("Mail", () =>
                 _notificationHelper.SendEmail(result)
                 , Cron.MinuteInterval(1), TimeZoneInfo.Local
             );

        //BackgroundJob.Enqueue(() => _notificationHelper.SendEmail(result));
    } 
}

So what am I doing wrong here?


Solution

  • If you look at the Hangfire Dashboard it will tell you how many BackgroundJobServers are running. If there are zero BackgroundJobServers running, then Hangfire will do nothing until one is started.

    enter image description here

    Since you are using the BackgroundJobServer in a using, it is disposed after the recurring job is created. The BackgroundJobServer has to be active when the recurring job is triggered, or else no job is created. You can start the BackgroundJobServer at the start of your program, and dispose it when you close the program. Or you can run the BackgroundJobServer in a separate windows service application so that it is always running in the background while the machine is turned on.

    To learn more about setting up the background server, see: http://docs.hangfire.io/en/latest/background-processing/