Skip to content

Kitto.Notification.Jobs

Background job runner for KittoX: a worker thread pool, separate from the Indy request pool, that executes long-running operations (exports, reports, imports) off the request/response cycle. A job (TKXJob) is submitted to the TKXJobQueue, runs on a worker, reports progress and completion through its TKXJobContext — which publishes events on the notification EventBus — and typically produces a downloadable artifact under {AppHome}\Jobs\{JobId}\. Job status is observable (for the notification center and the status/download endpoints). Scope v1: single-instance, in-memory job registry, cooperative cancellation.

TKXJobInfo record

An observable snapshot of a job's state, returned by the queue for the notification center and the status/download endpoints.

TKXJobItem record

Internal work item queued for a worker (job + identity).

TKXJobContext class

Runtime context passed to a running job. Offers progress reporting, cooperative cancellation, event emission (to the job owner's notification channel), the per-job artifact directory, and a way to declare the result file that the download endpoint will later serve.

pascal
class function Current: TKXJobContext;

The job context of the job currently executing on this worker thread, or nil on a normal request thread. Lets deep code (e.g. an export loop) report progress/cancellation without threading a context parameter through every call.

pascal
procedure ReportProgress(const APercent: Integer;

Updates the job's percent/message and publishes a JobProgress event to the owner.

pascal
procedure EmitEvent(const AEvent: TKXEvent);

Publishes an arbitrary event to the job owner's channel.

pascal
function IsCancelled: Boolean;

True once cancellation has been requested; a well-behaved job checks this periodically and returns early.

pascal
function ArtifactDir: string;

The job's artifact directory ({AppHome}\Jobs\{JobId}), created on demand.

pascal
function ArtifactPath(const AFileName: string): string;

Full path of a file inside this job's artifact directory ({AppHome}\Jobs\{JobId}\AFileName), creating the directory.

pascal
procedure SetResultFile(const AFullPath, AClientFileName, AContentType: string);

Declares the file produced by the job as its downloadable result; on successful completion the queue emits a JobCompleted event whose action is the job download URL, and the download endpoint serves this file.

pascal
procedure RequestCancel;

Requests cancellation (used internally by TKXJobQueue.Cancel).

TKXJob class

Abstract background job. Descendants implement Execute with the long-running work, using the context to report progress, honour cancellation and declare the produced artifact.

pascal
procedure Execute(const AContext: TKXJobContext);

Runs the job's work. Raising an exception marks the job failed.

TKXJobQueue class

Worker-pool job runner. Submitted jobs run on a fixed pool of worker threads (config Server/Jobs/PoolSize, default 4), independent of the Indy request threads. The queue keeps an in-memory registry of job status for observability. Process-global single instance.

pascal
function Submit(const AJob: TKXJob;

Submits a job for background execution and returns its id. The queue takes ownership of AJob and frees it after it runs.

pascal
function TryGetInfo(const AJobId: TGUID;

Returns the current status snapshot of a job, if known.

pascal
function GetUserJobs(const AUser: string): TArray<TKXJobInfo>;

Returns the known jobs owned by the given user, newest first.

pascal
procedure Cancel(const AJobId: TGUID);

Requests cooperative cancellation of a running/pending job.

pascal
procedure RemoveJob(const AJobId: TGUID);

Removes a job from the list (stopping it first if running) and deletes its artifact directory. Used by cancel, delete, dismiss and by the download endpoint (a downloaded job leaves the list).

pascal
procedure RemoveExpired(const AMaxAgeHours: Integer);

Retention: removes finished jobs (completed/failed/cancelled) whose completion is older than AMaxAgeHours, deleting their artifacts. A no-op when AMaxAgeHours <= 0.

pascal
class function Instance: TKXJobQueue;

The process-global job queue, created on first use with the pool size from Server/Jobs/PoolSize (default 4).

Released under Apache License, Version 2.0.