使用 Hangfire 自定义输入

Custom Input with Hangfire

我是 Hangfire 的新手,正在尝试找出一种方法让用户自定义触发作业的时间。我有一个 Jquery 方法来接收用户的输入并获取值。我正在使用 MVC,所以控制器每分钟都在使用基本的重复作业。

[HttpPost]

public string GetUrlSource(string url){
JobStorage.Current = new SqlServerStorage(ConnectionString);

RecurringJob.AddOrUpdate(()
    => GetUrlSource(url), Cron.Minutely//Trying to change to custom input. There is an input on the view with an id of freqInput and I'm using AJAX call to pull the jquery to the controller and set to a parameter.);
RecurringJob.Trigger("1");

马修,我希望我理解正确你的问题。

Hangfire 可以根据 cron 表达式安排作业。 话虽如此,我建议您在 UI 上添加一个 select/drop,并列出 cron 可能性。将所选项目的 value 传递给控制器​​/hangfire 作业。请参阅下面的代码示例:

cron expression tester

HTML:

<select>
   <option value="0 10 * * * ">Every day at 10</option>
   <option value="0 10 * * 6">Every Saturday at 10</option>
   <option value="0 10 * * 1">Every Monday at 10</option>
</select>

Hangfire:

string cronExp = "0 10 * * *";
RecurringJob.AddOrUpdate(() => Console.WriteLine("Recurring!"),  cronExp);