@travetto/worker 5.0.11 → 5.0.13
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 -5
- package/package.json +1 -1
- package/src/ipc.ts +106 -0
- package/src/ipc/channel.ts +0 -107
- package/src/ipc/child.ts +0 -10
- package/src/ipc/parent.ts +0 -28
- package/src/ipc/types.ts +0 -9
package/README.md
CHANGED
|
@@ -19,4 +19,4 @@ This module provides the necessary primitives for handling dependent workers. A
|
|
|
19
19
|
With respect to managing multiple executions, [WorkPool](https://github.com/travetto/travetto/tree/main/module/worker/src/pool.ts#L32) 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#L6) is used to support a wide range of use cases. [AsyncQueue](https://github.com/travetto/travetto/tree/main/module/runtime/src/queue.ts#L6) 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#L8) 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
package/package.json
CHANGED
package/src/ipc.ts
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { ChildProcess } from 'node:child_process';
|
|
2
|
+
import { EventEmitter } from 'node:events';
|
|
3
|
+
|
|
4
|
+
import { ShutdownManager, Util } from '@travetto/runtime';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Channel that represents ipc communication
|
|
8
|
+
*/
|
|
9
|
+
export class IpcChannel<V = unknown> {
|
|
10
|
+
|
|
11
|
+
#emitter = new EventEmitter();
|
|
12
|
+
proc: NodeJS.Process | ChildProcess;
|
|
13
|
+
parentId: number;
|
|
14
|
+
|
|
15
|
+
constructor(proc: NodeJS.Process | ChildProcess = process) {
|
|
16
|
+
this.proc = proc;
|
|
17
|
+
this.parentId = proc instanceof ChildProcess ? process.pid : process.ppid;
|
|
18
|
+
|
|
19
|
+
// Close on shutdown
|
|
20
|
+
ShutdownManager.onGracefulShutdown(() => this.destroy(), this);
|
|
21
|
+
|
|
22
|
+
this.proc.on('message', (ev: { type: string }) => {
|
|
23
|
+
console.debug('Received', { pid: this.parentId, id: this.id, type: ev.type });
|
|
24
|
+
this.#emitter.emit(ev.type, ev);
|
|
25
|
+
this.#emitter.emit('*', ev);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Gets channel unique identifier
|
|
31
|
+
*/
|
|
32
|
+
get id(): number | undefined {
|
|
33
|
+
return this.proc.pid;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Determines if channel is active
|
|
38
|
+
*/
|
|
39
|
+
get active(): boolean {
|
|
40
|
+
return (this.proc instanceof ChildProcess) ? !this.proc.killed : !!this.proc.connected;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Send data to the parent
|
|
45
|
+
*/
|
|
46
|
+
send(eventType: string, data?: Record<string, unknown>): void {
|
|
47
|
+
console.debug('Sending', { pid: this.parentId, id: this.id, eventType });
|
|
48
|
+
if (!this.active) {
|
|
49
|
+
throw new Error('Cannot send message to inactive process');
|
|
50
|
+
} else if (this.proc.send && this.proc.connected) {
|
|
51
|
+
this.proc.send({ ...(data ?? {}), type: eventType }, (err) => err && console.error(err));
|
|
52
|
+
} else {
|
|
53
|
+
throw new Error('this.proc.send was not defined');
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Listen for a specific message type
|
|
59
|
+
*/
|
|
60
|
+
on(eventType: string, callback: (e: V & { type: string }) => unknown | void): () => void {
|
|
61
|
+
this.#emitter.on(eventType, callback);
|
|
62
|
+
return () => this.off(eventType, callback);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Remove event listener
|
|
67
|
+
*/
|
|
68
|
+
off(eventType: string, callback: (e: V & { type: string }) => unknown | void): void {
|
|
69
|
+
this.#emitter.off(eventType, callback);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Listen for a specific message type, once
|
|
74
|
+
*/
|
|
75
|
+
once(eventType: string): Promise<V & { type: string }> {
|
|
76
|
+
return new Promise(res => this.#emitter.once(eventType, res));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Destroy self
|
|
81
|
+
*/
|
|
82
|
+
async destroy(): Promise<void> {
|
|
83
|
+
if (this.active) {
|
|
84
|
+
try {
|
|
85
|
+
console.debug('Killing', { pid: this.parentId, id: this.id });
|
|
86
|
+
if (this.proc instanceof ChildProcess) {
|
|
87
|
+
const complete = new Promise<void>(r => this.proc.on('close', r));
|
|
88
|
+
this.proc.kill();
|
|
89
|
+
await Promise.race([complete, Util.nonBlockingTimeout(1000)]);
|
|
90
|
+
} else {
|
|
91
|
+
this.proc.disconnect();
|
|
92
|
+
}
|
|
93
|
+
} catch { }
|
|
94
|
+
}
|
|
95
|
+
this.release();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Remove all listeners, but do not destroy
|
|
100
|
+
*/
|
|
101
|
+
release(): void {
|
|
102
|
+
console.debug('Released', { pid: this.parentId, id: this.id });
|
|
103
|
+
this.proc.removeAllListeners();
|
|
104
|
+
this.#emitter.removeAllListeners();
|
|
105
|
+
}
|
|
106
|
+
}
|
package/src/ipc/channel.ts
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
import { ChildProcess } from 'node:child_process';
|
|
2
|
-
import { EventEmitter } from 'node:events';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Channel that represents communication between parent/child
|
|
6
|
-
*/
|
|
7
|
-
export class ProcessCommChannel<T extends NodeJS.Process | ChildProcess, V = unknown, U extends { type: string } = V & { type: string }> {
|
|
8
|
-
|
|
9
|
-
#proc: T | undefined;
|
|
10
|
-
#emitter = new EventEmitter();
|
|
11
|
-
|
|
12
|
-
constructor(proc: T) {
|
|
13
|
-
this.#proc = proc;
|
|
14
|
-
console.debug('Constructed Execution', { pid: this.id });
|
|
15
|
-
this.#proc.on('message', this.#handleMessage.bind(this));
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
get #parentId(): number {
|
|
19
|
-
return process.pid;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
#handleMessage(ev: { type: string }): void {
|
|
23
|
-
console.debug('Received', { pid: this.#parentId, id: this.id, type: ev.type });
|
|
24
|
-
this.#emitter.emit(ev.type, ev);
|
|
25
|
-
this.#emitter.emit('*', ev);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
get proc(): T | undefined {
|
|
29
|
-
return this.#proc;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
set proc(proc: T | undefined) {
|
|
33
|
-
this.#proc = proc;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Gets channel unique identifier
|
|
38
|
-
*/
|
|
39
|
-
get id(): number | undefined {
|
|
40
|
-
return this.#proc && this.#proc.pid;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Determines if channel is active
|
|
45
|
-
*/
|
|
46
|
-
get active(): boolean {
|
|
47
|
-
return !!this.#proc;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Send data to the parent
|
|
52
|
-
*/
|
|
53
|
-
send(eventType: string, data?: Record<string, unknown>): void {
|
|
54
|
-
console.debug('Sending', { pid: this.#parentId, id: this.id, eventType });
|
|
55
|
-
if (!this.#proc) {
|
|
56
|
-
throw new Error('this.proc was not defined');
|
|
57
|
-
} else if (this.#proc.send && this.#proc.connected) {
|
|
58
|
-
this.#proc.send({ ...(data ?? {}), type: eventType }, (err) => err && console.error(err));
|
|
59
|
-
} else {
|
|
60
|
-
throw new Error('this.proc.send was not defined');
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Listen for a specific message type
|
|
66
|
-
*/
|
|
67
|
-
on(eventType: string, callback: (e: U) => unknown | void): () => void {
|
|
68
|
-
this.#emitter.on(eventType, callback);
|
|
69
|
-
return () => this.off(eventType, callback);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Remove event listener
|
|
74
|
-
*/
|
|
75
|
-
off(eventType: string, callback: (e: U) => unknown | void): void {
|
|
76
|
-
this.#emitter.off(eventType, callback);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Listen for a specific message type, once
|
|
81
|
-
*/
|
|
82
|
-
once(eventType: string): Promise<U> {
|
|
83
|
-
return new Promise<U>(res => this.#emitter.once(eventType, res));
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Destroy self
|
|
88
|
-
*/
|
|
89
|
-
async destroy(): Promise<void> {
|
|
90
|
-
if (this.#proc) {
|
|
91
|
-
console.debug('Killing', { pid: this.#parentId, id: this.id });
|
|
92
|
-
if (!('argv' in this.#proc)) {
|
|
93
|
-
this.#proc.kill();
|
|
94
|
-
}
|
|
95
|
-
this.#proc = undefined;
|
|
96
|
-
}
|
|
97
|
-
this.release();
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Remove all listeners, but do not destroy
|
|
102
|
-
*/
|
|
103
|
-
release(): void {
|
|
104
|
-
console.debug('Released', { pid: this.#parentId, id: this.id });
|
|
105
|
-
this.#emitter.removeAllListeners();
|
|
106
|
-
}
|
|
107
|
-
}
|
package/src/ipc/child.ts
DELETED
package/src/ipc/parent.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { ChildProcess } from 'node:child_process';
|
|
2
|
-
|
|
3
|
-
import { ShutdownManager } from '@travetto/runtime';
|
|
4
|
-
|
|
5
|
-
import { ProcessCommChannel } from './channel';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Parent channel
|
|
9
|
-
*/
|
|
10
|
-
export class ParentCommChannel<U = unknown> extends ProcessCommChannel<ChildProcess, U> {
|
|
11
|
-
|
|
12
|
-
#complete: Promise<void>;
|
|
13
|
-
|
|
14
|
-
constructor(proc: ChildProcess) {
|
|
15
|
-
super(proc);
|
|
16
|
-
ShutdownManager.onGracefulShutdown(() => this.destroy(), this);
|
|
17
|
-
this.#complete = new Promise<void>(r => proc.on('close', r)).finally(() => { this.proc = undefined; });
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Kill self and child
|
|
22
|
-
*/
|
|
23
|
-
override async destroy(): Promise<void> {
|
|
24
|
-
const res = super.destroy();
|
|
25
|
-
await this.#complete;
|
|
26
|
-
return await res;
|
|
27
|
-
}
|
|
28
|
-
}
|