Skip to content

Commit

Permalink
fix: breaker should emit a shutdown event when it is shutdown (nodesh…
Browse files Browse the repository at this point in the history
…ift#625)

* fix: breaker should emit a shutdown event when it is shutdown

fixes nodeshift#620

* chore: Update README.md

Co-authored-by: Lance Ball <[email protected]>

Co-authored-by: Lance Ball <[email protected]>
  • Loading branch information
lholmquist and lance authored Jan 7, 2022
1 parent 42f21ed commit ea4d058
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ Here are the events you can listen for.
* `fallback` - emitted when the breaker has a fallback function and executes it
* `semaphoreLocked` - emitted when the breaker is at capacity and cannot execute the request
* `healthCheckFailed` - emitted when a user-supplied health check function returns a rejected promise
* `shutdown` - emitted when the breaker shuts down

Handling events gives a greater level of control over your application behavior.

Expand Down
11 changes: 6 additions & 5 deletions lib/circuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,12 @@ class CircuitBreaker extends EventEmitter {
* @returns {void}
*/
shutdown () {
/**
* Emitted when the circuit breaker has been shut down.
* @event CircuitBreaker#shutdown
*/
this.emit('shutdown');

this.disable();
this.removeAllListeners();
if (this[RESET_TIMEOUT]) {
Expand All @@ -333,11 +339,6 @@ class CircuitBreaker extends EventEmitter {
}
this.status.shutdown();
this[STATE] = SHUTDOWN;
/**
* Emitted when the circuit breaker has been shut down.
* @event CircuitBreaker#shutdown
*/
this.emit('shutdown');
}

/**
Expand Down
5 changes: 4 additions & 1 deletion test/circuit-shutdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ test('EventEmitter max listeners', t => {
});

test('Circuit shuts down properly', t => {
t.plan(6);
t.plan(7);
const breaker = new CircuitBreaker(passFail);
breaker.on('shutdown', _ => {
t.pass('Breaker emits Shutdown');
});
t.ok(breaker.fire(1), 'breaker is active');
breaker.shutdown();
t.ok(breaker.isShutdown, 'breaker is shutdown');
Expand Down

0 comments on commit ea4d058

Please sign in to comment.