Today, I realized that when EnQueued job is deleted by using API method BackgroundJob.Delete method, it
is removed from Enqueued list but queue length remains the same.
When I check JobQueue table on the database , I can see Deleted jobs. I guess this is the main problem , it should also be removed from JobQueue table when it is deleted by BackgroundJob.Delete.
Funny - I’ve just wondered why there is no foreign key defined on JobQueue.JobId column…
I’d say that introducing such a foreign key with cascading delete (and update?) would prevent this from happening.
Is there a particular reason for not putting a foreign key constraint on the JobId column of table JobQueue?
There are a lot of job queue implementations for Hangfire – SQL Server table, MSMQ, RabbitMQ, Redis, etc. In most of them deleting a background job id is inefficient and requires to iterate through all the queue items. So EnqueuedState.Handler.Unapply method doesn’t contain any logic to delete an item from a queue. Foreign key is also absent to prevent unnecessary checks.
Don’t worry, Worker class will simply skip a deleted background job without any processing.