Recurring job in hangfire is not fired
Asked Answered
S

2

5

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?

Submersed answered 16/5, 2018 at 11:21 Comment(2)
I don't know the library, but isn't the server already disposed before it can handle any job?Stalinsk
Well the server is really for the BackgroundJob that I used for testing. It shouldn't have any effect on the recurring job if I have come to understand the documentation correctly.Submersed
B
10

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/

Bungle answered 24/5, 2018 at 9:32 Comment(1)
Thanks! That did it.Submersed
B
2

When your application starts, hangfire will automatically creates a server and that server is your machine IIS, other things that remove hangfire statrup code from the using block, and below is some configuration for the IIS Server level that you have to implement any where you are using hangfire.

I also had faced same issue and after searching so much in the google for the same issue, I got solution from the below link, in which mentioned some configuration part which is very much important for running your job continuously.

http://docs.hangfire.io/en/latest/deployment-to-production/making-aspnet-app-always-running.html

-----below is the configuration you have to do in global applicationHost.config file (%WINDIR%\System32\inetsrv\config\applicationHost.config).

<applicationPools>
    <add name="MyAppWorkerProcess" managedRuntimeVersion="v4.0" startMode="AlwaysRunning" />
</applicationPools>

<!-- ... -->

<sites>
    <site name="MySite" id="1">
        <application path="/" serviceAutoStartEnabled="true"
                              serviceAutoStartProvider="ApplicationPreload" />
    </site>
</sites>

<!-- Just AFTER closing the `sites` element AND AFTER `webLimits` tag -->
<serviceAutoStartProviders>
    <add name="ApplicationPreload" type="WebApplication1.ApplicationPreload, WebApplication1" />
</serviceAutoStartProviders>
Bonni answered 9/6, 2018 at 5:56 Comment(1)
Thanks for your reference. My hangfire job doesn't run sometimes and when I go to dashboard, it starts to run. I think that's timeout problems.Lashelllasher

© 2022 - 2024 — McMap. All rights reserved.