'ecma',
'enqueue',
'enum',
+ 'errored',
'esm',
'fibonacci',
'fs',
*
* @typeParam Worker - Type of worker which manages this pool.
* @typeParam Data - Type of data sent to the worker. This can only be serializable data.
- * @typeParam Response - Type of response of execution. This can only be serializable data.
+ * @typeParam Response - Type of execution response. This can only be serializable data.
*/
export abstract class AbstractPool<
Worker extends IWorker,
id: crypto.randomUUID()
}
const res = new Promise<Response>((resolve, reject) => {
- this.promiseResponseMap.set(submittedTask.id, {
+ this.promiseResponseMap.set(submittedTask.id as string, {
resolve,
reject,
worker: workerNode.worker
* When the maximum number of workers is reached and workers are busy, an event is emitted. If you want to listen to this event, use the pool's `emitter`.
*
* @typeParam Data - Type of data sent to the worker. This can only be serializable data.
- * @typeParam Response - Type of response of execution. This can only be serializable data.
+ * @typeParam Response - Type of execution response. This can only be serializable data.
* @author [Christopher Quadflieg](https://github.com/Shinigami92)
* @since 2.0.0
*/
* This pool selects the workers in a round robin fashion.
*
* @typeParam Data - Type of data sent to the worker. This can only be serializable data.
- * @typeParam Response - Type of response of execution. This can only be serializable data.
+ * @typeParam Response - Type of execution response. This can only be serializable data.
* @author [Christopher Quadflieg](https://github.com/Shinigami92)
* @since 2.0.0
*/
*
* @typeParam Worker - Type of worker which manages this pool.
* @typeParam Data - Type of data sent to the worker. This can only be serializable data.
- * @typeParam Response - Type of response of execution. This can only be serializable data.
+ * @typeParam Response - Type of execution response. This can only be serializable data.
*/
export interface IPool<
Worker extends IWorker,
*
* @typeParam Worker - Type of worker which manages the strategy.
* @typeParam Data - Type of data sent to the worker. This can only be serializable data.
- * @typeParam Response - Type of response of execution. This can only be serializable data.
+ * @typeParam Response - Type of execution response. This can only be serializable data.
*/
export abstract class AbstractWorkerChoiceStrategy<
Worker extends IWorker,
*
* @typeParam Worker - Type of worker which manages the strategy.
* @typeParam Data - Type of data sent to the worker. This can only be serializable data.
- * @typeParam Response - Type of response of execution. This can only be serializable data.
+ * @typeParam Response - Type of execution response. This can only be serializable data.
*/
export class FairShareWorkerChoiceStrategy<
Worker extends IWorker,
*
* @typeParam Worker - Type of worker which manages the strategy.
* @typeParam Data - Type of data sent to the worker. This can only be serializable data.
- * @typeParam Response - Type of response of execution. This can only be serializable data.
+ * @typeParam Response - Type of execution response. This can only be serializable data.
*/
export class LessBusyWorkerChoiceStrategy<
Worker extends IWorker,
*
* @typeParam Worker - Type of worker which manages the strategy.
* @typeParam Data - Type of data sent to the worker. This can only be serializable data.
- * @typeParam Response - Type of response of execution. This can only be serializable data.
+ * @typeParam Response - Type of execution response. This can only be serializable data.
*/
export class LessUsedWorkerChoiceStrategy<
Worker extends IWorker,
*
* @typeParam Worker - Type of worker which manages the strategy.
* @typeParam Data - Type of data sent to the worker. This can only be serializable data.
- * @typeParam Response - Type of response of execution. This can only be serializable data.
+ * @typeParam Response - Type of execution response. This can only be serializable data.
*/
export class RoundRobinWorkerChoiceStrategy<
Worker extends IWorker,
*
* @typeParam Worker - Type of worker which manages the strategy.
* @typeParam Data - Type of data sent to the worker. This can only be serializable data.
- * @typeParam Response - Type of response of execution. This can only be serializable data.
+ * @typeParam Response - Type of execution response. This can only be serializable data.
*/
export class WeightedRoundRobinWorkerChoiceStrategy<
Worker extends IWorker,
*
* @typeParam Worker - Type of worker.
* @typeParam Data - Type of data sent to the worker. This can only be serializable data.
- * @typeParam Response - Type of response of execution. This can only be serializable data.
+ * @typeParam Response - Type of execution response. This can only be serializable data.
*/
export class WorkerChoiceStrategyContext<
Worker extends IWorker,
* When the maximum number of threads is reached and workers are busy, an event is emitted. If you want to listen to this event, use the pool's `emitter`.
*
* @typeParam Data - Type of data sent to the worker. This can only be serializable data.
- * @typeParam Response - Type of response of execution. This can only be serializable data.
+ * @typeParam Response - Type of execution response. This can only be serializable data.
* @author [Alessandro Pio Ardizio](https://github.com/pioardi)
* @since 0.0.1
*/
* This pool selects the threads in a round robin fashion.
*
* @typeParam Data - Type of data sent to the worker. This can only be serializable data.
- * @typeParam Response - Type of response of execution. This can only be serializable data.
+ * @typeParam Response - Type of execution response. This can only be serializable data.
* @author [Alessandro Pio Ardizio](https://github.com/pioardi)
* @since 0.0.1
*/
) => void
/**
- * Worker task interface.
+ * Message object that is passed as a task between main worker and worker.
*
* @typeParam Data - Type of data sent to the worker. This can only be serializable data.
* @internal
*/
export interface Task<Data = unknown> {
/**
- * Worker task data.
+ * Input data that will be passed to the worker.
*/
- data: Data
+ readonly data?: Data
/**
- * Task UUID.
+ * UUID of the message.
*/
- id: string
+ readonly id?: string
}
/**
* Worker tasks usage statistics.
*/
export interface TasksUsage {
+ /**
+ * Number of tasks executed.
+ */
run: number
+ /**
+ * Number of tasks running.
+ */
running: number
+ /**
+ * Tasks runtime.
+ */
runTime: number
+ /**
+ * Tasks runtime history.
+ */
runTimeHistory: CircularArray<number>
+ /**
+ * Average tasks runtime.
+ */
avgRunTime: number
+ /**
+ * Median tasks runtime.
+ */
medRunTime: number
+ /**
+ * Number of tasks errored.
+ */
error: number
}
/**
* Worker node worker.
*/
- worker: Worker
+ readonly worker: Worker
/**
* Worker node tasks usage statistics.
*/
/**
* Worker node tasks queue.
*/
- tasksQueue: Array<Task<Data>>
+ readonly tasksQueue: Array<Task<Data>>
}
import type { Worker as ClusterWorker } from 'node:cluster'
import type { MessagePort } from 'node:worker_threads'
import type { KillBehavior } from './worker/worker-options'
-import type { IWorker } from './pools/worker'
+import type { IWorker, Task } from './pools/worker'
/**
* Make all properties in T non-readonly.
export type Draft<T> = { -readonly [P in keyof T]?: T[P] }
/**
- * Message object that is passed between worker and main worker.
+ * Message object that is passed between main worker and worker.
*
* @typeParam Data - Type of data sent to the worker. This can only be serializable data.
* @typeParam MainWorker - Type of main worker.
export interface MessageValue<
Data = unknown,
MainWorker extends ClusterWorker | MessagePort | unknown = unknown
-> {
- /**
- * Input data that will be passed to the worker.
- */
- readonly data?: Data
- /**
- * Id of the message.
- */
- readonly id?: string
+> extends Task<Data> {
/**
* Kill code.
*/