1 import EventEmitter from
'events'
2 import type { MessageValue
} from
'../utility-types'
3 import type { IPool
} from
'./pool'
5 export type ErrorHandler
<Worker
> = (this: Worker
, e
: Error) => void
6 export type OnlineHandler
<Worker
> = (this: Worker
) => void
7 export type ExitHandler
<Worker
> = (this: Worker
, code
: number) => void
9 export interface IWorker
{
10 on(event
: 'error', handler
: ErrorHandler
<this>): void
11 on(event
: 'online', handler
: OnlineHandler
<this>): void
12 on(event
: 'exit', handler
: ExitHandler
<this>): void
15 export interface PoolOptions
<Worker
> {
17 * A function that will listen for error event on each worker.
19 errorHandler
?: ErrorHandler
<Worker
>
21 * A function that will listen for online event on each worker.
23 onlineHandler
?: OnlineHandler
<Worker
>
25 * A function that will listen for exit event on each worker.
27 exitHandler
?: ExitHandler
<Worker
>
29 * This is just to avoid not useful warnings message, is used to set `maxListeners` on event emitters (workers are event emitters).
36 class PoolEmitter
extends EventEmitter
{}
38 export abstract class AbstractPool
<
39 Worker
extends IWorker
,
40 // eslint-disable-next-line @typescript-eslint/no-explicit-any
42 // eslint-disable-next-line @typescript-eslint/no-explicit-any
44 > implements IPool
<Data
, Response
> {
45 public readonly workers
: Worker
[] = []
46 public nextWorker
: number = 0
49 * `workerId` as key and an integer value
51 public readonly tasks
: Map
<Worker
, number> = new Map
<Worker
, number>()
53 public readonly emitter
: PoolEmitter
55 protected id
: number = 0
58 public readonly numWorkers
: number,
59 public readonly filePath
: string,
60 public readonly opts
: PoolOptions
<Worker
> = { maxTasks
: 1000 }
63 throw new Error('Cannot start a pool from a worker!')
65 // TODO christopher 2021-02-07: Improve this check e.g. with a pattern or blank check
67 throw new Error('Please specify a file with a worker implementation')
72 for (let i
= 1; i
<= this.numWorkers
; i
++) {
73 this.internalNewWorker()
76 this.emitter
= new PoolEmitter()
79 protected setupHook (): void {
83 protected abstract isMain (): boolean
85 public async destroy (): Promise
<void> {
86 for (const worker
of this.workers
) {
87 await this.destroyWorker(worker
)
91 protected abstract destroyWorker (worker
: Worker
): void | Promise
<void>
93 protected abstract sendToWorker (
95 message
: MessageValue
<Data
>
98 protected addWorker (worker
: Worker
): void {
99 const previousWorkerIndex
= this.tasks
.get(worker
)
100 if (previousWorkerIndex
!== undefined) {
101 this.tasks
.set(worker
, previousWorkerIndex
+ 1)
103 throw Error('Worker could not be found in tasks map')
108 * Execute the task specified into the constructor with the data parameter.
110 * @param data The input for the task specified.
111 * @returns Promise that is resolved when the task is done.
113 public execute (data
: Data
): Promise
<Response
> {
114 // configure worker to handle message with the specified task
115 const worker
= this.chooseWorker()
116 this.addWorker(worker
)
118 const res
= this.internalExecute(worker
, id
)
119 this.sendToWorker(worker
, { data
: data
|| ({} as Data
), id
: id
})
123 protected abstract registerWorkerMessageListener (
125 listener
: (message
: MessageValue
<Response
>) => void
128 protected abstract unregisterWorkerMessageListener (
130 listener
: (message
: MessageValue
<Response
>) => void
133 protected internalExecute (worker
: Worker
, id
: number): Promise
<Response
> {
134 return new Promise((resolve
, reject
) => {
135 const listener
: (message
: MessageValue
<Response
>) => void = message
=> {
136 if (message
.id
=== id
) {
137 this.unregisterWorkerMessageListener(worker
, listener
)
138 this.addWorker(worker
)
139 if (message
.error
) reject(message
.error
)
140 else resolve(message
.data
as Response
)
143 this.registerWorkerMessageListener(worker
, listener
)
147 protected chooseWorker (): Worker
{
148 if (this.workers
.length
- 1 === this.nextWorker
) {
150 return this.workers
[this.nextWorker
]
153 return this.workers
[this.nextWorker
]
157 protected abstract newWorker (): Worker
159 protected abstract afterNewWorkerPushed (worker
: Worker
): void
161 protected internalNewWorker (): Worker
{
162 const worker
: Worker
= this.newWorker()
163 worker
.on('error', this.opts
.errorHandler
?? (() => {}))
164 worker
.on('online', this.opts
.onlineHandler
?? (() => {}))
165 // TODO handle properly when a worker exit
166 worker
.on('exit', this.opts
.exitHandler
?? (() => {}))
167 this.workers
.push(worker
)
168 this.afterNewWorkerPushed(worker
)
170 this.tasks
.set(worker
, 0)