Pool busy event emitting on all pool types (#241)
[poolifier.git] / src / pools / cluster / dynamic.ts
CommitLineData
7c0ba920 1import { PoolType } from '../pool-internal'
c97c7edb 2import type { ClusterPoolOptions } from './fixed'
325f50bc 3import { FixedClusterPool } from './fixed'
f045358d 4
4ade5f1f 5/**
729c563d 6 * A cluster pool with a dynamic number of workers, but a guaranteed minimum number of workers.
4ade5f1f 7 *
729c563d
S
8 * This cluster pool creates new workers when the others are busy, up to the maximum number of workers.
9 * 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`.
10 *
deb85c12
JB
11 * @template Data Type of data sent to the worker. This can only be serializable data.
12 * @template Response Type of response of execution. This can only be serializable data.
4ade5f1f 13 *
325f50bc
S
14 * @author [Christopher Quadflieg](https://github.com/Shinigami92)
15 * @since 2.0.0
4ade5f1f 16 */
325f50bc 17export class DynamicClusterPool<
deb85c12
JB
18 Data = unknown,
19 Response = unknown
325f50bc 20> extends FixedClusterPool<Data, Response> {
4ade5f1f 21 /**
729c563d
S
22 * Constructs a new poolifier dynamic cluster pool.
23 *
24 * @param min Minimum number of workers which are always active.
25 * @param max Maximum number of workers that can be created by this pool.
31b90205 26 * @param filePath Path to an implementation of a `ClusterWorker` file, which can be relative or absolute.
1927ee67 27 * @param opts Options for this dynamic cluster pool. Default: `{}`
4ade5f1f
S
28 */
29 public constructor (
c97c7edb 30 min: number,
4ade5f1f 31 public readonly max: number,
31b90205 32 filePath: string,
1927ee67 33 opts: ClusterPoolOptions = {}
4ade5f1f 34 ) {
31b90205 35 super(min, filePath, opts)
4ade5f1f
S
36 }
37
a35560ba 38 /** @inheritdoc */
7c0ba920
JB
39 public get type (): PoolType {
40 return PoolType.DYNAMIC
41 }
42
43 /** @inheritdoc */
44 public get busy (): boolean {
45 return this.workers.length === this.max
4ade5f1f
S
46 }
47}