Job Handler

Each schedule must relate to an implementation of the IJobHandler interface. As a result the handler will be invoked according to its schedule.

Note

A job handler must analyze all possible situation. For example, when the handler was invoked but previous handling has not finished yet; there are misfire tasks because the application was stopped for a while; the handler was invoked early than it was planned; the handler was not invoked at the exact time, and so forth.

Registering Job Handlers

Job handlers are created and managed by IoC container so the handlers must be registered:

builder.RegisterType<MyJobHandler>().AsSelf().As<IJobHandler>().SingleInstance();

To register all handlers of an assembly use the RegisterJobHandlers() helper:

builder.RegisterJobHandlers(assembly);

Job Handler Context

When a job handler is invoked it gets an information to process task. This information is called the job handler context and presented as the IJobHandlerContext interface. The IJobHandlerContext interface has next properties.

InstanceId:The unique job instance identifier. Formed automatically with using IJobInfo. Each instance is handled only once by some cluster node.
FireTimeUtc:The actual time the trigger fired. For instance the scheduled time may have been 10:00:00 but the actual fire time may have been 10:00:03 if the scheduler was too busy.
ScheduledFireTimeUtc:
 The scheduled time the trigger fired for. For instance the scheduled time may have been 10:00:00 but the actual fire time may have been 10:00:03 if the scheduler was too busy.
PreviousFireTimeUtc:
 Gets the previous fire time or null if the handler was invoked the first time.
NextFireTimeUtc:
 Gets the next fire time ot null if the handler will not be invoked anymore.
Data:The job data which is defined by the job info.

Job Handler Example

To define a job handler you need to implement the IJobHandler interface.

class MyJobHandler : IJobHandler
{
    public async Task Handle(IJobInfo jobInfo, IJobHandlerContext context)
    {
        await Console.Out.WriteLineAsync($"Greetings from {nameof(MyJobHandler)}!");
    }
}