Is there any way to add custom fields saying project owner and project id so I can keep track of which jobs have been done for particular project. I am trying to make it so that if you are looking at the project detail page the progress bar will be displayed even you refresh the browser.
Using a IClientFilter
, you can add parameters to the job.
public class HangfireJobDecorator : IClientFilter
{
private readonly IMyService _service;
public HangfireJobDecorator (IMyService service)
{
_service = service;
}
public void OnCreating(CreatingContext filterContext)
{
filterContext.SetJobParameter("MyValue", _service.GetMyValue());
}
public void OnCreated(CreatedContext filterContext) { }
}
You’ll add this filter where you setup hangfire in your application.
GlobalJobFilters.Filters.Add(new HangfireJobDecorator(new MyService))
This is only suitable if you are able to add your wanted parameters in a generic way.
If you need to set parameters when you queue the job, you can use the storage connection:
JobStorage.Current.GetConnection().SetJobParameter(jobId, "foo", someLocalVariable);
The question, though, is if this is really what you want. This will store parameters on the job, that you can get hold of inside the job through the PerformContext
-parameter of your background job method. It might be that you want things the other way around - that instead of storing project details on the job, you want to store background job id in the project…
It that is the case, you should do exactly that: store the jobid that is returned from the Enqueue
-call, and later fetch the status of that job:
JobStorage.Current.GetConnection().GetStateData(jobId)
Thanks. This is what I was looking for. I am going to save the project id in JobParam table