fix: fix possible null exception with worker_threads pools
[poolifier.git] / src / pools / thread / fixed.ts
CommitLineData
fc3e6586 1import {
85aeb3f3
JB
2 type MessageChannel,
3 type MessagePort,
fc3e6586 4 SHARE_ENV,
7d91a8cd 5 type TransferListItem,
65d7a1c9 6 Worker,
90082c8c 7 type WorkerOptions,
65d7a1c9 8 isMainThread
fc3e6586 9} from 'node:worker_threads'
e102732c 10import type { MessageValue } from '../../utility-types'
c97c7edb 11import { AbstractPool } from '../abstract-pool'
4b628b48
JB
12import { type PoolOptions, type PoolType, PoolTypes } from '../pool'
13import { type WorkerType, WorkerTypes } from '../worker'
4ade5f1f 14
90082c8c
JB
15/**
16 * Options for a poolifier thread pool.
17 */
18export interface ThreadPoolOptions extends PoolOptions<Worker> {
19 /**
20 * Worker options.
21 *
22 * @see https://nodejs.org/api/worker_threads.html#new-workerfilename-options
23 */
24 workerOptions?: WorkerOptions
25}
26
4ade5f1f 27/**
729c563d
S
28 * A thread pool with a fixed number of threads.
29 *
e102732c
JB
30 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
31 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
4ade5f1f
S
32 * @author [Alessandro Pio Ardizio](https://github.com/pioardi)
33 * @since 0.0.1
34 */
d3c8a1a8 35export class FixedThreadPool<
deb85c12
JB
36 Data = unknown,
37 Response = unknown
e102732c 38> extends AbstractPool<Worker, Data, Response> {
4ade5f1f 39 /**
729c563d
S
40 * Constructs a new poolifier fixed thread pool.
41 *
38e795c1
JB
42 * @param numberOfThreads - Number of threads for this pool.
43 * @param filePath - Path to an implementation of a `ThreadWorker` file, which can be relative or absolute.
44 * @param opts - Options for this fixed thread pool.
4ade5f1f
S
45 */
46 public constructor (
5c5a1fb7 47 numberOfThreads: number,
c97c7edb 48 filePath: string,
90082c8c 49 protected readonly opts: ThreadPoolOptions = {}
4ade5f1f 50 ) {
5c5a1fb7 51 super(numberOfThreads, filePath, opts)
c97c7edb 52 }
4ade5f1f 53
afc003b2 54 /** @inheritDoc */
c97c7edb
S
55 protected isMain (): boolean {
56 return isMainThread
4ade5f1f
S
57 }
58
afc003b2 59 /** @inheritDoc */
aa9eede8 60 protected async destroyWorkerNode (workerNodeKey: number): Promise<void> {
ae3ab61d 61 this.flagWorkerNodeAsNotReady(workerNodeKey)
81c02522
JB
62 this.flushTasksQueue(workerNodeKey)
63 // FIXME: wait for tasks to be finished
aa9eede8
JB
64 const workerNode = this.workerNodes[workerNodeKey]
65 const worker = workerNode.worker
041dc05b 66 const waitWorkerExit = new Promise<void>(resolve => {
ae036c3e 67 worker.once('exit', () => {
81c02522
JB
68 resolve()
69 })
70 })
72ae84a2 71 await this.sendKillMessageToWorker(workerNodeKey)
aa9eede8 72 workerNode.closeChannel()
78f60f82 73 workerNode.removeAllListeners()
c97c7edb 74 await worker.terminate()
c2301b8e 75 await waitWorkerExit
4ade5f1f
S
76 }
77
afc003b2 78 /** @inheritDoc */
aa9eede8
JB
79 protected sendToWorker (
80 workerNodeKey: number,
7d91a8cd
JB
81 message: MessageValue<Data>,
82 transferList?: TransferListItem[]
aa9eede8 83 ): void {
fa548cda 84 this.workerNodes[workerNodeKey].messageChannel?.port1?.postMessage(
dbfa7948 85 { ...message, workerId: this.getWorkerInfo(workerNodeKey).id },
72ae84a2
JB
86 transferList
87 )
85aeb3f3
JB
88 }
89
90 /** @inheritDoc */
aa9eede8 91 protected sendStartupMessageToWorker (workerNodeKey: number): void {
75de9f41 92 const workerNode = this.workerNodes[workerNodeKey]
75de9f41
JB
93 const port2: MessagePort = (workerNode.messageChannel as MessageChannel)
94 .port2
e9dd5b66 95 workerNode.worker.postMessage(
85aeb3f3
JB
96 {
97 ready: false,
dbfa7948 98 workerId: this.getWorkerInfo(workerNodeKey).id,
85aeb3f3
JB
99 port: port2
100 },
101 [port2]
102 )
103 }
104
105 /** @inheritDoc */
106 protected registerWorkerMessageListener<Message extends Data | Response>(
aa9eede8 107 workerNodeKey: number,
85aeb3f3
JB
108 listener: (message: MessageValue<Message>) => void
109 ): void {
fa548cda
JB
110 this.workerNodes[workerNodeKey].messageChannel?.port1?.on(
111 'message',
112 listener
113 )
4ade5f1f
S
114 }
115
ae036c3e
JB
116 /** @inheritDoc */
117 protected registerOnceWorkerMessageListener<Message extends Data | Response>(
118 workerNodeKey: number,
119 listener: (message: MessageValue<Message>) => void
120 ): void {
fa548cda
JB
121 this.workerNodes[workerNodeKey].messageChannel?.port1?.once(
122 'message',
123 listener
124 )
ae036c3e
JB
125 }
126
127 /** @inheritDoc */
128 protected deregisterWorkerMessageListener<Message extends Data | Response>(
129 workerNodeKey: number,
130 listener: (message: MessageValue<Message>) => void
131 ): void {
fa548cda
JB
132 this.workerNodes[workerNodeKey].messageChannel?.port1?.off(
133 'message',
134 listener
135 )
ae036c3e
JB
136 }
137
afc003b2 138 /** @inheritDoc */
e102732c 139 protected createWorker (): Worker {
c97c7edb 140 return new Worker(this.filePath, {
90082c8c
JB
141 env: SHARE_ENV,
142 ...this.opts.workerOptions
4ade5f1f 143 })
c97c7edb
S
144 }
145
afc003b2 146 /** @inheritDoc */
8881ae32 147 protected get type (): PoolType {
6b27d407 148 return PoolTypes.fixed
7c0ba920
JB
149 }
150
184855e6
JB
151 /** @inheritDoc */
152 protected get worker (): WorkerType {
153 return WorkerTypes.thread
154 }
155
afc003b2 156 /** @inheritDoc */
c319c66b 157 protected get busy (): boolean {
c2ade475 158 return this.internalBusy()
7c0ba920 159 }
4ade5f1f 160}