@travetto/worker 4.0.0 → 4.0.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/worker",
3
- "version": "4.0.0",
3
+ "version": "4.0.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/base": "^4.0.0",
28
+ "@travetto/base": "^4.0.2",
29
29
  "generic-pool": "^3.9.0"
30
30
  },
31
31
  "travetto": {
package/src/pool.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import os from 'node:os';
2
- import timers from 'node:timers/promises';
3
2
  import { Options, Pool, createPool } from 'generic-pool';
4
3
 
5
4
  import { Env, Util } from '@travetto/base';
@@ -78,7 +77,7 @@ export class WorkPool {
78
77
  // Listen for shutdown
79
78
  opts?.shutdown?.addEventListener('abort', async () => {
80
79
  while (pendingAcquires) {
81
- await timers.setTimeout(10);
80
+ await Util.nonBlockingTimeout(10);
82
81
  }
83
82
  await pool.drain();
84
83
  await pool.clear();
@@ -1,5 +1,3 @@
1
- import { setTimeout } from 'node:timers/promises';
2
-
3
1
  import { TimeSpan, Util } from '@travetto/base';
4
2
 
5
3
  import { Timeout } from './timeout';
@@ -19,7 +17,7 @@ export class Barrier {
19
17
  static listenForUnhandled(): Promise<never> & { cancel: () => void } {
20
18
  const uncaught = Util.resolvablePromise<never>();
21
19
  const uncaughtWithCancel: typeof uncaught & { cancel?: () => void } = uncaught;
22
- const onError = (err: Error): void => { setTimeout(1).then(() => uncaught.reject(err)); };
20
+ const onError = (err: Error): void => { Util.queueMacroTask().then(() => uncaught.reject(err)); };
23
21
  process.on('unhandledRejection', onError).on('uncaughtException', onError);
24
22
  const cancel = (): void => {
25
23
  process.off('unhandledRejection', onError).off('unhandledException', onError);
@@ -1,4 +1,4 @@
1
- import { clearTimeout } from 'node:timers';
1
+ import timers from 'node:timers/promises';
2
2
  import { TimeSpan, TimeUtil, Util } from '@travetto/base';
3
3
  import { ExecutionError } from './error';
4
4
 
@@ -7,7 +7,7 @@ import { ExecutionError } from './error';
7
7
  */
8
8
  export class Timeout extends ExecutionError {
9
9
 
10
- #id: ReturnType<typeof setTimeout> | undefined;
10
+ #timeout?: AbortController;
11
11
  #promise = Util.resolvablePromise();
12
12
  #duration: number;
13
13
 
@@ -20,10 +20,10 @@ export class Timeout extends ExecutionError {
20
20
  * Stop timeout from firing
21
21
  */
22
22
  cancel(): void {
23
- if (this.#id) {
24
- clearTimeout(this.#id);
23
+ if (this.#timeout) {
25
24
  this.#promise.resolve();
26
- this.#id = undefined;
25
+ this.#timeout.abort();
26
+ this.#timeout = undefined;
27
27
  }
28
28
  }
29
29
 
@@ -31,9 +31,11 @@ export class Timeout extends ExecutionError {
31
31
  * Wait for timeout as a promise
32
32
  */
33
33
  wait(): Promise<void> {
34
- if (!this.#id) {
35
- this.#id = setTimeout(() => this.#promise.reject(this), this.#duration);
36
- this.#id.unref();
34
+ if (!this.#timeout) {
35
+ this.#timeout = new AbortController();
36
+ timers.setTimeout(this.#duration, undefined, { ref: false, signal: this.#timeout.signal })
37
+ .then(() => this.#promise.reject(this))
38
+ .catch(() => false);
37
39
  }
38
40
  return this.#promise;
39
41
  }