fix: fix task wait time computation
[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 /**
1c6fe997 6 * If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing, then the worker **wont** be deleted.
1a81f8af 7 */
4c35177b 8 SOFT: 'SOFT',
1a81f8af 9 /**
1c6fe997 10 * If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing, 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 *
d480d708 23 * @typeParam KB - Which specific KillBehavior type to test against.
38e795c1
JB
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 */
0e2c24b4 28export const isKillBehavior = <KB extends KillBehavior>(
1a81f8af
S
29 killBehavior: KB,
30 value: unknown
0e2c24b4 31): value is KB => {
1a81f8af
S
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 *
1ff6b9de 49 * @defaultValue 60000
325f50bc
S
50 */
51 maxInactiveTime?: number
52 /**
729c563d 53 * Whether your worker will perform asynchronous or not.
325f50bc 54 *
38e795c1 55 * @defaultValue false
a86b6df1 56 * @deprecated This option will be removed in the next major version.
325f50bc
S
57 */
58 async?: boolean
4c35177b 59 /**
1a81f8af
S
60 * `killBehavior` dictates if your async unit (worker/process) will be deleted in case that a task is active on it.
61 *
1c6fe997
JB
62 * - SOFT: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing, then the worker **won't** be deleted.
63 * - HARD: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still executing, then the worker will be deleted.
1a81f8af 64 *
4c35177b 65 * This option only apply to the newly created workers.
66 *
38e795c1 67 * @defaultValue KillBehaviors.SOFT
4c35177b 68 */
fb41d7f7 69 killBehavior?: KillBehavior
325f50bc 70}