hangfire 中的重复工作未被解雇
Recurring job in hangfire is not fired
无论我输入什么时间表,我在 Hangfire 中的重复性工作都没有被触发。我尝试使用 BackgroundJob
只是为了确保某些东西在工作,而且确实如此。我还检查了数据库,"hash" table 已正确填充计划作业。
这是我正在使用的代码:
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));
}
}
那么我做错了什么?
如果您查看 Hangfire 仪表板,它会告诉您有多少 BackgroundJobServers 正在 运行ning。如果 BackgroundJobServers 运行ning 为零,则 Hangfire 将在其中一个启动之前不执行任何操作。
由于您在 using
中使用 BackgroundJobServer
,它会在创建周期性作业后处理。 BackgroundJobServer
必须在触发重复作业时处于活动状态,否则不会创建任何作业。您可以在程序开始时启动 BackgroundJobServer
,并在关闭程序时处理它。或者您可以 运行 BackgroundJobServer
在一个单独的 windows 服务应用程序 中,这样它总是 运行ning 在后台,而机器已开启。
要了解有关设置后台服务器的更多信息,请参阅:http://docs.hangfire.io/en/latest/background-processing/
当您的应用程序启动时,hangfire 会自动创建一个服务器,该服务器就是您的机器 IIS,其他从 using 块中删除 hangfire statrup 代码的东西,下面是您必须进行的 IIS 服务器级别的一些配置在您使用 hangfire 的任何地方实施。
我也遇到过同样的问题,在 google 中搜索了很多同样的问题后,我从下面 link 得到了解决方案,其中提到了一些非常多的配置部分对 运行 你的工作很重要。
http://docs.hangfire.io/en/latest/deployment-to-production/making-aspnet-app-always-running.html
-----以下是您必须在全局 applicationHost.config 文件 (%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>
无论我输入什么时间表,我在 Hangfire 中的重复性工作都没有被触发。我尝试使用 BackgroundJob
只是为了确保某些东西在工作,而且确实如此。我还检查了数据库,"hash" table 已正确填充计划作业。
这是我正在使用的代码:
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));
}
}
那么我做错了什么?
如果您查看 Hangfire 仪表板,它会告诉您有多少 BackgroundJobServers 正在 运行ning。如果 BackgroundJobServers 运行ning 为零,则 Hangfire 将在其中一个启动之前不执行任何操作。
由于您在 using
中使用 BackgroundJobServer
,它会在创建周期性作业后处理。 BackgroundJobServer
必须在触发重复作业时处于活动状态,否则不会创建任何作业。您可以在程序开始时启动 BackgroundJobServer
,并在关闭程序时处理它。或者您可以 运行 BackgroundJobServer
在一个单独的 windows 服务应用程序 中,这样它总是 运行ning 在后台,而机器已开启。
要了解有关设置后台服务器的更多信息,请参阅:http://docs.hangfire.io/en/latest/background-processing/
当您的应用程序启动时,hangfire 会自动创建一个服务器,该服务器就是您的机器 IIS,其他从 using 块中删除 hangfire statrup 代码的东西,下面是您必须进行的 IIS 服务器级别的一些配置在您使用 hangfire 的任何地方实施。
我也遇到过同样的问题,在 google 中搜索了很多同样的问题后,我从下面 link 得到了解决方案,其中提到了一些非常多的配置部分对 运行 你的工作很重要。
http://docs.hangfire.io/en/latest/deployment-to-production/making-aspnet-app-always-running.html
-----以下是您必须在全局 applicationHost.config 文件 (%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>