如何以编程方式立即执行 ReccurringJob?

use*_*608 0 hangfire

我有很多 Hangfire 经常性工作,效果很好。

但是,我需要添加功能以允许用户抢占计划并立即运行任务。

这是我的启动:

RecurringJob.AddOrUpdate<SendFilesJob>("SendFilesJob", j => j.Execute(), send_schedule);
Run Code Online (Sandbox Code Playgroud)

我告诉用户作业最后一次运行的时间以及下次运行的时间,如下所示:

using (var connection = JobStorage.Current.GetConnection())
{
    foreach (var recurringJob in connection.GetRecurringJobs())
    {
        //report LastExecution, NextExecution, LastJobState
    }
}
Run Code Online (Sandbox Code Playgroud)

这也显示了当前正在通过 处理作业的时间LastJobState,这很棒。

但是,当用户抢占立即运行任务时,我这样调用它:

BackgroundJob.Enqueue<SendFilesJob>(j => j.Execute());
Run Code Online (Sandbox Code Playgroud)

这可行,但它不会与定期的工作录入报告挂钩;LastJobState因此用户看不到或值的变化LastExecution

有没有办法访问它RecurringJob本身并触发它,以便它更新这些值?

ape*_*kov 6

您可以尝试使用RecurringJob.Trigger("job-id");