d4bf30f39b3a58f7572064ca9627f434a6a77308
[poolifier.git] / src / pools / cluster / dynamic.ts
1 import { EventEmitter } from 'events'
2 import type { FixedClusterPoolOptions, WorkerWithMessageChannel } from './fixed'
3 import { FixedClusterPool } from './fixed'
4
5 class MyEmitter extends EventEmitter {}
6
7 export type DynamicClusterPoolOptions = FixedClusterPoolOptions
8
9 /**
10 * A cluster pool with a min/max number of workers, is possible to execute tasks in sync or async mode as you prefer.
11 *
12 * This cluster pool will create new workers when the other ones are busy, until the max number of workers,
13 * when the max number of workers is reached, an event will be emitted, if you want to listen this event use the emitter method.
14 *
15 * @author [Christopher Quadflieg](https://github.com/Shinigami92)
16 * @since 2.0.0
17 */
18 export class DynamicClusterPool<
19 // eslint-disable-next-line @typescript-eslint/no-explicit-any
20 Data = any,
21 // eslint-disable-next-line @typescript-eslint/no-explicit-any
22 Response = any
23 > extends FixedClusterPool<Data, Response> {
24 public readonly emitter: MyEmitter
25
26 /**
27 * @param min Min number of workers that will be always active
28 * @param max Max number of workers that will be active
29 * @param filename A file path with implementation of `ClusterWorker` class, relative path is fine.
30 * @param opts An object with possible options for example `errorHandler`, `onlineHandler`. Default: `{ maxTasks: 1000 }`
31 */
32 public constructor (
33 public readonly min: number,
34 public readonly max: number,
35 public readonly filename: string,
36 public readonly opts: DynamicClusterPoolOptions = { maxTasks: 1000 }
37 ) {
38 super(min, filename, opts)
39
40 this.emitter = new MyEmitter()
41 }
42
43 protected chooseWorker (): WorkerWithMessageChannel {
44 let worker: WorkerWithMessageChannel | undefined
45 for (const entry of this.tasks) {
46 if (entry[1] === 0) {
47 worker = entry[0]
48 break
49 }
50 }
51
52 if (worker) {
53 // a worker is free, use it
54 return worker
55 } else {
56 if (this.workers.length === this.max) {
57 this.emitter.emit('FullPool')
58 return super.chooseWorker()
59 }
60 // all workers are busy create a new worker
61 const worker = this.newWorker()
62 worker.on('message', (message: { kill?: number }) => {
63 if (message.kill) {
64 worker.send({ kill: 1 })
65 worker.kill()
66 // clean workers from data structures
67 const workerIndex = this.workers.indexOf(worker)
68 this.workers.splice(workerIndex, 1)
69 this.tasks.delete(worker)
70 }
71 })
72 return worker
73 }
74 }
75 }