Merge pull request #161 from pioardi/issue-70
[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
S
9 /**
10 * If `lastActiveTime` is greater than `maxInactiveTime` but a task is still running, then the worker will be deleted.
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 *
23 * @template KB Which specific KillBehavior to test against.
24 * @param killBehavior Which kind of kill behavior to detect. Default: `KillBehaviors.HARD`.
25 * @param value Any value.
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.
47 * - If `killBehavior` is set to `KillBehaviors.SOFT` your tasks have no timeout and your workers will not be terminated until your task is.
325f50bc
S
48 *
49 * @default 60.000 ms
50 */
51 maxInactiveTime?: number
52 /**
729c563d 53 * Whether your worker will perform asynchronous or not.
325f50bc
S
54 *
55 * @default false
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 *
61 * - SOFT: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still running, then the worker **wont** be deleted.
62 * - HARD: If `lastActiveTime` is greater than `maxInactiveTime` but a task is still running, then the worker will be deleted.
63 *
4c35177b 64 * This option only apply to the newly created workers.
65 *
1a81f8af 66 * @default KillBehaviors.SOFT
4c35177b 67 */
fb41d7f7 68 killBehavior?: KillBehavior
325f50bc 69}