HangfireIO / HangfireIO/Hangfire

Is there any way to manually fail a job dynamically based on some outcome in the Job's Code.

Open
#1,669 4 comments 0 reactions 0 assignees View on GitHub
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.

![image](https://user-images.githubusercontent.com/4049421/81551713-efc57600-939f-11ea-9e54-672ba3a20511.png)

Is there any other way?

Contributor guide

Open the contributing 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.