@travetto/worker 6.0.0-rc.0 → 6.0.0-rc.2
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/README.md +1 -1
- package/__index__.ts +2 -2
- package/package.json +2 -2
- package/src/pool.ts +9 -9
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@ yarn add @travetto/worker
|
|
|
16
16
|
This module provides the necessary primitives for handling dependent workers. A worker can be an individual actor or could be a pool of workers. Node provides ipc (inter-process communication) functionality out of the box. This module builds upon that by providing enhanced event management, richer process management, as well as constructs for orchestrating a conversation between two processes.
|
|
17
17
|
|
|
18
18
|
## Execution Pools
|
|
19
|
-
With respect to managing multiple executions, [WorkPool](https://github.com/travetto/travetto/tree/main/module/worker/src/pool.ts#L34) is provided to allow for concurrent operation, and processing of jobs concurrently. To manage the flow of jobs, [AsyncQueue](https://github.com/travetto/travetto/tree/main/module/runtime/src/queue.ts#
|
|
19
|
+
With respect to managing multiple executions, [WorkPool](https://github.com/travetto/travetto/tree/main/module/worker/src/pool.ts#L34) is provided to allow for concurrent operation, and processing of jobs concurrently. To manage the flow of jobs, [AsyncQueue](https://github.com/travetto/travetto/tree/main/module/runtime/src/queue.ts#L4) is used to support a wide range of use cases. [AsyncQueue](https://github.com/travetto/travetto/tree/main/module/runtime/src/queue.ts#L4) allows for manual control of iteration, which is useful for event driven work loads.
|
|
20
20
|
|
|
21
21
|
## IPC Support
|
|
22
22
|
To handle communication between processes, [IpcChannel](https://github.com/travetto/travetto/tree/main/module/worker/src/ipc.ts#L9) is provided. This class abstracts the underlying IPC mechanism and provides a simple interface for sending and receiving messages. It also includes event management capabilities, allowing for easy handling of different message types. By default the class assumes it is running in a child process, but it can also be used in a parent process (by passing in the child process) to communicate with child processes.
|
package/__index__.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './src/ipc';
|
|
2
|
-
export * from './src/pool';
|
|
1
|
+
export * from './src/ipc.ts';
|
|
2
|
+
export * from './src/pool.ts';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/worker",
|
|
3
|
-
"version": "6.0.0-rc.
|
|
3
|
+
"version": "6.0.0-rc.2",
|
|
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": "^6.0.0-rc.
|
|
28
|
+
"@travetto/runtime": "^6.0.0-rc.2",
|
|
29
29
|
"generic-pool": "^3.9.0"
|
|
30
30
|
},
|
|
31
31
|
"travetto": {
|
package/src/pool.ts
CHANGED
|
@@ -37,7 +37,7 @@ export class WorkPool {
|
|
|
37
37
|
static DEFAULT_SIZE = Math.max(Math.trunc(WorkPool.MAX_SIZE * .75), 4);
|
|
38
38
|
|
|
39
39
|
/** Build worker pool */
|
|
40
|
-
static #buildPool<I, O>(
|
|
40
|
+
static #buildPool<I, O>(input: WorkerInput<I, O>, opts?: WorkPoolConfig<I, O>): Pool<Worker<I, O>> {
|
|
41
41
|
let pendingAcquires = 0;
|
|
42
42
|
|
|
43
43
|
const trace = /@travetto\/worker/.test(Env.DEBUG.val ?? '');
|
|
@@ -47,13 +47,13 @@ export class WorkPool {
|
|
|
47
47
|
async create() {
|
|
48
48
|
try {
|
|
49
49
|
pendingAcquires += 1;
|
|
50
|
-
const
|
|
50
|
+
const worker: Worker<I, O> = {
|
|
51
51
|
id: Util.uuid(),
|
|
52
52
|
active: true,
|
|
53
|
-
...isWorkerFactory(
|
|
53
|
+
...isWorkerFactory(input) ? await input() : { execute: input }
|
|
54
54
|
};
|
|
55
|
-
await
|
|
56
|
-
return
|
|
55
|
+
await worker.init?.();
|
|
56
|
+
return worker;
|
|
57
57
|
} finally {
|
|
58
58
|
pendingAcquires -= 1;
|
|
59
59
|
}
|
|
@@ -142,14 +142,14 @@ export class WorkPool {
|
|
|
142
142
|
*/
|
|
143
143
|
static runStream<I, O>(worker: WorkerInput<I, O>, input: ItrSource<I>, opts?: WorkPoolConfig<I, O>): AsyncIterable<O> {
|
|
144
144
|
const itr = new AsyncQueue<O>();
|
|
145
|
-
const
|
|
145
|
+
const result = this.run(worker, input, {
|
|
146
146
|
...opts,
|
|
147
147
|
onComplete: (ev, inp, finishIdx) => {
|
|
148
148
|
itr.add(ev);
|
|
149
149
|
opts?.onComplete?.(ev, inp, finishIdx);
|
|
150
150
|
}
|
|
151
151
|
});
|
|
152
|
-
|
|
152
|
+
result.finally(() => itr.close());
|
|
153
153
|
return itr;
|
|
154
154
|
}
|
|
155
155
|
|
|
@@ -162,14 +162,14 @@ export class WorkPool {
|
|
|
162
162
|
total: number;
|
|
163
163
|
}> {
|
|
164
164
|
const itr = new AsyncQueue<{ idx: number, value: O, total: number }>();
|
|
165
|
-
const
|
|
165
|
+
const result = this.run(worker, input, {
|
|
166
166
|
...opts,
|
|
167
167
|
onComplete: (ev, inp, finishIdx) => {
|
|
168
168
|
itr.add({ value: ev, idx: finishIdx, total });
|
|
169
169
|
opts?.onComplete?.(ev, inp, finishIdx);
|
|
170
170
|
}
|
|
171
171
|
});
|
|
172
|
-
|
|
172
|
+
result.finally(() => itr.close());
|
|
173
173
|
return itr;
|
|
174
174
|
}
|
|
175
175
|
}
|