为ScheduleAt属性保存的篝火错误日期/时间

问题描述 投票:0回答:1

我有一个简单的方法,在该方法中,我尝试安排任务在特定时间运行一次。这是我的功能(在测试所有类型的工作装置之后,都有很多冗余代码)

    public string Schedule(Guid id, JobType jobType, DateTime scheduleTime)
    {
        //DateTime is paresed from a format like this : "2020-05-19T20:50:00Z";
        return _backgroundJobClient.Schedule(() => task(), scheduleTime);
    }

如果我在Hangfire.State.Data中运行日期为2020-05-19T20:50:00Z的上述示例,则具有:

{"EnqueueAt":"2020-05-19T20:50:00.0000000Z",

"ScheduledAt":"2020-05-19T15:01:16.5174515Z"}

EnqueueAt很好,但是ScheduledAt落后了几个小时。对于数据库,我使用的是Postgres,并且我还在其中设置了时区。

有人知道我在做什么错吗?谢谢

c# multithreading hangfire
1个回答
0
投票

我对时间参数感到困惑。 Schedule没有DateTime参数,您需要为任务指定一个延迟。

空中射击时间表定义:

public static string Schedule([NotNull] this IBackgroundJobClient client, [InstantHandle][NotNull] Expression<Func<Task>> methodCall, TimeSpan delay);

将计划日期转换为TimeSpan对我有用:

private TimeSpan GetDelay(DateTime scheduled)
{
    return scheduled.TimeOfDay - DateTime.Now.TimeOfDay;
}
© www.soinside.com 2019 - 2024. All rights reserved.