HangfireIO / HangfireIO/Hangfire
Is there any way to manually fail a job dynamically based on some outcome in the Job's Code.
- Dominant language
- C#
- Stars
- 10.1k
- Forks
- 1.8k
- Avg merge
- 1h 19m
- Merged PRs (30d)
- 1
Description
**Is there any way to manually fail a job dynamically based on some outcome in the Job's Code.**
Suppose a Background Job is to send a mail.
````csharp
[Queue("MailQueue")]
[DisplayName("Send mail to {0}")]
public static bool SendMail(string to, string body, PerformContext performContext)
{
var mailServer = new MailServer();
int errorCode = mailServer.Send(to, body);
if(errorCode == 1)
{
//Error code 1 means "To" address is expired. Now it doesn't make sense to retry
//TODO: Some way to manually fail this background job, so that it never re-triggers further.
return false;
}
else
{
return true;
}
````
Tried this:
````csharp
new BackgroundJobClient().ChangeState(performContext.BackgroundJob.Id, new Hangfire.States.FailedState(new ArgumentException($"No webhook registered for Keyword {keyword}")));
````
This results in adding a `Failed` state, but it gets scheduled again and re-triggers.

Is there any other way?
Contributor guide
Research direction
Start by reproducing the behavior with BackgroundJobClient.ChangeState and FailedState in the example. Trace how a failed state is scheduled again and identify the supported way to prevent retries for a job outcome such as an expired recipient. Done means the job is marked as failed without being re-triggered.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100