How to subscribe to onFailure events when using the circuit breaker strategy in the ResilienceCommand class #328
-
Hello! First of all thanks for you work 🙇♂️ With my team we are considering to use this tool in our API, so we are performing some tests. We were playing around with the decorator and the ResilienceCommand. And we started thinking on how could we eventually export stats to gain observability, similar to what this lib does for Opossum. So we wanted to subscribe to some events, and we could do it for the success event, but not for the failure or short circuit. import { Injectable } from '@nestjs/common';
import { ResilienceCommand, ResilienceFactory, ResilienceEventType } from 'nestjs-resilience';
import { ClientsService } from './clients.service';
@Injectable()
export class GetClientsService extends ResilienceCommand {
constructor(
private readonly factory: ResilienceFactory,
private readonly clientsService: ClientsService
) {
const circuitBreakerStrategy = factory.createCircuitBreakerStrategy({
requestVolumeThreshold: 1,
sleepWindowInMilliseconds: 5000,
errorThresholdPercentage: 50
});
super([circuitBreakerStrategy]);
this.eventBus.on(ResilienceEventType.ShortCircuit, () => console.log('GetClientsService EventBus:ShortCircuit'))
this.eventBus.on(ResilienceEventType.Success, () => console.log('GetClientsService EventBus:Success'))
this.eventBus.on(ResilienceEventType.Failure, () => console.log('GetClientsService EventBus:Failure'))
}
async run(id: number) {
console.log('GetClientsService run')
return this.clientsService.findOneById(id);
}
override onFailure(error: any) {
console.log('GetClientsService onFailure')
return super.onFailure(error);
}
override onSuccess() {
console.log('GetClientsService onSuccess')
return super.onSuccess();
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
@fhosen Hi there! Thank you for question! With your question I found some issues with events and fixed them. (https://github.com/SocketSomeone/nestjs-resilience/releases/tag/2.1.1)
Btw, if you wanna create metrics storage by commands, feel free to open PR! |
Beta Was this translation helpful? Give feedback.
-
Hey @SocketSomeone, thanks for the quick answer and fix; it's working like a charm now 😄
If we end up implementing something like that, for sure we'll try to contribute to the project! |
Beta Was this translation helpful? Give feedback.
@fhosen Hi there!
Thank you for question! With your question I found some issues with events and fixed them. (https://github.com/SocketSomeone/nestjs-resilience/releases/tag/2.1.1)
Now it`s works better:
ResilienceEventBus.getInstance()
(https://github.com/SocketSomeone/nestjs-resilience/blob/master/test/commands/resilience.command.spec.ts#L53)Btw, if you wanna create metrics storage by commands, feel free to open PR!