Created
September 22, 2017 16:13
-
-
Save hidegh/36bbae8868e5cf5b37aea70cfa37ed5c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Depends on domain events... | |
/// Reason: wanted to de-couple it from the next job and so I do have DI objects accessible... | |
/// | |
/// NOTE: | |
/// This is by default a PERMANENT failure notification filter, since the filter Order is so high, it get's surely executed after the AutomaticRetry filter. | |
/// Also if AutomaticRetry filter does delete the job on permanent fail, the PERMANENT failure notification won't occure. | |
/// | |
/// If the HangfirePermanentFailureNotificationEvent.Order is less than the AutomaticRetry.Order, we will get a notification on each failure, not just the last one! | |
/// </summary> | |
public class HangfirePermanentFailureNofitifactionAttribute : JobFilterAttribute, IElectStateFilter | |
{ | |
public HangfirePermanentFailureNofitifactionAttribute() | |
{ | |
Order = int.MaxValue; | |
} | |
public void OnStateElection(ElectStateContext context) | |
{ | |
var failedState = context.CandidateState as FailedState; | |
if (failedState == null) | |
{ | |
// This filter accepts only failed job state. | |
return; | |
} | |
var retryAttempt = context.GetJobParameter<int>("RetryCount") + 1; | |
DomainEvents.Raise(new HangfirePermanentFailureNotificationEvent( | |
jobId: context.BackgroundJob?.Id, | |
jobCreatedAt: context.BackgroundJob?.CreatedAt, | |
jobMethod: context.BackgroundJob?.Job?.Method, | |
jobArgs: context.BackgroundJob?.Job?.Args, | |
failedAt: failedState.FailedAt, | |
stateName: failedState.Name, | |
retryAttempt: retryAttempt, | |
isFinal: failedState.IsFinal, | |
reason: failedState.Reason, | |
exception: failedState.Exception | |
)); | |
} | |
} |
Author
hidegh
commented
Sep 15, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment