Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hook not checking if values are errors #50

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading