import type { IJob } from "./Generated/IJob"; import type { IJobRepository } from "./Generated/IJobRepository"; import type { IJobWorkerBase } from "./Generated/Worker/IJobWorkerBase"; import { IJobWorkerMetrics } from "./Generated/Worker/IJobWorkerMetrics"; import type { IJobWorkerSettings } from "./Generated/Worker/Settings/IJobWorkerSettings"; import type { LoggerFactory } from "./Helper/LoggerFactory"; import { TimeSpan } from "./types"; /** * A base class that allows to implement a job worker. * @template TParams The type of the parameter a job has. * @template TResult The type of the result of a job. */ export declare abstract class JobWorkerBase implements IJobWorkerBase { protected settings: IJobWorkerSettings; protected jobRepo: IJobRepository; private currentJob; private baseLogger; private cancellationToken; private executedJobs; private lastJobDurationMs; private lastJobFinishTime; private lastJobDeleteTime; /** The last time the job execution loop started in high resolution (nanoseconds) real time. */ protected lastJobExecutionStart?: bigint; /** The last time the job execution loop ended in high resolution (nanoseconds) real time. */ protected lastJobExecutionStop?: bigint; /** * Initializes a new instance of the {@link JobWorkerBase} class. * @param {IJobWorkerSettings} settings The job worker settings. * @param {IJobRepository} jobRepo An instance of the job repository. * @param {LoggerFactory} loggerFactory A factory function that creates an instance of a logger. */ constructor(settings: IJobWorkerSettings, jobRepo: IJobRepository, loggerFactory?: LoggerFactory); /** The name of this worker as used in logs and written to the job. */ runnerName: Readonly; /** * Returns metrics of this worker instance. * @returns {Record} An object with executedJobs and lastJobDurationMs. */ getMetrics(): IJobWorkerMetrics; /** * Should return a name that distinguishes this worker from other workers. Should be the same for all instances since an id is generated * an appended to the string given here. */ abstract getRunnerName(): string; /** * Should return the parameters for the initial job at startup. Only needed if an initial job should be created. Can return undefined if * no intiial job is needed or job has no parameters. */ abstract getInitialJobParameters(): TParams | undefined; /** * A hook that is called before a worker runs. This is called before the job is fetched so it might be that no job is * available. */ abstract preJobRunHookAsync(cancellationToken: AbortSignal): Promise; /** * A hook that is called after the job is finished Is also called when no job was available. */ abstract postJobRunHookAsync(cancellationToken: AbortSignal): Promise; /** * Should contain the logic needed to handle the job. The promise should resolve to the result of the job. * @param job The job including its paramters. * @param cancellationToken A token to cancel the operation. * @returns {Promise} A promise that contains the result of the job. */ abstract processJobAsync(job: IJob, cancellationToken: AbortSignal): Promise; /** * Starts the worker. After a configurable initial delay it will frequently poll for new jobs and take the one with * the oldest due date. Each runs calls {@link preJobRunHookAsync} {@link processJobAsync} and * {@link postJobRunHookAsync}. * If no job is available {@link processJobAsync} is not called. To stop the worker use the provided stoppingToken. * @param {AbortSignal} stoppingToken A token that can be used to stop the worker. */ executeAsync(stoppingToken: AbortSignal): Promise; /** * A wrapper around the repository to help set the amount of items the job must process. * @param {number} items The amount of items of the job. */ setTotalItemsAsync(items: number): Promise; /** * A wrapper around the repository to help add progress to the job. * @param {number} amount The amount of progress to add. */ addProgressAsync(amount?: number): Promise; /** * A wrapper around the repository to help add failed progress to the job. * @param {number} amount The amount of failed progress to add. */ addFailedProgressAsync(amount?: number): Promise; /** * Determines the time span until the next job should be added after finishing the current one. * @param {IJobWorkerSettings} settings - The current job settings. * @param {TResult} [result] - The result of the current job. * @returns {TimeSpan | undefined} - The time span until the next job is due, or undefined if no new job should be * added. */ addNextJobIn(settings: IJobWorkerSettings, result?: TResult): TimeSpan | undefined; /** * Internal method to check for available jobs. * @param {IJobRepository} repo The repository. * @param {IJobWorkerSettings} settings The settings. * @param {AbortSignal} cancellationToken The cancellation token. * @returns {Promise | undefined>} The next due job or undefined if no job is found. */ protected getJobAsync(repo: IJobRepository, settings: IJobWorkerSettings, cancellationToken: AbortSignal): Promise | undefined>; private assertRepo; private assertJob; /** * A helper function that waits a given amount of time that can be cancelled with a token. * @param {number} seconds The amount of seconds to wait. * @param {AbortSignal} cancellationToken A token to cancel the waiting. * @returns {Promise} A promise that resolves after the given amount of seconds or rejects when the token is cancelled. */ private delay; /** * Internal handler for job runs. Is called between pre and post hook for every run through the main loop. * @param {IJobWorkerSettings} settings The worker settings. * @param {AbortSignal} cancellationToken The cancellation token. * @param {boolean} firstRun If this is the first run. * @returns {Promise} A promise that resolves to true when a job was executed or false if no job was found or it was resetted. */ private handleJobRunAsync; private addInitialJob; private deleteOldJobs; }