Reputation: 63
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?
Upvotes: 5
Views: 16540
Reputation: 1297
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.
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/
Upvotes: 10
Reputation: 71
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>
Upvotes: 2