@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 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/worker",
3
- "version": "5.0.15",
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.13",
28
+ "@travetto/runtime": "^5.0.14",
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
@@ -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 ?? {}),
@@ -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 ?? true) {
119
+ if (worker.active) {
119
120
  try {
120
121
  await worker.release?.();
121
122
  } catch { }