Hangfire: How to enqueue a job conditionally

▼魔方 西西 提交于 2019-12-12 04:15:51

问题


I am using Hangfire to trigger a database retrieval operation as a background job.

This operation is only supposed to happen once, and can be triggered in multiple ways. (for example, in the UI whenever a user drags and drops a tool, I need to fire that job in the background. But if another tool is dragged and dropped, I don't want to fire the background job as it's already prefetched from the database).

This is what my code looks like now:

var jobId = BackgroundJob.Enqueue<BackgroundModelHelper>( (x) => x.PreFetchBillingByTimePeriods(organizationId) );

What I want is some kind of check before I execute above statement, to find if a background job has already been fired; if yes, then do not fire another and if not, then enqueue this .

for example:

bool prefetchIsFired = false;

// find out if a background job has already been fired. If yes, set prefetchIsFired to true.

if (!prefetchIsFired)
     var jobId = BackgroundJob.Enqueue<BackgroundModelHelper>( (x) => x.PreFetchBillingByTimePeriods(organizationId, null) );

回答1:


You can use a filter (DisableMultipleQueuedItemsFilter) on your job method like here : https://discuss.hangfire.io/t/how-do-i-prevent-creation-of-duplicate-jobs/1222/4



来源:https://stackoverflow.com/questions/41151200/hangfire-how-to-enqueue-a-job-conditionally

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!