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:
- There was a specific way to assign a unique id from the host system, to each hangfire job
- There was an accompanying api method to lookup a hangfire job using just this host system id
Other approaches I considered:
- 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.
- use a job filter to hook into the onCreating or onCreated events.