Commit | Line | Data |
---|---|---|
cf9aa6c3 | 1 | declare module 'poolifier' { |
2 | import { AsyncResource } from 'async_hooks' | |
3 | import { EventEmitter } from 'events' | |
4 | import { Worker } from 'worker_threads' | |
06b0522e CQ |
5 | |
6 | export interface FixedThreadPoolOptions { | |
7 | /** | |
8 | * A function that will listen for error event on each worker thread. | |
9 | */ | |
cf9aa6c3 | 10 | errorHandler?: (this: Worker, e: Error) => void |
06b0522e CQ |
11 | /** |
12 | * A function that will listen for online event on each worker thread. | |
13 | */ | |
cf9aa6c3 | 14 | onlineHandler?: (this: Worker) => void |
06b0522e CQ |
15 | /** |
16 | * A function that will listen for exit event on each worker thread. | |
17 | */ | |
cf9aa6c3 | 18 | exitHandler?: (this: Worker, code: number) => void |
06b0522e CQ |
19 | /** |
20 | * This is just to avoid not useful warnings message, is used to set `maxListeners` on event emitters (workers are event emitters). | |
21 | * | |
22 | * @default 1000 | |
23 | */ | |
cf9aa6c3 | 24 | maxTasks?: number |
06b0522e CQ |
25 | } |
26 | ||
27 | /** | |
28 | * A thread pool with a static number of threads, is possible to execute tasks in sync or async mode as you prefer. | |
29 | * | |
30 | * This pool will select the worker thread in a round robin fashion. | |
31 | * | |
32 | * @author [Alessandro Pio Ardizio](https://github.com/pioardi) | |
33 | * @since 0.0.1 | |
34 | */ | |
35 | export class FixedThreadPool<Data = any, Response = any> { | |
36 | /** | |
37 | * Num of threads for this worker pool. | |
38 | */ | |
cf9aa6c3 | 39 | numThreads: number |
40 | workers: Worker[] | |
41 | nextWorker: number | |
42 | opts: FixedThreadPoolOptions | |
43 | filePath: string | |
44 | tasks: Map<Worker, number> | |
06b0522e | 45 | |
cf9aa6c3 | 46 | _id: number |
06b0522e CQ |
47 | |
48 | /** | |
49 | * @param numThreads Num of threads for this worker pool. | |
50 | * @param filePath A file path with implementation of `ThreadWorker` class, relative path is fine. | |
51 | * @param opts An object with possible options for example `errorHandler`, `onlineHandler`. Default: `{ maxTasks: 1000 }` | |
52 | */ | |
cf9aa6c3 | 53 | constructor ( |
06b0522e CQ |
54 | numThreads: number, |
55 | filePath: string, | |
56 | opts?: FixedThreadPoolOptions | |
cf9aa6c3 | 57 | ) |
06b0522e | 58 | |
cf9aa6c3 | 59 | destroy (): Promise<void> |
06b0522e CQ |
60 | |
61 | /** | |
62 | * Execute the task specified into the constructor with the data parameter. | |
63 | * | |
64 | * @param data The input for the task specified. | |
65 | * @returns Promise that is resolved when the task is done. | |
66 | */ | |
cf9aa6c3 | 67 | execute (data: Data): Promise<Response> |
06b0522e | 68 | |
cf9aa6c3 | 69 | _execute (worker: Worker, id: number): Promise<unknown> |
70 | _chooseWorker (): Worker | |
71 | _newWorker (): Worker | |
06b0522e CQ |
72 | } |
73 | ||
cf9aa6c3 | 74 | export type DynamicThreadPoolOptions = FixedThreadPoolOptions |
06b0522e CQ |
75 | |
76 | class MyEmitter extends EventEmitter {} | |
77 | ||
78 | /** | |
79 | * A thread pool with a min/max number of threads, is possible to execute tasks in sync or async mode as you prefer. | |
80 | * | |
81 | * This thread pool will create new workers when the other ones are busy, until the max number of threads, | |
82 | * when the max number of threads is reached, an event will be emitted, if you want to listen this event use the emitter method. | |
83 | * | |
84 | * @author [Alessandro Pio Ardizio](https://github.com/pioardi) | |
85 | * @since 0.0.1 | |
86 | */ | |
87 | export class DynamicThreadPool< | |
88 | Data = any, | |
89 | Response = any | |
90 | > extends FixedThreadPool<Data, Response> { | |
cf9aa6c3 | 91 | max: number |
92 | emitter: MyEmitter | |
06b0522e CQ |
93 | |
94 | /** | |
95 | * @param min Min number of threads that will be always active | |
96 | * @param max Max number of threads that will be active | |
97 | * @param filename A file path with implementation of `ThreadWorker` class, relative path is fine. | |
98 | * @param opts An object with possible options for example `errorHandler`, `onlineHandler`. Default: `{ maxTasks: 1000 }` | |
99 | */ | |
cf9aa6c3 | 100 | constructor ( |
06b0522e CQ |
101 | min: number, |
102 | max: number, | |
103 | filename: string, | |
104 | opts?: DynamicThreadPoolOptions | |
cf9aa6c3 | 105 | ) |
06b0522e CQ |
106 | } |
107 | ||
108 | export interface ThreadWorkerOptions { | |
109 | /** | |
110 | * Max time to wait tasks to work on (in ms), after this period the new worker threads will die. | |
111 | * | |
112 | * @default 60.000 ms | |
113 | */ | |
cf9aa6c3 | 114 | maxInactiveTime?: number |
06b0522e CQ |
115 | /** |
116 | * `true` if your function contains async pieces, else `false`. | |
117 | * | |
118 | * @default false | |
119 | */ | |
cf9aa6c3 | 120 | async?: boolean |
06b0522e CQ |
121 | } |
122 | ||
123 | /** | |
124 | * An example worker that will be always alive, you just need to **extend** this class if you want a static pool. | |
125 | * | |
126 | * When this worker is inactive for more than 1 minute, it will send this info to the main thread, | |
127 | * if you are using DynamicThreadPool, the workers created after will be killed, the min num of thread will be guaranteed. | |
128 | * | |
129 | * @author [Alessandro Pio Ardizio](https://github.com/pioardi) | |
130 | * @since 0.0.1 | |
131 | */ | |
132 | export class ThreadWorker<Data = any, Response = any> extends AsyncResource { | |
cf9aa6c3 | 133 | opts: ThreadWorkerOptions |
134 | maxInactiveTime: number | |
135 | async: boolean | |
136 | lastTask: number | |
137 | interval: number | |
138 | parent: Worker | |
06b0522e | 139 | |
cf9aa6c3 | 140 | constructor (fn: (data: Data) => Response, opts?: ThreadWorkerOptions) |
06b0522e | 141 | |
cf9aa6c3 | 142 | _checkAlive (): boolean |
143 | _run (fn: (data: Data) => Response, value: { data: Data }): void | |
144 | _runAsync ( | |
06b0522e CQ |
145 | fn: (data: Data) => Promise<Response>, |
146 | value: { data: Data } | |
cf9aa6c3 | 147 | ): void |
06b0522e CQ |
148 | } |
149 | } |