Commit | Line | Data |
---|---|---|
66a7748d | 1 | import type { EventEmitterAsyncResource } from 'node:events' |
66a7748d | 2 | import type { PoolInfo } from 'poolifier' |
b779c0f8 | 3 | |
0749233f JB |
4 | import { existsSync } from 'node:fs' |
5 | ||
66a7748d | 6 | import type { SetInfo, WorkerData, WorkerOptions } from './WorkerTypes.js' |
7dde0b73 | 7 | |
3b09e788 | 8 | export abstract class WorkerAbstract<D extends WorkerData, R extends WorkerData> { |
66a7748d | 9 | protected readonly workerOptions: WorkerOptions |
0749233f JB |
10 | protected readonly workerScript: string |
11 | public abstract readonly emitter: EventEmitterAsyncResource | undefined | |
66a7748d | 12 | public abstract readonly info: PoolInfo | SetInfo |
66a7748d | 13 | public abstract readonly maxElementsPerWorker: number | undefined |
0749233f | 14 | public abstract readonly size: number |
6af9012e | 15 | |
7dde0b73 | 16 | /** |
e71cccf3 | 17 | * `WorkerAbstract` constructor. |
0e4fa348 JB |
18 | * @param workerScript - |
19 | * @param workerOptions - | |
7dde0b73 | 20 | */ |
5199f9fd | 21 | constructor (workerScript: string | undefined, workerOptions: WorkerOptions) { |
a78c196b | 22 | if (workerScript == null) { |
66a7748d | 23 | throw new TypeError('Worker script is not defined') |
44a95b7f | 24 | } |
49e2c1e5 | 25 | if (typeof workerScript !== 'string') { |
66a7748d | 26 | throw new TypeError('Worker script must be a string') |
49e2c1e5 JB |
27 | } |
28 | if (workerScript.trim().length === 0) { | |
66a7748d | 29 | throw new Error('Worker script is an empty string') |
ba516f9c | 30 | } |
d972af76 | 31 | if (!existsSync(workerScript)) { |
66a7748d | 32 | throw new Error('Worker script file does not exist') |
44a95b7f | 33 | } |
66a7748d JB |
34 | this.workerScript = workerScript |
35 | this.workerOptions = workerOptions | |
7dde0b73 JB |
36 | } |
37 | ||
0749233f JB |
38 | /** |
39 | * Adds a task element to the worker pool/set. | |
40 | * @param elementData - | |
41 | */ | |
42 | public abstract addElement (elementData: D): Promise<R> | |
8baf3f8f | 43 | /** |
361c98f5 | 44 | * Starts the worker pool/set. |
8baf3f8f | 45 | */ |
0749233f | 46 | public abstract start (): Promise<void> | void |
8baf3f8f | 47 | /** |
361c98f5 | 48 | * Stops the worker pool/set. |
8baf3f8f | 49 | */ |
66a7748d | 50 | public abstract stop (): Promise<void> |
7dde0b73 | 51 | } |