Table of Contents

Class JobTimeoutAttribute

Namespace
Quartz
Assembly
Quartz.dll

How long a firing of this job may run before the scheduler signals cancellation to it. Read by the timeout middleware AddJobTimeout registers, and overrides that call's scheduler-wide default.

[AttributeUsage(AttributeTargets.Class|AttributeTargets.Interface)]
public sealed class JobTimeoutAttribute : Attribute
Inheritance
JobTimeoutAttribute
Inherited Members

Examples

[JobTimeout("00:00:30")]
public sealed class ReportJob : IJob
{
    public async ValueTask Execute(IJobExecutionContext context, CancellationToken cancellationToken = default)
    {
        await BuildReport(cancellationToken);
    }
}

Remarks

Declared on the job rather than stored with it, for the reason DisallowConcurrentExecutionAttribute is: how long the work may take is a property of the code that does it, and an attribute travels with the type through every job store, every wire format and every way of scheduling — where a value written into a detail would have to be persisted, migrated and round-tripped to reach the same places. It is inherited from a base class or from an interface the job implements, so a contract can set the budget for everything that fulfils it.

Nothing happens without AddJobTimeout. The attribute is read by the timeout middleware, and a scheduler with no middleware has no timeouts. Call q.AddJobTimeout() to register it with no scheduler-wide default, so that only the jobs carrying this attribute are bounded, or q.AddJobTimeout(TimeSpan) to bound every job and let this attribute vary it.

A budget of zero means no timeout. [JobTimeout("00:00:00")] exempts a job from a scheduler-wide default — the long-running one whose whole point is to run until it is done. A negative budget is refused.

A job that ignores its CancellationToken cannot be stopped. The timeout signals cancellation the way an operator's InterruptFireInstance(string, CancellationToken) does, and nothing in .NET can abort code that declines to notice; CA2016 is what polices forwarding the token. Such a job runs to completion and is then reported as having timed out.

Constructors

JobTimeoutAttribute(string)

Declares the budget as a TimeSpan in its invariant form — "00:05:00" is five minutes, "1.00:00:00" is a day.

public JobTimeoutAttribute(string timeout)

Parameters

timeout string

The budget, as an invariant TimeSpan. "00:00:00" means this job has no timeout, whatever the scheduler's default is.

Remarks

A string, because TimeSpan is not something an attribute argument can be. The format is Parse(string, IFormatProvider)'s, parsed with the invariant culture so the same source reads the same everywhere, and a value it cannot parse is refused here rather than silently meaning nothing.

Exceptions

ArgumentException

timeout is not a TimeSpan.

ArgumentOutOfRangeException

timeout is negative.

Properties

Timeout

The budget a firing of this job gets, or Zero when it has none.

public TimeSpan Timeout { get; }

Property Value

TimeSpan

See Also