Skip to content

Commit 58c1c4b

Browse files
committed
Signal handling: Factor out common signal handling code
1 parent 4f27112 commit 58c1c4b

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

src/master/implementation.node.ts

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ let tsNodeAvailable: boolean | undefined
2727

2828
export const defaultPoolSize = cpus().length
2929

30+
interface Terminable {
31+
terminate(this: Terminable): any
32+
}
33+
34+
// Terminates the workers, empties the workers array, and exits.
35+
const onSignal = (workers: Terminable[], signal: string) => {
36+
Promise.all(workers.map(worker => worker.terminate())).then(
37+
() => process.exit(0),
38+
() => process.exit(1),
39+
)
40+
workers.length = 0
41+
}
42+
3043
function detectTsNode() {
3144
if (typeof __non_webpack_require__ === "function") {
3245
// Webpack build: => No ts-node required or possible
@@ -98,7 +111,7 @@ function initWorkerThreadsWorker(): ImplementationExport {
98111
? __non_webpack_require__("worker_threads").Worker
99112
: eval("require")("worker_threads").Worker
100113

101-
let allWorkers: Array<typeof NativeWorker> = []
114+
const allWorkers: Array<typeof NativeWorker> = []
102115

103116
class Worker extends NativeWorker {
104117
private mappedEventListeners: WeakMap<EventListener, EventListener>
@@ -139,18 +152,9 @@ function initWorkerThreadsWorker(): ImplementationExport {
139152
}
140153
}
141154

142-
const terminateWorkersAndMaster = () => {
143-
// we should terminate all workers and then gracefully shutdown self process
144-
Promise.all(allWorkers.map(worker => worker.terminate())).then(
145-
() => process.exit(0),
146-
() => process.exit(1),
147-
)
148-
allWorkers = []
149-
}
150-
151155
// Take care to not leave orphaned processes behind. See #147.
152-
process.on("SIGINT", () => terminateWorkersAndMaster())
153-
process.on("SIGTERM", () => terminateWorkersAndMaster())
156+
process.on("SIGINT", (signal) => onSignal(allWorkers, signal))
157+
process.on("SIGTERM", (signal) => onSignal(allWorkers, signal))
154158

155159
class BlobWorker extends Worker {
156160
constructor(blob: Uint8Array, options?: ThreadsWorkerOptions) {
@@ -219,19 +223,10 @@ function initTinyWorker(): ImplementationExport {
219223
}
220224
}
221225

222-
const terminateWorkersAndMaster = () => {
223-
// we should terminate all workers and then gracefully shutdown self process
224-
Promise.all(allWorkers.map(worker => worker.terminate())).then(
225-
() => process.exit(0),
226-
() => process.exit(1),
227-
)
228-
allWorkers = []
229-
}
230-
231226
// Take care to not leave orphaned processes behind
232227
// See <https://github.com/avoidwork/tiny-worker#faq>
233-
process.on("SIGINT", () => terminateWorkersAndMaster())
234-
process.on("SIGTERM", () => terminateWorkersAndMaster())
228+
process.on("SIGINT", (signal) => onSignal(allWorkers, signal))
229+
process.on("SIGTERM", (signal) => onSignal(allWorkers, signal))
235230

236231
class BlobWorker extends Worker {
237232
constructor(blob: Uint8Array, options?: ThreadsWorkerOptions) {

0 commit comments

Comments
 (0)