nestjs-resilience
Version:
A module for improving the reliability and fault-tolerance of your NestJS applications
38 lines (37 loc) • 1.55 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseCommand = void 0;
const common_1 = require("@nestjs/common");
const resilience_event_bus_1 = require("../resilience.event-bus");
const enum_1 = require("../enum");
const exceptions_1 = require("../exceptions");
class BaseCommand {
constructor(strategies, group, name) {
this.eventBus = resilience_event_bus_1.ResilienceEventBus.getInstance();
this.strategies = strategies;
this.group = group !== null && group !== void 0 ? group : 'default';
this.name = name !== null && name !== void 0 ? name : this.constructor.name;
this.logger = new common_1.Logger(this.toString());
}
onSuccess() {
this.logger.debug('Command executed successfully');
this.eventBus.emit(enum_1.ResilienceEventType.Success, this);
}
onFailure(error) {
if (error instanceof exceptions_1.TimeoutException) {
this.logger.debug('Command timed out');
this.eventBus.emit(enum_1.ResilienceEventType.Timeout, this);
}
if (error instanceof exceptions_1.CircuitOpenedException) {
this.logger.debug('Command short-circuited');
this.eventBus.emit(enum_1.ResilienceEventType.ShortCircuit, this);
}
this.logger.debug('Command failed');
this.eventBus.emit(enum_1.ResilienceEventType.Failure, this);
return error;
}
toString() {
return `${this.group}#${this.name}`;
}
}
exports.BaseCommand = BaseCommand;