Table of Contents

TJobWorker Class

Runs a job when it starts, periodically, and on request.

Remarks

Either set OnProcess, or derive from it and override ProcessJob. The job receives a cancellation token: check it in long units of work, or pass it on, so that a stop does not have to wait for the run to finish.

Runs never overlap: they happen one at a time, on the worker's own thread. Period is counted from the end of a run, so a slow run delays the next one rather than piling up.

An exception escaping a run is logged, passed to OnError, and the worker goes on. A run that gives up with EOperationCancelled because the worker is stopping is not a failure.

Hosted, it is added to the host like any worker: CreateHost.Add(TCleanupJob), where TCleanupJob sets its properties in an overridden constructor, or CreateHost.Add(Job as IHostedService) for an instance. Without a host, call Start and Stop and free it like any other object.

Syntax

Unit: Sparkle.Host.Jobs

TJobWorker = class(THostedWorker);

Methods

Name Description
ProcessJob Does the work of one run. Calls OnProcess unless overridden.
Trigger Asks for a run as soon as possible, without waiting for it.

Properties

Name Description
OnError Called with the exception of a failed run, after it was logged.
OnProcess The work of one run, unless ProcessJob is overridden.
OnRunFinished Called after each run, successful or not, with the updated state.
Period Seconds between the end of a run and the start of the next one.
Default: 60.
State What the worker has been doing. Safe from any thread.