Table of Contents

Class Job

Namespace
Dynamicweb.Scheduling
Assembly
Dynamicweb.Core.dll

Base class for a single execution of a scheduled task (Task). A job wraps the task with a task-specific logger and the bookkeeping needed to record the outcome (last result, last exception, last event id) back on the task. Jobs are created through GetJob(Task, CancellationToken) — which returns the built-in job that resolves and runs the task's BaseScheduledTaskAddIn — and executed by the scheduler service, which calls Execute() on a worker thread subject to the task's configured timeout.

public abstract class Job
Inheritance
Job
Inherited Members

Remarks

How to create a custom job: Subclassing Job is only relevant when building an alternative task execution pipeline; to add a new kind of scheduled task, subclass BaseScheduledTaskAddIn instead. A subclass implements Execute(), performing the work and reporting the outcome: call LogStarted() first and LogFinished() last (LogFinished() also persists the task when the outcome changed), set TaskRunResult and call UpdateLastResult(bool?) with the result, and route messages through the logging helpers so they end up in the task's log file.

Things to be careful of:

  • Execute() should not let exceptions escape: catch them, pass them to LogException(Exception) (which writes the task log and the event log and records the event id on the task) and record a failed result. An exception that escapes is treated by the scheduler as a failed, possibly timed-out run and bypasses the job's own bookkeeping.
  • The scheduler reads IsCancellationTokenUsed before running the job: when a subclass sets it to true, Execute() runs on a thread-pool task governed by CancellationToken and is expected to observe that token; when false (the default), the job runs on a dedicated thread that is interrupted (ThreadInterruptedException) on timeout or cancellation.
  • TaskRunResult is how the scheduler learns the outcome: false marks the run as failed and can stop subsequent sub-tasks in a task group, while null (the default) counts as success. Set it before Execute() returns.
  • Jobs run outside an HTTP request context when started by the scheduler service; use HostUrl for absolute URLs instead of relying on a current request.

Constructors

Job(Task)

Initializes the job for the given task and creates the task-specific logger. The job cannot be cancelled through CancellationToken when created with this overload.

protected Job(Task task)

Parameters

task Task

The scheduled task to execute. Must not be null.

Exceptions

ArgumentNullException

task is null.

Job(Task, CancellationToken)

Initializes the job for the given task with a cancellation token. The scheduler passes a token that is cancelled when the task's configured timeout elapses or the run is cancelled from administration.

protected Job(Task task, CancellationToken token)

Parameters

task Task

The scheduled task to execute. Must not be null.

token CancellationToken

The token that signals timeout or cancellation of the run; exposed through CancellationToken.

Exceptions

ArgumentNullException

task is null.

Fields

Logger

The logger writing to the task's own log file (the "ScheduledTasks" logger for LogFileName). Prefer the Log* helper methods, which prefix messages with the task name and id.

protected ILogger Logger

Field Value

ILogger

TaskRunResult

The outcome of the run, set by Execute() implementations. The scheduler reads it after Execute() returns: false marks the run as failed (which can stop subsequent sub-tasks in a task group), while true or null counts as success.

public bool? TaskRunResult

Field Value

bool?

_task

The scheduled task this job executes, as passed to the constructor. Never null.

protected Task _task

Field Value

Task

Properties

CancellationToken

Gets the token that is cancelled when the task's configured timeout elapses or the run is cancelled from administration. Default (non-cancellable) when the job was created without a token.

public CancellationToken CancellationToken { get; }

Property Value

CancellationToken

HostUrl

Gets or sets the base URL of the site the job runs for. Assigned by the scheduler before Execute() is called; when not set, falls back to the URL of the current HTTP request if one exists. May be null when no host URL is known.

public Uri? HostUrl { get; set; }

Property Value

Uri

IsCancellationTokenUsed

Gets a value indicating whether Execute() observes CancellationToken. The scheduler reads this before running the job: when true, the job runs on a thread-pool task governed by the token; when false (the default), it runs on a dedicated thread that is interrupted on timeout or cancellation. Subclasses that support cooperative cancellation set this to true.

public bool IsCancellationTokenUsed { get; protected set; }

Property Value

bool

Methods

Execute()

Performs the run. Called once by the scheduler on a worker thread, subject to the task's configured timeout. Implementations should catch their own exceptions, report them through LogException(Exception), and record the outcome in TaskRunResult and via UpdateLastResult(bool?) before returning.

public abstract void Execute()

LogError(string)

Writes an error message to the task's log file, prefixed with the task name and id.

protected void LogError(string message)

Parameters

message string

The message to log.

LogException(Exception)

Reports an exception that failed the run: writes it to the task's log file, creates an error entry in the event log, and records the event id on the task so administration can link the failed run to the event. The exception is also remembered and stored as the task's last exception by LogFinished().

protected void LogException(Exception ex)

Parameters

ex Exception

The exception to report. Must not be null.

Exceptions

ArgumentNullException

ex is null.

LogFinished()

Logs that the run has finished, records the last exception (if any was passed to LogException(Exception)) on the task, and saves the task when its recorded state changed during the run. Call at the end of Execute() — typically in a finally block — paired with LogStarted().

protected void LogFinished()

LogMessage(string)

Writes an informational message to the task's log file, prefixed with the task name and id.

protected void LogMessage(string message)

Parameters

message string

The message to log.

LogStarted()

Logs that the run has started and clears the last event id recorded on the task. Call at the beginning of Execute(), paired with LogFinished() at the end.

protected void LogStarted()

LogWarning(string)

Writes a warning to both the task's log file and the system "Scheduling" log, prefixed with the task name and id.

protected void LogWarning(string message)

Parameters

message string

The message to log.

UpdateLastResult(bool?)

Records the outcome of the run as the task's last result, marking the task for saving (performed by LogFinished()) when the result changed. Call together with setting TaskRunResult.

protected void UpdateLastResult(bool? result)

Parameters

result bool?

The outcome to record; false for a failed run, true for a successful one.

To top