@mdf.js/tasks
Version:
MMS - API Core - Tasks
151 lines • 5.48 kB
TypeScript
/**
* Copyright 2024 Mytra Control S.L. All rights reserved.
*
* Use of this source code is governed by an MIT-style license that can be found in the LICENSE file
* or at https://opensource.org/licenses/MIT.
*/
import { Crash } from '@mdf.js/crash';
import { RetryOptions } from '@mdf.js/utils';
import { EventEmitter } from 'events';
import { DoneEventHandler, MetaData, TaskOptions } from './types';
/** Represents the task handler */
export declare interface TaskHandler<Result, Binded> {
/**
* Register an event listener over the `done` event, which is emitted when a task has ended, either
* due to completion or failure.
* @param event - `done` event
* @param listener - Done event handler
* @event
*/
on(event: 'done', listener: DoneEventHandler<Result>): this;
/**
* Register an event listener over the `done` event, which is emitted when a task has ended, either
* due to completion or failure.
* @param event - `done` event
* @param listener - Done event handler
* @event
*/
addListener(event: 'done', listener: DoneEventHandler<Result>): this;
/**
* Registers a event listener over the `done` event, at the beginning of the listeners array,
* which is emitted when a task has ended, either due to completion or failure.
* @param event - `done` event
* @param listener - Done event handler
* @event
*/
prependListener(event: 'done', listener: DoneEventHandler<Result>): this;
/**
* Registers a one-time event listener over the `done` event, which is emitted when a task has
* ended, either due to completion or failure.
* @param event - `done` event
* @param listener - Done event handler
* @event
*/
once(event: 'done', listener: DoneEventHandler<Result>): this;
/**
* Registers a one-time event listener over the `done` event, at the beginning of the listeners
* array, which is emitted when a task has ended, either due to completion or failure.
* @param event - `done` event
* @param listener - Done event handler
* @event
*/
prependOnceListener(event: 'done', listener: DoneEventHandler<Result>): this;
/**
* Removes the specified listener from the listener array for the `done` event.
* @param event - `done` event
* @param listener - Done event handler
* @event
*/
removeListener(event: 'done', listener: DoneEventHandler<Result>): this;
/**
* Removes the specified listener from the listener array for the `done` event.
* @param event - `done` event
* @param listener - Done event handler
* @event
*/
off(event: 'done', listener: DoneEventHandler<Result>): this;
/**
* Removes all listeners, or those of the specified event.
* @param event - `done` event
* @event
*/
removeAllListeners(event?: 'done'): this;
}
/** Represents the task handler */
export declare abstract class TaskHandler<Result = any, Binded = any> extends EventEmitter {
/** Unique task identification, unique for each task */
readonly uuid: string;
/** Task identifier, defined by the user */
readonly taskId: string;
/** Date when the task was created */
readonly createdAt: Date;
/** Date when the task was executed in ISO format */
protected executedAt?: Date;
/** Date when the task was completed in ISO format */
protected completedAt?: Date;
/** Date when the task was cancelled in ISO format */
protected cancelledAt?: Date;
/** Date when the task was failed in ISO format */
protected failedAt?: Date;
/** Reason of failure or cancellation */
protected _reason?: string;
/** Additional metadata in case the execution required execute other tasks */
protected readonly _$meta: MetaData[];
/** Task priority */
readonly priority: number;
/** Task weight */
readonly weight: number;
/** Status of the task */
private status;
/** Retry options */
protected retryOptions: RetryOptions;
/** Context to be bind to the task */
protected context?: Binded;
/** Result of the task */
private result?;
/** Strategy to retry the task */
private readonly strategy;
/**
* Create a new task handler
* @param options - The options for the task
*/
constructor(options?: TaskOptions<Binded>);
/**
* Get the cause of the error
* @param error - The error to get the cause
* @returns The cause of the error
*/
private getCause;
/**
* Execute the task
* @returns The result of the task
*/
private shouldBeExecuted;
/**
* Handle the error
* @param rawError - The error to handle
* @returns The error
*/
private onError;
/**
* Handle the success
* @param result - The result of the task
* @returns The result
*/
private onSuccess;
/** Handle the retry */
private onRetry;
/** Return the duration of the task */
private get duration();
/** Notify that the task has been processed */
private done;
/** Return the metadata of the task */
get metadata(): MetaData;
/** Execute the task */
execute(): Promise<Result>;
/** Cancel the task */
cancel(error?: Crash): void;
/** Execute the underlayer execution strategy */
protected abstract _execute(): Promise<Result>;
}
//# sourceMappingURL=TaskHandler.d.ts.map