@travetto/worker 5.0.15 → 5.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/package.json +2 -2
- package/src/ipc.ts +0 -2
- package/src/pool.ts +15 -14
package/LICENSE
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/worker",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.16",
|
|
4
4
|
"description": "Process management utilities, with a focus on inter-process communication",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"exec",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"directory": "module/worker"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@travetto/runtime": "^5.0.
|
|
28
|
+
"@travetto/runtime": "^5.0.14",
|
|
29
29
|
"generic-pool": "^3.9.0"
|
|
30
30
|
},
|
|
31
31
|
"travetto": {
|
package/src/ipc.ts
CHANGED
package/src/pool.ts
CHANGED
|
@@ -4,27 +4,29 @@ import { Options, Pool, createPool } from 'generic-pool';
|
|
|
4
4
|
import { Env, Util, AsyncQueue } from '@travetto/runtime';
|
|
5
5
|
|
|
6
6
|
type ItrSource<I> = Iterable<I> | AsyncIterable<I>;
|
|
7
|
+
type WorkerExecutor<I, O> = (input: I, idx: number) => Promise<O>;
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* Worker definition
|
|
10
11
|
*/
|
|
11
12
|
export interface Worker<I, O = unknown> {
|
|
12
|
-
active
|
|
13
|
-
id
|
|
13
|
+
active: boolean;
|
|
14
|
+
id: unknown;
|
|
14
15
|
init?(): Promise<unknown>;
|
|
15
|
-
execute
|
|
16
|
+
execute: WorkerExecutor<I, O>;
|
|
16
17
|
destroy?(): Promise<void>;
|
|
17
18
|
release?(): unknown;
|
|
18
19
|
}
|
|
19
20
|
|
|
20
|
-
type
|
|
21
|
+
type WorkerFactoryInput<I, O = unknown> = Partial<Worker<I, O>> & { execute: WorkerExecutor<I, O> };
|
|
22
|
+
type WorkerInput<I, O> = (() => WorkerFactoryInput<I, O>) | WorkerExecutor<I, O>;
|
|
21
23
|
type WorkPoolConfig<I, O> = Options & {
|
|
22
24
|
onComplete?: (output: O, input: I, finishIdx: number) => void;
|
|
23
25
|
onError?(ev: Error, input: I, finishIdx: number): (unknown | Promise<unknown>);
|
|
24
26
|
shutdown?: AbortSignal;
|
|
25
27
|
};
|
|
26
28
|
|
|
27
|
-
const isWorkerFactory = <I, O>(o: WorkerInput<I, O>): o is (() =>
|
|
29
|
+
const isWorkerFactory = <I, O>(o: WorkerInput<I, O>): o is (() => WorkerFactoryInput<I, O>) => o.length === 0;
|
|
28
30
|
|
|
29
31
|
/**
|
|
30
32
|
* Work pool support
|
|
@@ -45,13 +47,12 @@ export class WorkPool {
|
|
|
45
47
|
async create() {
|
|
46
48
|
try {
|
|
47
49
|
pendingAcquires += 1;
|
|
48
|
-
const res
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
50
|
+
const res: Worker<I, O> = {
|
|
51
|
+
id: Util.uuid(),
|
|
52
|
+
active: true,
|
|
53
|
+
...isWorkerFactory(worker) ? await worker() : { execute: worker }
|
|
54
|
+
};
|
|
55
|
+
await res.init?.();
|
|
55
56
|
return res;
|
|
56
57
|
} finally {
|
|
57
58
|
pendingAcquires -= 1;
|
|
@@ -63,7 +64,7 @@ export class WorkPool {
|
|
|
63
64
|
}
|
|
64
65
|
return x.destroy?.();
|
|
65
66
|
},
|
|
66
|
-
validate: async (x: Worker<I, O>) => x.active
|
|
67
|
+
validate: async (x: Worker<I, O>) => x.active
|
|
67
68
|
}, {
|
|
68
69
|
evictionRunIntervalMillis: 5000,
|
|
69
70
|
...(opts ?? {}),
|
|
@@ -115,7 +116,7 @@ export class WorkPool {
|
|
|
115
116
|
console.debug('Releasing', { pid: process.pid, worker: worker.id });
|
|
116
117
|
}
|
|
117
118
|
try {
|
|
118
|
-
if (worker.active
|
|
119
|
+
if (worker.active) {
|
|
119
120
|
try {
|
|
120
121
|
await worker.release?.();
|
|
121
122
|
} catch { }
|