We left Russia. We are against aggression and war with Ukraine. It is a tragedy for our nations, it is a nightmare

Linking Hangfire jobs with a host system id

I’m creating a link between each ‘job’ in our host system and the hangfire job enqueued to perform it. This is so I can check if there’s an active hangfire job, so we don’t enqueue another until its Deleted or Succeeded, thus avoiding duplicates.

In the happy path, I save the hangfire job id, but as a fail-safe I’m also assigning a Guid (via the job args) so I can find the job in the edge case where the job is successfully enqueued, but a system failure occurs before the hangfire job id is saved (linked to the host job id). In this case I can find the job in the Hangfire.Job table by searching for the guid in the Arguments field, and assign its job id to the host job again.

This works, but it would be nice if:

  1. There was a specific way to assign a unique id from the host system, to each hangfire job
  2. There was an accompanying api method to lookup a hangfire job using just this host system id

Other approaches I considered:

  1. wrap the enqueue and saving of the hangfire job in a transaction. It wasn’t possible to do this with TransactionScope. “Enlisting in Ambient transactions is not supported”. I had concerns about this approach blocking anyway.
  2. use a job filter to hook into the onCreating or onCreated events.