import type { TimeSpan } from 'src/NodeJS/types'; import { IJobWorkerMetrics } from './IJobWorkerMetrics'; import { IJob } from '../IJob'; import { IJobWorkerSettings } from './Settings/IJobWorkerSettings'; /** A base worker for recurrent jobs managed in a database. */ export interface IJobWorkerBase { /** Gets the name of the current runner. */ runnerName: string; /** * Exports collected metrics. * * @returns An object with ExecutedJobs and LastJobDurationMs. */ getMetrics(): IJobWorkerMetrics; /** * Should return the name of the runner. A random string is attached to it so it will be unique for each instance. * * @returns A string representing the name of the runner. */ getRunnerName(): string; /** * Returns the parameters that are used if an initial job is created. * * @returns The parameters for the initial job. */ getInitialJobParameters(): TParams | undefined; /** * Hook that is called when a job run starts. * * @param cancellationToken A token to cancel the operation. * @returns A ValueTask that represents the asynchronous operation. */ preJobRunHookAsync(cancellationToken: AbortSignal): Promise; /** * Hook that is called when a job run ends. * * @param cancellationToken A token to cancel the operation. * @returns A ValueTask that represents the asynchronous operation. */ postJobRunHookAsync(cancellationToken: AbortSignal): Promise; /** * Processes the job and returns the result, if any. * * @param job The job. * @param serviceScope A service scope for injecting additional services. * @param cancellationToken A token to cancel the operation. * @returns The result of the job, if any. */ processJobAsync(job: IJob, cancellationToken: AbortSignal): Promise; /** * @inheritdoc */ executeAsync(stoppingToken: AbortSignal): Promise; /** * Updates the total items of the current job. * * @param items The amount of items the job must process. * @returns A task indicating when the operation is complete. */ setTotalItemsAsync(items: number): Promise; /** * Updates the given amount to successful items on the current job. * * @param amount The amount to add, 1 if nothing given. * @returns A taks indicating when the operation is complete. */ addProgressAsync(amount?: number): Promise; /** * Updates the given amount to successful items on the current job. * * @param amount The amount to add, 1 if nothing given. * @returns A taks indicating when the operation is complete. */ addFailedProgressAsync(amount?: number): Promise; /** * Retrieves a time that marks the span between finishing the current job and the due date of the next job. If null, * no new job will be added. * * @param settings An instance of the current job settings. * @param result The result of the current job. * @returns The timespan from now until the next job is due. */ addNextJobIn(settings: IJobWorkerSettings, result?: TResult): TimeSpan | undefined; }