docs: update changelog entries
[poolifier.git] / src / worker / worker-options.ts
CommitLineData
4c35177b 1/**
1a81f8af 2 * Enumeration of kill behaviors.
4c35177b 3 */
1a81f8af
S
4export const KillBehaviors = Object.freeze({
5 /**
6 * If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still running, then the worker **wont** be deleted.
7 */
4c35177b 8 SOFT: 'SOFT',
1a81f8af 9 /**
a9d9ea34 10 * If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still running, then the worker will be deleted.
1a81f8af 11 */
4c35177b 12 HARD: 'HARD'
1a81f8af
S
13} as const)
14
15/**
16 * Kill behavior.
17 */
18export type KillBehavior = keyof typeof KillBehaviors
19
20/**
21 * Detects whether the given value is a kill behavior or not.
22 *
38e795c1
JB
23 * @typeParam KB - Which specific KillBehavior to test against.
24 * @param killBehavior - Which kind of kill behavior to detect.
25 * @param value - Any value.
1a81f8af
S
26 * @returns `true` if `value` was strictly equals to `killBehavior`, otherwise `false`.
27 */
28export function isKillBehavior<KB extends KillBehavior> (
29 killBehavior: KB,
30 value: unknown
31): value is KB {
32 return value === killBehavior
33}
4c35177b 34
729c563d
S
35/**
36 * Options for workers.
37 */
325f50bc
S
38export interface WorkerOptions {
39 /**
729c563d 40 * Maximum waiting time in milliseconds for tasks.
1a81f8af 41 *
729c563d 42 * After this time, newly created workers will be terminated.
4c35177b 43 * The last active time of your worker unit will be updated when a task is submitted to a worker or when a worker terminate a task.
1a81f8af
S
44 *
45 * - If `killBehavior` is set to `KillBehaviors.HARD` this value represents also the timeout for the tasks that you submit to the pool,
46 * when this timeout expires your tasks is interrupted and the worker is killed if is not part of the minimum size of the pool.
15eacd5d 47 * - If `killBehavior` is set to `KillBehaviors.SOFT` your tasks have no timeout and your workers will not be terminated until your task is completed.
325f50bc 48 *
38e795c1 49 * @defaultValue 60000 ms
325f50bc
S
50 */
51 maxInactiveTime?: number
52 /**
729c563d 53 * Whether your worker will perform asynchronous or not.
325f50bc 54 *
38e795c1 55 * @defaultValue false
325f50bc
S
56 */
57 async?: boolean
4c35177b 58 /**
1a81f8af
S
59 * `killBehavior` dictates if your async unit (worker/process) will be deleted in case that a task is active on it.
60 *
31b90205 61 * - SOFT: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still running, then the worker **won't** be deleted.
a9d9ea34 62 * - HARD: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still running, then the worker will be deleted.
1a81f8af 63 *
4c35177b 64 * This option only apply to the newly created workers.
65 *
38e795c1 66 * @defaultValue KillBehaviors.SOFT
4c35177b 67 */
fb41d7f7 68 killBehavior?: KillBehavior
325f50bc 69}