@travetto/worker 5.0.15 → 5.0.17

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2023 ArcSine Technologies
3
+ Copyright (c) 2020 ArcSine Technologies
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
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#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.
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#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#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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/worker",
3
- "version": "5.0.15",
3
+ "version": "5.0.17",
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.13",
28
+ "@travetto/runtime": "^5.0.15",
29
29
  "generic-pool": "^3.9.0"
30
30
  },
31
31
  "travetto": {
package/src/ipc.ts CHANGED
@@ -87,8 +87,6 @@ export class IpcChannel<V = unknown> {
87
87
  const complete = new Promise<void>(r => this.proc.on('close', r));
88
88
  this.proc.kill();
89
89
  await Promise.race([complete, Util.nonBlockingTimeout(1000)]);
90
- } else {
91
- this.proc.disconnect();
92
90
  }
93
91
  } catch { }
94
92
  }
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?: boolean;
13
- id?: unknown;
13
+ active: boolean;
14
+ id: unknown;
14
15
  init?(): Promise<unknown>;
15
- execute(input: I, idx: number): Promise<O>;
16
+ execute: WorkerExecutor<I, O>;
16
17
  destroy?(): Promise<void>;
17
18
  release?(): unknown;
18
19
  }
19
20
 
20
- type WorkerInput<I, O> = (() => Worker<I, O>) | ((input: I, inputIdx: number) => Promise<O>);
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 (() => Worker<I, O>) => o.length === 0;
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
@@ -32,7 +34,7 @@ const isWorkerFactory = <I, O>(o: WorkerInput<I, O>): o is (() => Worker<I, O>)
32
34
  export class WorkPool {
33
35
 
34
36
  static MAX_SIZE = os.availableParallelism();
35
- static DEFAULT_SIZE = Math.min(WorkPool.MAX_SIZE, 4);
37
+ static DEFAULT_SIZE = Math.max(Math.trunc(WorkPool.MAX_SIZE * .75), 4);
36
38
 
37
39
  /** Build worker pool */
38
40
  static #buildPool<I, O>(worker: WorkerInput<I, O>, opts?: WorkPoolConfig<I, O>): Pool<Worker<I, O>> {
@@ -45,13 +47,12 @@ export class WorkPool {
45
47
  async create() {
46
48
  try {
47
49
  pendingAcquires += 1;
48
- const res = isWorkerFactory(worker) ? await worker() : { execute: worker };
49
- res.id ??= Util.uuid();
50
-
51
- if (res.init) {
52
- await res.init();
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 ?? true
67
+ validate: async (x: Worker<I, O>) => x.active
67
68
  }, {
68
69
  evictionRunIntervalMillis: 5000,
69
70
  ...(opts ?? {}),
@@ -71,7 +72,6 @@ export class WorkPool {
71
72
  min: opts?.min ?? 1,
72
73
  });
73
74
 
74
-
75
75
  // Listen for shutdown
76
76
  opts?.shutdown?.addEventListener('abort', async () => {
77
77
  while (pendingAcquires) {
@@ -115,7 +115,7 @@ export class WorkPool {
115
115
  console.debug('Releasing', { pid: process.pid, worker: worker.id });
116
116
  }
117
117
  try {
118
- if (worker.active ?? true) {
118
+ if (worker.active) {
119
119
  try {
120
120
  await worker.release?.();
121
121
  } catch { }