-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
128 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import EventEmitter from 'node:events' | ||
import cluster from 'node:cluster' | ||
import { cpus } from 'node:os' | ||
|
||
export type MultiprocessOptions = { | ||
/** | ||
* If `true`, a single worker failure should not kill us all. A failed worker will get respawned. | ||
*/ | ||
keepAlive: boolean | ||
autostart: boolean | ||
workers: number | undefined | ||
main: MultiprocessWork | ||
} | ||
|
||
const DEFAULT_OPTIONS: Partial<MultiprocessOptions> = { | ||
keepAlive: true, | ||
autostart: true, | ||
} | ||
|
||
export type MultiprocessWork = () => void | Promise<void> | ||
|
||
export class Multiprocess extends EventEmitter { | ||
private keepAlive: boolean | ||
private readonly work: MultiprocessWork | ||
|
||
constructor(work: MultiprocessWork, options: Partial<MultiprocessOptions>) { | ||
super() | ||
const effectiveOptions = { ...DEFAULT_OPTIONS, ...options } | ||
if (!work || typeof work !== 'function') { | ||
throw new Error('You need to provide a worker function.') | ||
} | ||
|
||
this.keepAlive = effectiveOptions.keepAlive ?? true | ||
this.work = work.bind(this) | ||
this.fork = this.fork.bind(this) | ||
this.stop = this.stop.bind(this) | ||
|
||
if (cluster.isPrimary) { | ||
cluster.setupPrimary({ | ||
silent: false, | ||
}) | ||
} | ||
|
||
if (effectiveOptions.autostart) { | ||
if (cluster.isWorker) { | ||
this.work() | ||
} else { | ||
this.start(effectiveOptions) | ||
} | ||
} | ||
} | ||
|
||
start(options: Partial<MultiprocessOptions>) { | ||
if (options.workers === 0) { | ||
this.work() | ||
return | ||
} | ||
let processes = options.workers || cpus().length // TODO workers = -1 or undef means no workers | ||
process.on('SIGINT', this.stop).on('SIGTERM', this.stop) | ||
cluster.on('online', (wrk) => { | ||
this.emit('worker', wrk.process.pid) | ||
}) | ||
cluster.on('exit', (wrk) => { | ||
this.emit('exit', wrk.process.pid) | ||
return this.fork() | ||
}) | ||
|
||
while (processes) { | ||
processes -= 1 | ||
cluster.fork() | ||
} | ||
|
||
if (options.main) { | ||
options.main() | ||
} | ||
} | ||
|
||
stop() { | ||
if (cluster.isPrimary) { | ||
this.keepAlive = false | ||
for (const worker of Object.values(cluster.workers || {})) { | ||
if (worker) { | ||
worker.process.kill() | ||
worker.kill() | ||
} | ||
} | ||
this.emit('offline') | ||
} | ||
} | ||
|
||
fork(): void { | ||
if (this.keepAlive) { | ||
cluster.fork() | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Leverage node:cluster to spawn multiple identical child processes. | ||
* | ||
* @param work - execute inside a worker. | ||
* @param options | ||
*/ | ||
export function multiprocess(work: MultiprocessWork, options: Partial<MultiprocessOptions> = {}) { | ||
return new Multiprocess(work, options) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters