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. 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);