build: cleanup eslint configuration
[e-mobility-charging-stations-simulator.git] / src / worker / WorkerAbstract.ts
CommitLineData
38b2428f 1import type { EventEmitterAsyncResource } from 'node:events';
d972af76 2import { existsSync } from 'node:fs';
4bfd80fa 3
2b59e7f7 4import type { PoolInfo } from 'poolifier';
b779c0f8 5
b779c0f8 6import type { SetInfo, WorkerData, WorkerOptions } from './WorkerTypes';
be245fda 7import { defaultErrorHandler, defaultExitHandler } from './WorkerUtils';
7dde0b73 8
268a74bb 9export abstract class WorkerAbstract<T extends WorkerData> {
322c9192 10 protected readonly workerScript: string;
4d7227e6 11 protected readonly workerOptions: WorkerOptions;
b779c0f8 12 public abstract readonly info: PoolInfo | SetInfo;
f2bf9948 13 public abstract readonly size: number;
72092cfc 14 public abstract readonly maxElementsPerWorker: number | undefined;
38b2428f 15 public abstract readonly emitter: EventEmitterAsyncResource | undefined;
6af9012e 16
7dde0b73 17 /**
e71cccf3 18 * `WorkerAbstract` constructor.
7dde0b73 19 *
0e4fa348
JB
20 * @param workerScript -
21 * @param workerOptions -
7dde0b73 22 */
4a3807d1 23 constructor(workerScript: string, workerOptions: WorkerOptions) {
a78c196b 24 if (workerScript == null) {
44a95b7f
JB
25 throw new Error('Worker script is not defined');
26 }
ba516f9c
JB
27 if (typeof workerScript === 'string' && workerScript.trim().length === 0) {
28 throw new Error('Worker script is empty');
29 }
d972af76 30 if (!existsSync(workerScript)) {
44a95b7f
JB
31 throw new Error('Worker script file does not exist');
32 }
ad2f27c3 33 this.workerScript = workerScript;
4d7227e6 34 this.workerOptions = workerOptions;
a37fc6dc
JB
35 this.workerOptions.poolOptions!.errorHandler =
36 this.workerOptions.poolOptions?.errorHandler ?? defaultErrorHandler;
37 this.workerOptions.poolOptions!.exitHandler =
38 this.workerOptions.poolOptions?.exitHandler ?? defaultExitHandler;
7dde0b73
JB
39 }
40
8baf3f8f 41 /**
361c98f5 42 * Starts the worker pool/set.
8baf3f8f 43 */
418106c8 44 public abstract start(): Promise<void>;
8baf3f8f 45 /**
361c98f5 46 * Stops the worker pool/set.
8baf3f8f 47 */
ded13d97 48 public abstract stop(): Promise<void>;
8baf3f8f 49 /**
361c98f5 50 * Adds a task element to the worker pool/set.
8baf3f8f
JB
51 *
52 * @param elementData -
53 */
c3ee95af 54 public abstract addElement(elementData: T): Promise<void>;
7dde0b73 55}