Hangfire的作业不在一天的特定时间运行。

huangapple go评论73阅读模式
英文:

Hangfire jobs don't run at time of day

问题

我成功地使用不同分钟间隔使Hangfire(https://www.hangfire.io/)工作。(初始作业的定时似乎有点随机,但之后运行得很好。)

然而,我的按时间触发的作业根本没有触发。

using var server = new BackgroundJobServer();

Console.WriteLine("Hangfire Server started. Press any key to exit...");
Console.WriteLine($"Started: {DateTime.Now:g}");
Console.WriteLine();

RecurringJob.AddOrUpdate<TaskExecutor>("X", x => x.RunTask("One Time 1"), "0 10 8 8 */1");
RecurringJob.AddOrUpdate<TaskExecutor>("Z", x => x.RunTask("One Time 2"), "0 10 * * * ");

Console.ReadKey();

我原本期望这两个任务在今天上午10点运行(2023年8月8日)。但是两个任务都没有运行。

有人知道我漏掉了什么吗?

注意:我没有显示TaskExecutor.RunTask()方法。但我确信它是正确的,因为对于不同的分钟间隔它运行得很好。

英文:

I got Hangfire working with various-minute intervals. (The timing of the initial job seems a bit random, but then works great thereafter.)

However, my time-of-day jobs are not firing at all.

using var server = new BackgroundJobServer();

Console.WriteLine(&quot;Hangfire Server started. Press any key to exit...&quot;);
Console.WriteLine($&quot;Started: {DateTime.Now:g}&quot;);
Console.WriteLine();

RecurringJob.AddOrUpdate&lt;TaskExecutor&gt;(&quot;X&quot;, x =&gt; x.RunTask(&quot;One Time 1&quot;), &quot;0 10 8 8 */1&quot;);
RecurringJob.AddOrUpdate&lt;TaskExecutor&gt;(&quot;Z&quot;, x =&gt; x.RunTask(&quot;One Time 2&quot;), &quot;0 10 * * * &quot;);

Console.ReadKey();

I was expecting both tasks to run at 10:00 AM today (8/8/2023). But neither one runs.

Does anyone know what I'm missing?

Note: My TaskExecutor.RunTask() method is not shown. But I'm sure it is right because it worked fine for various minute intervals.

答案1

得分: 0

原来Hangfire默认使用的是UTC时间。通过使用本地时间,代码可以按预期工作。

RecurringJobOptions jobOptions = new()
{
    TimeZone = TimeZoneInfo.Local
};

RecurringJob.AddOrUpdate<TaskExecutor>("X", x => x.RunTask("One Time 1"), "0 10 8 8 */1", jobOptions);
RecurringJob.AddOrUpdate<TaskExecutor>("Z", x => x.RunTask("One Time 2"), "0 10 * * *", jobOptions);
英文:

Turns out Hangfire uses UTC time by default. The code works as expected by using local time.

RecurringJobOptions jobOptions = new()
{
    TimeZone = TimeZoneInfo.Local
};

RecurringJob.AddOrUpdate&lt;TaskExecutor&gt;(&quot;X&quot;, x =&gt; x.RunTask(&quot;One Time 1&quot;), &quot;0 10 8 8 */1&quot;, jobOptions);
RecurringJob.AddOrUpdate&lt;TaskExecutor&gt;(&quot;Z&quot;, x =&gt; x.RunTask(&quot;One Time 2&quot;), &quot;0 10 * * * &quot;, jobOptions);

huangapple
  • 本文由 发表于 2023年8月9日 00:06:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76861361.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定