Table of Contents

Class QuartzHostedService

Namespace
Quartz
Assembly
Quartz.dll

Runs the schedulers registered in the container for as long as the application runs.

public class QuartzHostedService : IHostedLifecycleService, IHostedService
Inheritance
QuartzHostedService
Implements
Inherited Members

Remarks

Every scheduler in the container is started, the default one and each named one, and each is configured by QuartzHostedServiceOptions under its own name — so one scheduler can wait for application startup while another starts immediately, and one whose AutoStart is false is created and bound but left for the application to start.

The schedulers are resolved when the host starts rather than when the service is registered, so AddQuartzHostedService and AddQuartz can be called in either order. Registering the hosted service first used to leave the default scheduler unstarted and say nothing about it.

Deriving from this and registering the derived type with AddQuartzHostedService<T> is a supported extension point, but only through the four lifecycle hooks — StartingAsync(CancellationToken), StartedAsync(CancellationToken), StoppingAsync(CancellationToken) and StoppedAsync(CancellationToken) — which exist for nothing else. StartAsync(CancellationToken) and StopAsync(CancellationToken) are not overridable: they maintain state a subclass cannot see, and an override that did not call base left the schedulers bound to the repository with nothing left to shut them down. What a subclass would have overridden them for is reading the running schedulers, and that is Schedulers.

Constructors

QuartzHostedService(IHostApplicationLifetime, IServiceProvider, IOptionsMonitor<QuartzHostedServiceOptions>)

Constructed by the container; an application registers this service with AddQuartzHostedService rather than building one.

public QuartzHostedService(IHostApplicationLifetime applicationLifetime, IServiceProvider serviceProvider, IOptionsMonitor<QuartzHostedServiceOptions> options)

Parameters

applicationLifetime IHostApplicationLifetime

The host's lifetime, which AwaitApplicationStarted waits on.

serviceProvider IServiceProvider

The container the schedulers are resolved from.

options IOptionsMonitor<QuartzHostedServiceOptions>

One QuartzHostedServiceOptions per scheduler name.

Properties

Schedulers

The schedulers this service is running, from the moment they are resolved until they are shut down.

protected IReadOnlyList<IScheduler> Schedulers { get; }

Property Value

IReadOnlyList<IScheduler>

Remarks

A snapshot rather than a live view, so a hook cannot be handed a list that empties underneath it. Empty before StartAsync(CancellationToken) has resolved them and after StopAsync(CancellationToken) has shut them down, which makes StartedAsync(CancellationToken) and StoppingAsync(CancellationToken) the two hooks it is worth reading from.

Methods

StartAsync(CancellationToken)

Resolves every scheduler the container holds, binds it to the repository and starts it as its options say.

public Task StartAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

The host's start token.

Returns

Task

Remarks

Not overridable: it maintains state a subclass cannot see, and an override that did not call base left the schedulers bound with nothing to shut them down. The four hooks are the extension point.

StartedAsync(CancellationToken)

Runs once every scheduler has started. Does nothing; override it to do something — Schedulers is what it is for.

public virtual Task StartedAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

The host's start token.

Returns

Task

StartingAsync(CancellationToken)

Runs before any scheduler has been resolved. Does nothing; override it to do something.

public virtual Task StartingAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

The host's start token.

Returns

Task

StopAsync(CancellationToken)

Waits for whatever start-up is still running and shuts every scheduler down, unbinding it from the repository.

public Task StopAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

The host's shutdown token.

Returns

Task

Remarks

Not overridable, for the reason StartAsync(CancellationToken) is not.

Safe to call more than once, and safe to call concurrently, because the generic host does both: StopAsync while RunAsync is pending raises ApplicationStopping, WaitForShutdownAsync wakes on it and stops the host again, and the two stops reach every hosted service at once. Every call after the first joins the stop already under way and observes its outcome — the same completion, and the same failure if a shutdown threw.

StoppedAsync(CancellationToken)

Runs once every scheduler has been shut down. Does nothing; override it to do something.

public virtual Task StoppedAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

The host's shutdown token.

Returns

Task

StoppingAsync(CancellationToken)

Runs before the schedulers are shut down, while Schedulers still lists them. Does nothing; override it to do something.

public virtual Task StoppingAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

The host's shutdown token.

Returns

Task