From 0963529f8190e00c36e63bbd74809fb7669a6c32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Cruz?= Date: Fri, 20 Dec 2024 15:51:26 +0000 Subject: [PATCH 1/2] Fix `hook` not checking if values are errors --- src/index.js | 6 +++++- test/index.test.js | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 30d3e2a..ad411f7 100644 --- a/src/index.js +++ b/src/index.js @@ -98,7 +98,7 @@ class ProcessManager { for (const result of results) { if (result instanceof TimeoutError) { this.log.warn(`Timeout: ${result.message}`); - } else if (result) { + } else if (result instanceof Error) { this.errors.push(result); } } @@ -205,6 +205,10 @@ class ProcessManager { this.exit(); } + /** + * Wait for output to flush. + */ + async flushOutput() { // Process stdout and stderr can be in non-blocking mode so writes to it may not be flushed when the process exits. // To ensure that all output is flushed before the process exits, we can write an empty string to stdout and stderr, diff --git a/test/index.test.js b/test/index.test.js index e70ee10..dc129c0 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -238,6 +238,13 @@ describe('ProcessManager', () => { }, type }); + processManager.addHook({ + handler: () => { + // This should be ignored since it's not an Error instance. + return 'foo'; + }, + type + }); processManager.configure({ timeout: 1 }); expect(processManager.errors).toHaveLength(0); From 4ab0b0c3bddd719af9b25ee437ab28cb05986e7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Cruz?= Date: Fri, 20 Dec 2024 15:51:40 +0000 Subject: [PATCH 2/2] Add `loop` return value documentation --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 1592395..659d7a0 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,18 @@ processManager.loop(async () => { }, { interval: 600 }); ``` +You can also return an object with an `interval` property to override the next interval. + +```javascript +const processManager = require('process-manager'); + +processManager.loop(async () => { + console.log(await client.getSomeInfo()); + + return { interval: 1000 }; +}, { interval: 600 }); +``` + ### on(fn) This lifecycle is used to get a function suited for using with an event emitter. It does not exit unless something goes wrong.