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

Flush stdout and stderr before exiting #49

Merged
merged 1 commit 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ processManager.addHook({

## Debug

Enable verbose debugging by configuring your own logger and passing it to `proccessManager.configure({ log: myCustomLogger })`.
Enable verbose debugging by configuring your own logger and passing it to `processManager.configure({ log: myCustomLogger })`.

The minimum requirements for it to work is that the logger must be Object-like and have functions assigned to properties `info`, `warn`, and `error`.
The functions should be able to handle two different argument signatures:
Expand Down
14 changes: 14 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,22 @@ class ProcessManager {

await this.hook('exit', this.errors);

this.log.info('Flushing output');

await this.flushOutput();

this.exit();
}

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,
// and wait for the write operation to complete.
await Promise.all([
new Promise(resolve => process.stdout.write('', resolve)),
new Promise(resolve => process.stderr.write('', resolve))
]);
}
}

/**
Expand Down
11 changes: 11 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ describe('ProcessManager', () => {
jest.spyOn(process, 'exit').mockImplementation(() => {});
jest.spyOn(process, 'on').mockImplementation(() => {});
jest.spyOn(console, 'error').mockImplementation(() => {});
jest.spyOn(process.stderr, 'write').mockImplementation((data, cb) => cb?.());
jest.spyOn(process.stdout, 'write').mockImplementation((data, cb) => cb?.());

const utils = require('../src/utils');

Expand Down Expand Up @@ -291,6 +293,15 @@ describe('ProcessManager', () => {
expect(processManager.hook).toHaveBeenCalledWith('exit', []);
});

test('flushes stdout and stderr', async () => {
await processManager.shutdown();

expect(process.stdout.write).toHaveBeenCalledTimes(1);
expect(process.stdout.write).toHaveBeenCalledWith('', expect.any(Function));
expect(process.stderr.write).toHaveBeenCalledTimes(1);
expect(process.stderr.write).toHaveBeenCalledWith('', expect.any(Function));
});

test('calls `processManager.exit()`', async () => {
jest.spyOn(processManager, 'exit').mockImplementation(() => {});

Expand Down
Loading