Commit | Line | Data |
---|---|---|
962a8159 | 1 | import type EventEmitterAsyncResource from 'node:events'; |
130783a7 | 2 | import fs from 'node:fs'; |
4bfd80fa | 3 | |
b779c0f8 JB |
4 | import type { PoolInfo } from 'poolifier'; |
5 | ||
268a74bb | 6 | import { WorkerConstants } from './WorkerConstants'; |
b779c0f8 | 7 | import type { SetInfo, WorkerData, WorkerOptions } from './WorkerTypes'; |
7dde0b73 | 8 | |
268a74bb | 9 | export 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; |
962a8159 | 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 | */ |
e7aeea18 JB |
23 | constructor( |
24 | workerScript: string, | |
25 | workerOptions: WorkerOptions = { | |
3fa0f0ed JB |
26 | workerStartDelay: WorkerConstants.DEFAULT_WORKER_START_DELAY, |
27 | elementStartDelay: WorkerConstants.DEFAULT_ELEMENT_START_DELAY, | |
28 | poolMinSize: WorkerConstants.DEFAULT_POOL_MIN_SIZE, | |
29 | poolMaxSize: WorkerConstants.DEFAULT_POOL_MAX_SIZE, | |
30 | elementsPerWorker: WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER, | |
abe9e9dd | 31 | poolOptions: {}, |
59b6ed8d | 32 | messageHandler: WorkerConstants.EMPTY_FUNCTION, |
e7aeea18 JB |
33 | } |
34 | ) { | |
ba516f9c | 35 | if (workerScript === null || workerScript === undefined) { |
44a95b7f JB |
36 | throw new Error('Worker script is not defined'); |
37 | } | |
ba516f9c JB |
38 | if (typeof workerScript === 'string' && workerScript.trim().length === 0) { |
39 | throw new Error('Worker script is empty'); | |
40 | } | |
44a95b7f JB |
41 | if (!fs.existsSync(workerScript)) { |
42 | throw new Error('Worker script file does not exist'); | |
43 | } | |
ad2f27c3 | 44 | this.workerScript = workerScript; |
4d7227e6 | 45 | this.workerOptions = workerOptions; |
7dde0b73 JB |
46 | } |
47 | ||
8baf3f8f JB |
48 | /** |
49 | * Start the worker pool/set. | |
50 | */ | |
418106c8 | 51 | public abstract start(): Promise<void>; |
8baf3f8f JB |
52 | /** |
53 | * Stop the worker pool/set. | |
54 | */ | |
ded13d97 | 55 | public abstract stop(): Promise<void>; |
8baf3f8f JB |
56 | /** |
57 | * Add a task element to the worker pool/set. | |
58 | * | |
59 | * @param elementData - | |
60 | */ | |
c3ee95af | 61 | public abstract addElement(elementData: T): Promise<void>; |
7dde0b73 | 62 | } |