-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Workerpool.ts
310 lines (255 loc) · 7.42 KB
/
Workerpool.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import type { Class, JsonValue, Promisable, SetOptional } from "./deps.ts";
import type { Executable } from "./Executable.ts";
import { Runner, RunnerExecutionError } from "./Runner.ts";
import type { Task } from "./Task.ts";
type CallbackContext<TPayload = JsonValue, TResult = unknown> = {
task: Task<TPayload>;
runner: Runner<TPayload, TResult>;
};
export type WorkerpoolOptions<TPayload = JsonValue, TResult = unknown> = {
/**
* Worker classes implementing the Executable interface.
*/
workers: Class<Executable<TPayload, TResult>>[];
/**
* Size of the worker pool, A.K.A. poolSize.
*
* @default 10
*/
concurrency?: number;
/**
* Retries before treating a task as failed.
*
* @default 0,
*/
maximumRetries?: number;
/**
* If specified, workers will be discarded after this many successful or
* failure tasks.
*
* @default Infinity
*/
maximumTaskPerRunner?: number;
/**
* Implementation of task enqueuing.
*
* Retries will also call this method with the task object, this function
* should reset the mutex lock if available.
*/
enqueue: (task: Task<TPayload>) => Promisable<void>;
/**
* Retrieves the next pending task, this function should acquire mutex lock
* for the task.
*/
dequeue: () => Promisable<Task<TPayload> | undefined>;
/**
* Callback style task handler.
*/
onTaskFinished?: {
(
error: Error,
result: null,
context: CallbackContext<TPayload, TResult>,
): Promisable<void>;
(
error: null,
result: TResult,
context: CallbackContext<TPayload, TResult>,
): Promisable<void>;
};
/**
* Called when the state of the pool is changed.
*/
onStateChange?: (state: WorkerpoolState) => Promisable<void>;
};
/**
* 1. **running:** Workerpool becomes active via .start() or .enqueue().
* 2. **draining:** Workerpool is paused via .pause().
* 3. **drained:** All active runners are disposed via task exhaustion or pausing.
*/
export type WorkerpoolState = "running" | "draining" | "drained";
export class Workerpool<TPayload = JsonValue, TResult = unknown> {
#active = false;
#state: WorkerpoolState = "drained";
#dequeueActive = false;
#concurrency = 10;
#maximumRetries = 0;
#maximumTaskPerRunner = Infinity;
#runnerFactories = new Map<string, Class<Executable<TPayload, TResult>>>();
#runners = new Set<Runner<TPayload, TResult>>();
constructor(readonly options: WorkerpoolOptions<TPayload, TResult>) {
options.workers?.forEach((worker) => {
this.#runnerFactories.set(worker.name, worker);
});
if (options.concurrency) {
this.#concurrency = options.concurrency;
}
if (options.maximumRetries) {
this.#maximumRetries = options.maximumRetries;
}
if (options.maximumTaskPerRunner) {
this.#maximumTaskPerRunner = options.maximumTaskPerRunner;
}
}
get concurrency() {
return this.#runners.size;
}
get paused() {
return !this.#active;
}
get state() {
return this.#state;
}
set state(value: WorkerpoolState) {
if (this.#state === value) return;
this.#state = value;
this.options.onStateChange?.bind(this)(value);
}
start() {
if (this.#active) {
return this;
}
this.#active = true;
this.state = "running";
this.#startDequeue();
return this;
}
/**
* Pause further task execution.
*
* Workerpool will start draining idle runners, and fires the drained()
* callback when all runners currently active are disposed.
*/
pause(): Promisable<void> {
if (!this.#active) return;
this.#active = false;
this.state = "draining";
// Drain immediately if queue is already empty.
return this.#disposeIdleRunners();
}
/**
* @deprecated Use `pause` for elegance.
*/
stop() {
return this.pause();
}
enqueue({
executionCount = 0,
...task
}: SetOptional<Task<TPayload>, "executionCount">) {
const doEnqueue = async () => {
await this.options.enqueue({ executionCount, ...task });
// Restart dequeue if we still have concurrent capacity.
if (
this.#active &&
(this.#runners.size < this.#concurrency ||
[...this.#runners].some(({ busy }) => !busy))
) {
this.#startDequeue();
}
};
doEnqueue();
return this;
}
async #startDequeue() {
// The idea is to maintain one and only one active dequeuing chain at a time.
if (this.#dequeueActive) return;
this.#dequeueActive = true;
this.state = "running";
return await this.#dequeue();
}
async #dequeue(): Promise<void> {
if (!this.#active) {
this.#dequeueActive = false;
if (this.#state === "draining") {
return await this.#disposeIdleRunners();
} else {
return;
}
}
const task = await this.options.dequeue();
// No tasks available, mark inactive and wait for next enqueue.
if (!task) {
this.#dequeueActive = false;
// Set drained if all runners are already idle.
if ([...this.#runners].every(({ busy }) => !busy)) {
this.state = "drained";
}
return;
}
const runner = this.#getRunner(task.name);
// No runners available yet, put the task back and wait.
if (!runner) {
return await this.options.enqueue(task);
}
task.executionCount++;
runner
.execute(task.payload)
.then(
(result) =>
this.options.onTaskFinished?.(null, result, { task, runner }),
(error) => {
if (
error instanceof RunnerExecutionError &&
error.retryable &&
task.executionCount < this.#maximumRetries
) {
this.enqueue(task);
} else if (this.options.onTaskFinished) {
return this.options.onTaskFinished(error, null, { task, runner });
} else {
throw error;
}
},
)
.finally(() => {
if (runner.executionCount >= this.#maximumTaskPerRunner) {
this.#runners.delete(runner);
}
this.#dequeue();
});
this.#dequeue();
}
async #disposeIdleRunners() {
// Release idle runners
const idleRunners = [...this.#runners].filter((runner) => !runner.busy);
for (const runner of idleRunners) {
this.#runners.delete(runner);
}
await Promise.all(idleRunners.map((runner) => runner.dispose()));
if (this.#runners.size === 0) {
this.state = "drained";
}
}
#getRunner(name: string): Runner<TPayload, TResult> | undefined {
const idleRunners = [...this.#runners].filter((runner) => !runner.busy);
const runner = idleRunners.find(
({ name: runnerName }) => runnerName === name,
);
if (runner) {
return runner;
}
if (this.#runners.size < this.#concurrency) {
const executableClass = this.#runnerFactories.get(name);
if (!executableClass) {
throw new Error(`No executable is named ${name}.`);
}
const runnerInstance = new Runner<TPayload, TResult>(
new executableClass(),
executableClass.name,
);
this.#runners.add(runnerInstance);
return runnerInstance;
} else {
// Discard idle runners of other types, if available.
const idleRunner = idleRunners.find(
({ name: runnerName }) => runnerName !== name,
);
if (idleRunner) {
this.#runners.delete(idleRunner);
idleRunner.dispose();
return this.#getRunner(name);
}
}
}
}