I have a .NET web application which uses Hangfire for some recurring jobs. I want to build the functionality so that the users can change the interval of the jobs through the application. But it seems the 'update' doesn't work.
I have configured the Startup.cs to start the jobs when the application runs:
public void Configuration(IAppBuilder app)
{
app.UseHangfireAspNet(GetHangfireServers);
//Admin user can view job dashboard
app.UseHangfireDashboard("/jobs", new DashboardOptions
{
Authorization = new[] { new JobsAuthorizationFilter() }
});
app.UseHangfireServer();
RecurringJob.AddOrUpdate("my-job-id", () => FileProcess.GetOrders(), interval); //interval is a parameter and its initial values is * * * * * which sets the job to run every minute
}
And now in a controller, this method will be called when users click an update button.
public ActionResult JobUpdate(string newInterval)
{
var manager = new RecurringJobManager();
manager. RecurringJob.AddOrUpdate("my-job-id", () => FileProcess.GetOrders(), newInterval);
return new EmptyResult();
}
But it seems the job doesn't get updated. I've double checked the Hangfire.Hash table, the Cron value of this job doesn't change at all.
I've also tried to remove the jobs and then add it back, also doesn't work
public ActionResult JobUpdate(string newInterval)
{
var manager = new RecurringJobManager();
manager.RemoveIfExists("my-job-id");
manager.AddOrUpdate("my-job-id", () => FileProcess.GetOrders(), newInterval);
manager.Trigger("my-job-id");
}
What have I missed? How can I get the interval to be set and updated by the user?
It turns out the problem that is causing my method not being update is the 'Interval' parameter. Because I use caching to cache this value, the old value only expires after a certain amount of time. That's why it doesn't get updated. I removed caching, and now it all works.