I use Hangfire for scheduling jobs. In my setup I have defined two distinct queues. One is used to process recurrent jobs and jobs with a 'low' priority. The other queue is used for jobs that have a 'high' priority which are created ad hoc resulting from user input. Non recurrent jobs are scheduled like this to ensure they end up in the correct queue:
var client = new BackgroundJobClient();
var state = new EnqueuedState(queueName);
client.Create(methodCall, state);
All works fine, but now I have a new requirement where I need to delay execution for 'high priority' jobs under certain conditions.
I found this question here on Stack Overflow which mentions the BackgroundJobClient.Schedule operation accepts an enqueueAt parameter:
var client = new BackgroundJobClient();
client.Schedule(methodCall, enqueueAt);
Unfortunately this Schedule operation does not provide me with an option to define the queue. Jobs scheduled using this approach end up in the 'default' queue of Hangfire. Any idea how I can implement the job scheduling delay together with having the ability to determine the queue at runtime?
You can use an argument that defines the queue name for methodCall
. Based on discussion here, you can selectively run the method on queue of your choice. UseQueueFromParameter
should solve the issue you are facing