Skip to content

Commit

Permalink
Add shutdown() and bedrock.stopped event.
Browse files Browse the repository at this point in the history
The `shutdown()` call performs an orderly shutdown and emits events.
This differs from the current `exit()` and `process.exit()` behavior
which is more immediate.
  • Loading branch information
davidlehn committed Oct 6, 2023
1 parent f231f49 commit 61cb5ad
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
## 6.1.0 - 2023-xx-xx

### Added
- Add documentation for `bedrock.stop` and `bedrock.exit` events.
- Add `bedrock.shutdown()` call that performs an orderly shutdown and emits
various events. This differs from `exit()` and `process.exit()` which are
more immediate.
- Add `bedrock.stopped` event, emitted before `bedrock.exit`.
- Add documentation for `bedrock.stop`, `bedrock.stopped`, and `bedrock.exit`
events.

### Changed
- Deprecate undocumented `bedrock-cli.exit` event. Use `bedrock.exit`. If there
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,10 @@ event or causes the application to exit early.
the event modules should use to stop services for a clean shutdown and to
emit any custom events they would like to make available to their
dependents.
- **bedrock.stopped**
- Emitted while exiting, and after `bedrock.stop` if process was previously
in a started state. External access to web services or other features
provided by modules should now be unavailable.
- **bedrock.exit**
- Emitted immediately before exiting.

Expand Down
32 changes: 22 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,24 @@ export async function runOnce(id, fn, options = {}) {
}

/**
* Called from a worker to exit gracefully and without an error code. Typically
* used by subcommands. Use `process.exit(1)` (or other error code) to exit
* with an error code.
* Called from a worker to exit immediately and without an error code.
* Typically used by subcommands. Use `process.exit(1)` (or other error code)
* to exit with an error code. Use `shutdown` to perform an orderly exit.
*/
export function exit() {
cluster.worker.kill();
}

/**
* Called from a worker to exit gracefully and without an error code. Typically
* used by subcommands. Use `process.exit(1)` (or other error code) to exit
* with an error code. Use `exit()` to exit immediately. This call will emit
* events for an orderly shutdown.
*/
export async function shutdown() {
await _exit();
}

async function _waitForOneMessage({type, id}) {
// get coordinated message from primary
return new Promise(resolve => {
Expand Down Expand Up @@ -706,7 +716,7 @@ async function _preparePrimaryExit() {
}
}

async function _exit(code) {
async function _exit(code = 0) {
/* Notes on exiting:
When the primary does an orderly exit, it must notify all workers
Expand Down Expand Up @@ -740,13 +750,18 @@ async function _exit(code) {
if(BEDROCK_STARTED) {
await events.emit('bedrock.stop');
}
await events.emit('bedrock.stopped');
// FIXME: deprecated in v6.x, remove in v7+
await events.emit('bedrock-cli.exit');
await events.emit('bedrock.exit');
}
} finally {
await _logExit(code);
process.exit(code);
if(cluster.isPrimary) {
await _logExit(code);
process.exit(code);
} else {
cluster.worker.kill();
}
}
}

Expand All @@ -757,10 +772,7 @@ async function _exitOnError(err) {
}
}

async function _logExit(code = 0) {
if(!cluster.isPrimary) {
return;
}
async function _logExit() {
// log final message and wait for logger to flush
const logger = loggers.get('app').child('bedrock/primary');
try {
Expand Down

0 comments on commit 61cb5ad

Please sign in to comment.