英文:
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("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();
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<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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论