Best Way to Always Track A Job, Even on Failure

Hello, I am just start to use Hangfire and I’ve run into an issue where I need some advice. I am new developer at my company and they have been using Hangfire for background jobs and processing large queries and file parsing.

Problem #1: In order to improve our reporting and analysis of jobs and errors, I am trying to associate a job id to a “scenario” or basically a workflow we do internally. Initially, I thought I could use a Continuation task, but it appears when a parent job FAILS, the task is never run. This is a problem, since when a job is invoked, I need to map the job id and scenario id to a secondary table. My initial attempt at this was fairly straight forward -> invoke the job, get the job id and then run a child task after, which just uses the entity framework to create a mapping record:

public void LinkBackgroundJobToInstance(string jobId, int id)
        BackgroundJob.ContinueWith(jobId, () => _context.Database.ExecuteSqlCommand(MapSql, jobId, id));

    public void Process(Expression<Action<ScenarioBusinessLogic>> scenarioMethod,  int id)
        var jobId = BackgroundJob.Enqueue(scenarioMethod);
        LinkBackgroundJobToInstance(jobId, id);

This works fine, until a job fails. The other option I see is to maybe use a JobFilter. However, I’m not sure of how I can inject my DbContext into this filter and write a new database record. I would also have to pass in my scenario id and I’m not sure how that works, can I pass a dictionary or “auxiliary” data into an action before it runs?

The final option I see is to use the PerformContext parameter. Unfortunately, the developer before me created A LOT of methods and didn’t really use SOLID or DRY principals, so I would be going back to retrofit 20-25 methods with PerformContext parameters added to method names and having them all invoke another method I create to insert my database mapping.

I was really hoping for a clean, easy way to do this and it could be accomplished if ContinueWith fired after a parent job fails. Since this is not the case, I’m hoping someone could help me determine the best way to do this.

Thanks in advance.