JSONValue can not be used by custom defined interfaces (#201)
[poolifier.git] / src / pools / cluster / dynamic.ts
CommitLineData
c97c7edb 1import type { Worker } from 'cluster'
1a81f8af 2import { isKillBehavior, KillBehaviors } from '../../worker/worker-options'
c97c7edb 3import type { ClusterPoolOptions } from './fixed'
325f50bc 4import { FixedClusterPool } from './fixed'
f045358d 5
4ade5f1f 6/**
729c563d 7 * A cluster pool with a dynamic number of workers, but a guaranteed minimum number of workers.
4ade5f1f 8 *
729c563d
S
9 * This cluster pool creates new workers when the others are busy, up to the maximum number of workers.
10 * When the maximum number of workers is reached, an event is emitted. If you want to listen to this event, use the pool's `emitter`.
11 *
deb85c12
JB
12 * @template Data Type of data sent to the worker. This can only be serializable data.
13 * @template Response Type of response of execution. This can only be serializable data.
4ade5f1f 14 *
325f50bc
S
15 * @author [Christopher Quadflieg](https://github.com/Shinigami92)
16 * @since 2.0.0
4ade5f1f 17 */
325f50bc 18export class DynamicClusterPool<
deb85c12
JB
19 Data = unknown,
20 Response = unknown
325f50bc 21> extends FixedClusterPool<Data, Response> {
4ade5f1f 22 /**
729c563d
S
23 * Constructs a new poolifier dynamic cluster pool.
24 *
25 * @param min Minimum number of workers which are always active.
26 * @param max Maximum number of workers that can be created by this pool.
31b90205
JB
27 * @param filePath Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
28 * @param opts Options for this dynamic cluster pool. Default: `{ maxTasks: 1000 }`
4ade5f1f
S
29 */
30 public constructor (
c97c7edb 31 min: number,
4ade5f1f 32 public readonly max: number,
31b90205 33 filePath: string,
c97c7edb 34 opts: ClusterPoolOptions = { maxTasks: 1000 }
4ade5f1f 35 ) {
31b90205 36 super(min, filePath, opts)
4ade5f1f
S
37 }
38
729c563d
S
39 /**
40 * Choose a worker for the next task.
41 *
42 * It will first check for and return an idle worker.
43 * If all workers are busy, then it will try to create a new one up to the `max` worker count.
44 * If the max worker count is reached, the emitter will emit a `FullPool` event and it will fall back to using a round robin algorithm to distribute the load.
50eceb07
S
45 *
46 * @returns Cluster worker.
729c563d 47 */
c97c7edb 48 protected chooseWorker (): Worker {
4f7fa42a
S
49 for (const [worker, numberOfTasks] of this.tasks) {
50 if (numberOfTasks === 0) {
51 // A worker is free, use it
52 return worker
4ade5f1f
S
53 }
54 }
55
4f7fa42a
S
56 if (this.workers.length === this.max) {
57 this.emitter.emit('FullPool')
58 return super.chooseWorker()
4ade5f1f 59 }
4f7fa42a
S
60
61 // All workers are busy, create a new worker
3ec964d6 62 const workerCreated = this.createAndSetupWorker()
63 this.registerWorkerMessageListener<Data>(workerCreated, message => {
64 const tasksInProgress = this.tasks.get(workerCreated)
1a81f8af
S
65 if (
66 isKillBehavior(KillBehaviors.HARD, message.kill) ||
67 tasksInProgress === 0
68 ) {
d63d3be3 69 // Kill received from the worker, means that no new tasks are submitted to that worker for a while ( > maxInactiveTime)
3ec964d6 70 void this.destroyWorker(workerCreated)
4f7fa42a
S
71 }
72 })
3ec964d6 73 return workerCreated
4ade5f1f
S
74 }
75}