@mdf.js/core
Version:
MMS - API Core - Common types, classes and functions
140 lines • 6.12 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, Multi } from '@mdf.js/crash';
import { EventEmitter } from 'events';
import { AnyHeaders, AnyOptions, DoneEventHandler, JobObject, JobRequest, Options, Result, Status } from './types';
/**
* JobHandler class
* @category @mdf.js/core
*/
export declare interface JobHandler<Type extends string> {
/**
* Register an event listener over the `done` event, which is emitted when a job has ended, either
* due to completion or failure.
* @param event - `done` event
* @param listener - The listener function to add
*/
on(event: 'done', listener: DoneEventHandler<Type>): this;
/**
* Register an event listener over the `done` event, which is emitted when a job has ended, either
* due to completion or failure.
* @param event - `done` event
* @param listener - The listener function to add
*/
addListener(event: 'done', listener: DoneEventHandler<Type>): this;
/**
* Registers a event listener over the `done` event, at the beginning of the listeners array,
* which is emitted when a job has ended, either due to completion or failure.
* @param event - `done` event
* @param listener - The listener function to add
*/
prependListener(event: 'done', listener: DoneEventHandler<Type>): this;
/**
* Registers a one-time event listener over the `done` event, which is emitted when a job has
* ended, either due to completion or failure.
* @param event - `done` event
* @param listener - The listener function to add
*/
once(event: 'done', listener: DoneEventHandler<Type>): this;
/**
* Registers a one-time event listener over the `done` event, at the beginning of the listeners
* array, which is emitted when a job has ended, either due to completion or failure.
* @param event - `done` event
* @param listener - The listener function to add
*/
prependOnceListener(event: 'done', listener: DoneEventHandler<Type>): this;
/**
* Removes the specified listener from the listener array for the `done` event.
* @param event - `done` event
* @param listener - The listener function to remove
*/
removeListener(event: 'done', listener: DoneEventHandler<Type>): this;
/**
* Removes the specified listener from the listener array for the `done` event.
* @param event - `done` event
* @param listener - The listener function to remove
*/
off(event: 'done', listener: DoneEventHandler<Type>): this;
/**
* Removes all listeners, or those of the specified event.
* @param event - `done` event
*/
removeAllListeners(event?: 'done'): this;
}
/**
* JobHandler class
* @category @mdf.js/core
* @typeParam Type - Job type, used as selector for strategies in job processors
* @typeParam Data - Job payload
* @typeParam CustomHeaders - Custom headers, used to pass specific information for job processors
* @typeParam CustomOptions - Custom options, used to pass specific information for job processors
*/
export declare class JobHandler<Type extends string = string, Data = unknown, CustomHeaders extends Record<string, any> = AnyHeaders, CustomOptions extends Record<string, any> = AnyOptions> extends EventEmitter implements JobObject<Type, Data, CustomHeaders> {
/** Unique job processing identification */
readonly uuid: string;
/** User job request identifier, defined by the user */
readonly jobUserId: string;
/** Unique user job request identification, based on jobUserId */
readonly jobUserUUID: string;
/** Date object with the timestamp when the job was created */
readonly createdAt: Date;
/** Job type, used as selector for strategies in job processors */
readonly type: Type;
/** Job meta information, used to pass specific information for job processors */
readonly options?: Options<CustomHeaders, CustomOptions>;
/** Date object with the timestamp when the job was resolved */
private resolvedAt?;
/** Job processing status */
private _status;
/** Error raised during job processing */
private _errors?;
/** Job payload */
private _data;
/** Pending confirmation */
private pendingDone;
/**
* Create a new instance of JobHandler
* @param jobRequest - job request object
*/
constructor(jobRequest: JobRequest<Type, Data, CustomHeaders, CustomOptions>);
/**
* Create a new instance of JobHandler
* @param jobUserId - User job request identifier, defined by the user
* @param data - Job payload
* @param type - Job type, used as selector for strategies in job processors
* @param options - JobHandler options
*/
constructor(jobUserId: string, data: Data, type?: Type, options?: Options<CustomHeaders, CustomOptions>);
/** Job payload */
get data(): Data;
set data(value: Data);
/** True if the job task raised any error */
get hasErrors(): boolean;
/** Errors raised during the job */
get errors(): Multi | undefined;
/** Return the process time in msec */
get processTime(): number;
/** Return the job processing status */
get status(): Status;
/**
* Add a new error in the job
* @param error - error to be added to the job
*/
addError(error: Crash | Multi): void;
/**
* Notify the results of the process
* @param error - conditional parameter for error notification
*/
done(error?: Crash): void;
/** Return the result of the publication process */
result(): Result<Type>;
/** Return an object with the key information of the job, this information is used by the plugs */
toObject(): JobObject<Type, Data, CustomHeaders, CustomOptions>;
/** Update the job status to processing if it is in pending state */
private updateStatusToProcessing;
}
//# sourceMappingURL=JobHandler.d.ts.map