docs: enhance worker options
[poolifier.git] / src / worker / worker-options.ts
1 /**
2 * Enumeration of kill behaviors.
3 */
4 export 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 */
8 SOFT: 'SOFT',
9 /**
10 * If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still running, then the worker will be deleted.
11 */
12 HARD: 'HARD'
13 } as const)
14
15 /**
16 * Kill behavior.
17 */
18 export type KillBehavior = keyof typeof KillBehaviors
19
20 /**
21 * Detects whether the given value is a kill behavior or not.
22 *
23 * @typeParam KB - Which specific KillBehavior type to test against.
24 * @param killBehavior - Which kind of kill behavior to detect.
25 * @param value - Any value.
26 * @returns `true` if `value` was strictly equals to `killBehavior`, otherwise `false`.
27 */
28 export function isKillBehavior<KB extends KillBehavior> (
29 killBehavior: KB,
30 value: unknown
31 ): value is KB {
32 return value === killBehavior
33 }
34
35 /**
36 * Options for workers.
37 */
38 export interface WorkerOptions {
39 /**
40 * Maximum waiting time in milliseconds for tasks.
41 *
42 * After this time, newly created workers will be terminated.
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.
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 completed.
48 *
49 * @defaultValue 60000
50 */
51 maxInactiveTime?: number
52 /**
53 * Whether your worker will perform asynchronous or not.
54 *
55 * @defaultValue false
56 */
57 async?: boolean
58 /**
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 **won't** be deleted.
62 * - HARD: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still running, then the worker will be deleted.
63 *
64 * This option only apply to the newly created workers.
65 *
66 * @defaultValue KillBehaviors.SOFT
67 */
68 killBehavior?: KillBehavior
69 }