@travetto/worker 7.0.0-rc.1 → 7.0.0-rc.3

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/__index__.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export * from './src/ipc.ts';
2
- export * from './src/pool.ts';
2
+ export * from './src/pool.ts';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/worker",
3
- "version": "7.0.0-rc.1",
3
+ "version": "7.0.0-rc.3",
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": "^7.0.0-rc.1",
28
+ "@travetto/runtime": "^7.0.0-rc.3",
29
29
  "generic-pool": "^3.9.0"
30
30
  },
31
31
  "travetto": {
package/src/ipc.ts CHANGED
@@ -9,20 +9,20 @@ import { ShutdownManager, Util } from '@travetto/runtime';
9
9
  export class IpcChannel<V = unknown> {
10
10
 
11
11
  #emitter = new EventEmitter();
12
- proc: NodeJS.Process | ChildProcess;
12
+ subProcess: NodeJS.Process | ChildProcess;
13
13
  parentId: number;
14
14
 
15
- constructor(proc: NodeJS.Process | ChildProcess = process) {
16
- this.proc = proc;
17
- this.parentId = proc instanceof ChildProcess ? process.pid : process.ppid;
15
+ constructor(subProcess: NodeJS.Process | ChildProcess = process) {
16
+ this.subProcess = subProcess;
17
+ this.parentId = subProcess instanceof ChildProcess ? process.pid : process.ppid;
18
18
 
19
19
  // Close on shutdown
20
20
  ShutdownManager.onGracefulShutdown(() => this.destroy());
21
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);
22
+ this.subProcess.on('message', (event: { type: string }) => {
23
+ console.debug('Received', { pid: this.parentId, id: this.id, type: event.type });
24
+ this.#emitter.emit(event.type, event);
25
+ this.#emitter.emit('*', event);
26
26
  });
27
27
  }
28
28
 
@@ -30,14 +30,14 @@ export class IpcChannel<V = unknown> {
30
30
  * Gets channel unique identifier
31
31
  */
32
32
  get id(): number | undefined {
33
- return this.proc.pid;
33
+ return this.subProcess.pid;
34
34
  }
35
35
 
36
36
  /**
37
37
  * Determines if channel is active
38
38
  */
39
39
  get active(): boolean {
40
- return (this.proc instanceof ChildProcess) ? !this.proc.killed : !!this.proc.connected;
40
+ return (this.subProcess instanceof ChildProcess) ? !this.subProcess.killed : !!this.subProcess.connected;
41
41
  }
42
42
 
43
43
  /**
@@ -47,17 +47,17 @@ export class IpcChannel<V = unknown> {
47
47
  console.debug('Sending', { pid: this.parentId, id: this.id, eventType });
48
48
  if (!this.active) {
49
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 }, undefined, undefined, (err) => err && console.error(err));
50
+ } else if (this.subProcess.send && this.subProcess.connected) {
51
+ this.subProcess.send({ ...(data ?? {}), type: eventType }, undefined, undefined, (error) => error && console.error(error));
52
52
  } else {
53
- throw new Error('this.proc.send was not defined');
53
+ throw new Error('this.subProcess.send was not defined');
54
54
  }
55
55
  }
56
56
 
57
57
  /**
58
58
  * Listen for a specific message type
59
59
  */
60
- on(eventType: string, callback: (e: V & { type: string }) => unknown | void): () => void {
60
+ on(eventType: string, callback: (event: V & { type: string }) => unknown | void): () => void {
61
61
  this.#emitter.on(eventType, callback);
62
62
  return () => this.off(eventType, callback);
63
63
  }
@@ -65,7 +65,7 @@ export class IpcChannel<V = unknown> {
65
65
  /**
66
66
  * Remove event listener
67
67
  */
68
- off(eventType: string, callback: (e: V & { type: string }) => unknown | void): void {
68
+ off(eventType: string, callback: (event: V & { type: string }) => unknown | void): void {
69
69
  this.#emitter.off(eventType, callback);
70
70
  }
71
71
 
@@ -73,7 +73,7 @@ export class IpcChannel<V = unknown> {
73
73
  * Listen for a specific message type, once
74
74
  */
75
75
  once(eventType: string): Promise<V & { type: string }> {
76
- return new Promise(res => this.#emitter.once(eventType, res));
76
+ return new Promise(resolve => this.#emitter.once(eventType, resolve));
77
77
  }
78
78
 
79
79
  /**
@@ -83,9 +83,9 @@ export class IpcChannel<V = unknown> {
83
83
  if (this.active) {
84
84
  try {
85
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();
86
+ if (this.subProcess instanceof ChildProcess) {
87
+ const complete = new Promise<void>(resolve => this.subProcess.on('close', resolve));
88
+ this.subProcess.kill();
89
89
  await Promise.race([complete, Util.nonBlockingTimeout(1000)]);
90
90
  }
91
91
  } catch { }
@@ -98,7 +98,7 @@ export class IpcChannel<V = unknown> {
98
98
  */
99
99
  release(): void {
100
100
  console.debug('Released', { pid: this.parentId, id: this.id });
101
- this.proc.removeAllListeners();
101
+ this.subProcess.removeAllListeners();
102
102
  this.#emitter.removeAllListeners();
103
103
  }
104
104
  }
package/src/pool.ts CHANGED
@@ -3,7 +3,7 @@ import { Options, Pool, createPool } from 'generic-pool';
3
3
 
4
4
  import { Env, Util, AsyncQueue } from '@travetto/runtime';
5
5
 
6
- type ItrSource<I> = Iterable<I> | AsyncIterable<I>;
6
+ type IterableSource<I> = Iterable<I> | AsyncIterable<I>;
7
7
  type WorkerExecutor<I, O> = (input: I, idx: number) => Promise<O>;
8
8
 
9
9
  /**
@@ -22,11 +22,11 @@ type WorkerFactoryInput<I, O = unknown> = Partial<Worker<I, O>> & { execute: Wor
22
22
  type WorkerInput<I, O> = (() => WorkerFactoryInput<I, O>) | WorkerExecutor<I, O>;
23
23
  type WorkPoolConfig<I, O> = Options & {
24
24
  onComplete?: (output: O, input: I, finishIdx: number) => void;
25
- onError?(ev: Error, input: I, finishIdx: number): (unknown | Promise<unknown>);
25
+ onError?(event: Error, input: I, finishIdx: number): (unknown | Promise<unknown>);
26
26
  shutdown?: AbortSignal;
27
27
  };
28
28
 
29
- const isWorkerFactory = <I, O>(o: WorkerInput<I, O>): o is (() => WorkerFactoryInput<I, O>) => o.length === 0;
29
+ const isWorkerFactory = <I, O>(value: WorkerInput<I, O>): value is (() => WorkerFactoryInput<I, O>) => value.length === 0;
30
30
 
31
31
  /**
32
32
  * Work pool support
@@ -37,10 +37,10 @@ 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>(input: WorkerInput<I, O>, opts?: WorkPoolConfig<I, O>): Pool<Worker<I, O>> {
40
+ static #buildPool<I, O>(input: WorkerInput<I, O>, options?: WorkPoolConfig<I, O>): Pool<Worker<I, O>> {
41
41
  let pendingAcquires = 0;
42
42
 
43
- const trace = /@travetto\/worker/.test(Env.DEBUG.val ?? '');
43
+ const trace = /@travetto\/worker/.test(Env.DEBUG.value ?? '');
44
44
 
45
45
  // Create the pool
46
46
  const pool = createPool({
@@ -58,22 +58,22 @@ export class WorkPool {
58
58
  pendingAcquires -= 1;
59
59
  }
60
60
  },
61
- async destroy(x) {
61
+ async destroy(worker) {
62
62
  if (trace) {
63
- console.debug('Destroying', { pid: process.pid, worker: x.id });
63
+ console.debug('Destroying', { pid: process.pid, worker: worker.id });
64
64
  }
65
- return x.destroy?.();
65
+ return worker.destroy?.();
66
66
  },
67
- validate: async (x: Worker<I, O>) => x.active
67
+ validate: async (worker: Worker<I, O>) => worker.active
68
68
  }, {
69
69
  evictionRunIntervalMillis: 5000,
70
- ...(opts ?? {}),
71
- max: opts?.max ?? WorkPool.DEFAULT_SIZE,
72
- min: opts?.min ?? 1,
70
+ ...(options ?? {}),
71
+ max: options?.max ?? WorkPool.DEFAULT_SIZE,
72
+ min: options?.min ?? 1,
73
73
  });
74
74
 
75
75
  // Listen for shutdown
76
- opts?.shutdown?.addEventListener('abort', async () => {
76
+ options?.shutdown?.addEventListener('abort', async () => {
77
77
  while (pendingAcquires) {
78
78
  await Util.nonBlockingTimeout(10);
79
79
  }
@@ -87,17 +87,17 @@ export class WorkPool {
87
87
  /**
88
88
  * Process a given input source and worker, and fire on completion
89
89
  */
90
- static async run<I, O>(workerFactory: WorkerInput<I, O>, src: ItrSource<I>, opts: WorkPoolConfig<I, O> = {}): Promise<void> {
90
+ static async run<I, O>(workerFactory: WorkerInput<I, O>, source: IterableSource<I>, options: WorkPoolConfig<I, O> = {}): Promise<void> {
91
91
 
92
- const trace = /@travetto\/worker/.test(Env.DEBUG.val ?? '');
92
+ const trace = /@travetto\/worker/.test(Env.DEBUG.value ?? '');
93
93
  const pending = new Set<Promise<unknown>>();
94
94
  const errors: Error[] = [];
95
95
  let inputIdx = 0;
96
96
  let finishIdx = 0;
97
97
 
98
- const pool = this.#buildPool(workerFactory, opts);
98
+ const pool = this.#buildPool(workerFactory, options);
99
99
 
100
- for await (const nextInput of src) {
100
+ for await (const nextInput of source) {
101
101
  const worker = await pool.acquire()!;
102
102
 
103
103
  if (trace) {
@@ -105,10 +105,10 @@ export class WorkPool {
105
105
  }
106
106
 
107
107
  const completion = worker.execute(nextInput, inputIdx += 1)
108
- .then(v => opts.onComplete?.(v, nextInput, finishIdx += 1))
109
- .catch(err => {
110
- errors.push(err);
111
- opts?.onError?.(err, nextInput, finishIdx += 1);
108
+ .then(output => options.onComplete?.(output, nextInput, finishIdx += 1))
109
+ .catch(error => {
110
+ errors.push(error);
111
+ options?.onError?.(error, nextInput, finishIdx += 1);
112
112
  }) // Catch error
113
113
  .finally(async () => {
114
114
  if (trace) {
@@ -140,36 +140,36 @@ export class WorkPool {
140
140
  /**
141
141
  * Process a given input source as an async iterable
142
142
  */
143
- static runStream<I, O>(worker: WorkerInput<I, O>, input: ItrSource<I>, opts?: WorkPoolConfig<I, O>): AsyncIterable<O> {
144
- const itr = new AsyncQueue<O>();
143
+ static runStream<I, O>(worker: WorkerInput<I, O>, input: IterableSource<I>, options?: WorkPoolConfig<I, O>): AsyncIterable<O> {
144
+ const queue = new AsyncQueue<O>();
145
145
  const result = this.run(worker, input, {
146
- ...opts,
147
- onComplete: (ev, inp, finishIdx) => {
148
- itr.add(ev);
149
- opts?.onComplete?.(ev, inp, finishIdx);
146
+ ...options,
147
+ onComplete: (event, value, finishIdx) => {
148
+ queue.add(event);
149
+ options?.onComplete?.(event, value, finishIdx);
150
150
  }
151
151
  });
152
- result.finally(() => itr.close());
153
- return itr;
152
+ result.finally(() => queue.close());
153
+ return queue;
154
154
  }
155
155
 
156
156
  /**
157
157
  * Process a given input source as an async iterable with progress information
158
158
  */
159
- static runStreamProgress<I, O>(worker: WorkerInput<I, O>, input: ItrSource<I>, total: number, opts?: WorkPoolConfig<I, O>): AsyncIterable<{
159
+ static runStreamProgress<I, O>(worker: WorkerInput<I, O>, input: IterableSource<I>, total: number, options?: WorkPoolConfig<I, O>): AsyncIterable<{
160
160
  idx: number;
161
161
  value: O;
162
162
  total: number;
163
163
  }> {
164
- const itr = new AsyncQueue<{ idx: number, value: O, total: number }>();
164
+ const queue = new AsyncQueue<{ idx: number, value: O, total: number }>();
165
165
  const result = this.run(worker, input, {
166
- ...opts,
167
- onComplete: (ev, inp, finishIdx) => {
168
- itr.add({ value: ev, idx: finishIdx, total });
169
- opts?.onComplete?.(ev, inp, finishIdx);
166
+ ...options,
167
+ onComplete: (event, value, finishIdx) => {
168
+ queue.add({ value: event, idx: finishIdx, total });
169
+ options?.onComplete?.(event, value, finishIdx);
170
170
  }
171
171
  });
172
- result.finally(() => itr.close());
173
- return itr;
172
+ result.finally(() => queue.close());
173
+ return queue;
174
174
  }
175
175
  }