HangfireIO / HangfireIO/Hangfire

Feature Request: Provide a means of directly cancelling a running job (by id) without having to delete it

Open
#2,076 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
10.1k
Forks
1.8k
Avg merge
1h 19m
Merged PRs (30d)
1

Description

I've been writing some integration tests and when it came time to explore how to recreate/mimic the cancellation support, I wasn't finding a way to simply cancel a single specific job that is running. In my case I'm working with recurring Jobs and I would have the JobId available to me now that TriggerExecution(jobName) returns it and we've aligned to use that method now.

On researching the issues here, it seems like the only option is (a) delete the job and (b) wait for the background monitoring process to note that it was deleted and then that would trigger the cancellation token.

In my case, we'd really like to not have to delete the job as that's not our real intent - we just want to simply cancel it. When we trigger that cancellation, it would ideally be nearly instant - without some long waiting period in between. I'm not sure if there's a way to configure how long that background process waits between its polling/checks so perhaps there's a way to address that as when we're running our tests we're using the in-memory storage provider so frequent polling wouldn't be an issue.

I'm also a bit unclear of the 'official' or 'right' way to find the Job/Delete it, etc. I need to go back & re-read the various related posts I've browsed and see which one feels like the correct approach.

What would be really handy is just a simple method for cancellation like we have for other similar needs. i.e. bool BackgroundJobs.Cancel(jobId) or something similar. Perhaps a boolean result would be insufficient to reflect the potential outcomes but the key thing I'm looking for is that it returns a result value that indicates if the job was indeed cancelled (or other potential outcomes that may occur - i.e. if the job already completed before it could be cancelled, job did not exist, etc.)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the TriggerExecution(jobName) flow and the existing delete-triggered cancellation behavior described in the issue. Define the cancellation entry point by job ID and its result states, including completed, missing, and successfully cancelled jobs; done should provide direct cancellation without deleting the job or waiting for background polling.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend, distributed-systems
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.