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