Click or drag to resize
IState Interface
Provides the essential members for describing a background job state.

Namespace: Hangfire.States
Assembly: Hangfire.Core (in Hangfire.Core.dll) Version: 1.5.0.0
Syntax
public interface IState

The IState type exposes the following members.

Properties
  NameDescription
Public propertyIgnoreJobLoadException
Gets whether transition to this state should ignore job de-serialization exceptions.
Public propertyIsFinal
Gets if the current state is a final one.
Public propertyName
Gets the unique name of the state.
Public propertyReason
Gets the human-readable reason of a state transition.
Top
Methods
  NameDescription
Public methodSerializeData
Gets a serialized representation of the current state.
Top
Remarks

Background job processing in Hangfire is all about moving a background job from one state to another. States are used to clearly decide what to do with a background job. For example, EnqueuedState tells Hangfire that a job should be processed by a Worker, and FailedState tells Hangfire that a job should be investigated by a developer.

Each state have some essential properties like Name, IsFinal and a custom ones that are exposed through the SerializeData method. Serialized data may be used during the processing stage.

Hangfire allows you to define custom states to extend the processing pipeline. IStateHandler interface implementation can be used to define additional work for a state transition, and IBackgroundProcess interface implementation can be used to process background jobs in a new state. For example, delayed jobs and their ScheduledState, continuations and their AwaitingState can be simply moved to an extension package.

Examples

Let's create a new state. Consider you haves background jobs that throw a transient exception from time to time, and you want to simply ignore those exceptions. By default, Hangfire will move a job that throwed an exception to the FailedState, however a job in the failed state will live in a Failed jobs page forever, unless we use AutomaticRetryAttribute, delete or retry it manually, because the FailedState is not a final state.

Our new state will look like a FailedState, but we define the state as a final one, letting Hangfire to expire faulted jobs. Please refer to the IState interface properties to learn about their details.

In articles related to IStateHandler and IElectStateFilter interfaces we'll discuss how to use this new state.

C#
public class FaultedState : IState
{
    public static readonly string StateName = "Faulted";

    public FaultedState(Exception exception)
    {
        Message = exception.Message;
        Details = exception.ToString();
    }

    public string Name => StateName;
    public string Reason { get; set; }

    public bool IsFinal => true;
    public bool IgnoreJobLoadException => true;

    // Here are our custom properties, we'll made them
    // accessible through the Dashboard UI.
    public string Message { get; }
    public string Details { get; }

    public Dictionary<string, string> SerializeData()
    {
        return new Dictionary<string, string>
        {
            { "Message", Message },
            { "Details", Details }
        };
    }
}
See Also