How to cancel current execution of Recurring Job?


#1

Hi, I’m trying to find a way to cancel the current execution of a recurring job.

I’ve already tested BackgroundJob.Delete() and this correctly throws a JobAbortedException within a job that is currently executing as long as it invokes ThrowIfCancellationRequested() on its cancellation token.

However, calling RecurringJob.RemoveIfExists() only deletes the recurring job definition, and doesn’t cancel any running background jobs that were created from the recurring job definition.

Basically I need a way to be able to lookup any background jobs that are associated with a recurring job, and manually cancel them using the Delete() method. However, I don’t know how to do this or if it is even possible.

Can anyone provide any help please?


#2

I just found the solution, which I’ll include here for posterity:

List<RecurringJobDto> list;
using (var connection = JobStorage.Current.GetConnection())
{
    list = connection.GetRecurringJobs();
}

var job = list?.FirstOrDefault(j => j.Id == jobId);  // jobId is the recurring job ID, whatever that is
if (job != null && !string.IsNullOrEmpty(job.LastJobId))
{
    BackgroundJob.Delete(job.LastJobId);
}

#3

Can you help me on how to delete All Current Execution(s) of a specific recurring job … not just the latest ?

Thank you,
Fady Naguib


#4

I have Create one Recuring job

RecurringJob.AddOrUpdate("abc", () => Process(), Cron.MinuteInterval(1));
is working good but this job not stop on this code

List<RecurringJobDto> list;
            using (var connection = JobStorage.Current.GetConnection())
            {
                list = connection.GetRecurringJobs();
            }
            var job = list?.FirstOrDefault(j => j.Id == "abc");  // jobId is the recurring job ID, whatever that is
            if (job != null && !string.IsNullOrEmpty(job.LastJobId))
            {
                BackgroundJob.Delete(job.LastJobId);
                RecurringJob.RemoveIfExists(job.LastJobId);
            }

Please help me how to stop or remove this job