I am currently working on EmailNotification module in which i have started using hangfire. The only issue is after trying 10 times, if hangfire fails to (schedule job) email in my case, there is not way i can find to get updates regarding that via code.
I know of the fact, i can access hangfire - dashboard by configuring hangfire as below:
public void ConfigureHangfire(IAppBuilder app)
{
var container = AutofacConfig.RegisterBackgroundJobComponents();
var sqlOptions = new SqlServerStorageOptions
{
PrepareSchemaIfNecessary = Config.CreateHangfireSchema
};
Hangfire.GlobalConfiguration.Configuration.UseSqlServerStorage("hangfire", sqlOptions);
Hangfire.GlobalConfiguration.Configuration.UseAutofacActivator(container);
var options = new BackgroundJobServerOptions() {Queues = new[] {"emails"}};
app.UseHangfireDashboard();
app.UseHangfireServer(options);
}
But the issue is i am not able to find a way to access failed job programmatically. I wonder if anyone has come across this issue, would like to know details.
After performing these steps, open your browser and hit the http://<your-app>/hangfire URL to see the Dashboard. By default Hangfire allows access to Dashboard pages only for local requests. In order to give appropriate rights for production use, please see the Configuring Authorization section.
Hangfire leverages a couple of tables and indexes to persist background jobs and other information related to the processing: Some of these tables are used for the core functionality, others fulfill the extensibility needs (making possible to write extensions without changing the underlying schema).
Batch jobs will help to build more complex workflows with Hangfire. They will give you the power of parallel processing and continuations, you can look at this code snippet: BatchJob . Create(x => { x.
You can use Hangfire Job Filters for this. Job filter allows you to extend functionality of hangfire, and you can do many interesing things with them (See the official documentation here for more details)
Create a class that extends from JobFilterAttribute
And then implement IElectStateFilter
interface. This interface provides you with a method, OnStateElection
which is called when the current state of the job is being changed to the specified candidate state, say FailedState
.
public class MyCustomFilter : JobFilterAttribute, IElectStateFilter
{
public void IElectStateFilter.OnStateElection(ElectStateContext context)
{
var failedState = context.CandidateState as FailedState;
if (failedState != null)
{
//Job has failed
//Job ID => context.BackgroundJob.Id,
//Exception => failedState.Exception
}
}
}
And then, register this attribute -
GlobalJobFilters.Filters.Add(new MyCustomFiler());
If you need to capture event, after the state is applied, you can implement IApplyStateFilter
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With