How to get the Job Id of a recurring Job

recurring
Tags: #<Tag:0x00007f64bb2692c0>

#1

I am trying to create a recurring job using Hangfire. I need to know how to get the Job Id of a recurring job created which I can use to remove the recurring job when my task is achieved.
I would want to use the RecurringJob.RemoveIfExists method for which I need to provide the JobId.
Can anyone please help me on this?


#2

For recurring jobs, a user himself is responsible for id generation, please see the docs to learn how to specify an identirifer. However, the following sentense looks weird:

I need to know how to get the Job Id of a recurring job created which I can use to remove the recurring job when my task is achieved.

Maybe it is better to use fire-and-forget jobs or delayed jobs in your case?


#3

Here’s how I did it:

var jobName = SomeMethodToGetYourJobName();
var con = Hangfire.JobStorage.Current.GetConnection();
var recJob = con.GetAllEntriesFromHash($"recurring-job:{jobName}");

recJob then has these keys: CreatedAt, Cron, Job, NextExecution, Queue, TimeZoneId


#4

I used the following code in a FilterAttribute to get the recurringJobId.

filterContext.Connection.GetJobParameter(filterContext.BackgroundJob.Id, “RecurringJobId”)

I needed to make a id-specific disableconcurrentexecution attribute