Is there any documentation for job filters? For example how do i know a job completed successfully? OnPerformed is called even if there is an error.
If you want to know if a job completed successfully, you can use the IServerFilter
in combination with the OnPerformed
method. To know if the job was a success, you can check if the PerformedContext
Exception
is null. If the Exception
is present, it means that the job failed the current attempt.
If you want to know if a job has failed all it’s attempt, you can use the IApplyStateFilter
in combination with the OnStateApplied
method. To know if the job failed all it’s attempt, you can check if the ApplyStateContext
NewState
has the type FailedState
.
here’s the documentation
I ended up using a filter like:
public void OnStateApplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
{
if (context.NewState is SucceededState succeededState)
{
this.Ping(monitoredJob.MonitorId + "/complete", null);
}
if (context.NewState is FailedState failedState)
{
var queryParameters = new Dictionary<string, string>();
queryParameters.Add("msg", failedState.Exception.Message);
this.Ping(monitoredJob.MonitorId + "/fail", queryParameters);
}
}
This seems to nicely catch succeeded jobs and failed jobs.
Is there something about the flow that I’m missing?