@takentrade/takentrade-libs 4.1.1 → 4.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -57
- package/dist/auth/guards/index.d.ts +1 -0
- package/dist/auth/guards/index.js +1 -0
- package/dist/auth/guards/tnt-base-auth.guard.d.ts +15 -0
- package/dist/auth/guards/tnt-base-auth.guard.js +54 -0
- package/dist/bullmq/config/bull.config.js +1 -1
- package/dist/index.d.ts +0 -3
- package/dist/index.js +0 -3
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +15 -21
- package/dist/metrics/circuit-breaker-metrics.service.d.ts +0 -61
- package/dist/metrics/circuit-breaker-metrics.service.js +0 -149
- package/dist/metrics/index.d.ts +0 -5
- package/dist/metrics/index.js +0 -21
- package/dist/metrics/metrics-registry.d.ts +0 -50
- package/dist/metrics/metrics-registry.js +0 -156
- package/dist/metrics/metrics-updater.d.ts +0 -32
- package/dist/metrics/metrics-updater.js +0 -102
- package/dist/metrics/metrics.controller.d.ts +0 -11
- package/dist/metrics/metrics.controller.js +0 -32
- package/dist/metrics/metrics.interface.d.ts +0 -53
- package/dist/metrics/metrics.interface.js +0 -23
- package/dist/metrics/metrics.module.d.ts +0 -11
- package/dist/metrics/metrics.module.js +0 -39
- package/dist/metrics/metrics.service.d.ts +0 -50
- package/dist/metrics/metrics.service.js +0 -165
- package/dist/monitoring/alerting.service.d.ts +0 -82
- package/dist/monitoring/alerting.service.js +0 -290
- package/dist/monitoring/index.d.ts +0 -5
- package/dist/monitoring/index.js +0 -21
- package/dist/monitoring/monitoring.interface.d.ts +0 -102
- package/dist/monitoring/monitoring.interface.js +0 -10
- package/dist/monitoring/monitoring.module.d.ts +0 -8
- package/dist/monitoring/monitoring.module.js +0 -44
- package/dist/monitoring/sentry.service.d.ts +0 -65
- package/dist/monitoring/sentry.service.js +0 -218
- package/dist/monitoring/uptime.service.d.ts +0 -59
- package/dist/monitoring/uptime.service.js +0 -213
- package/dist/resilience/circuit-breaker-event-handler.d.ts +0 -16
- package/dist/resilience/circuit-breaker-event-handler.js +0 -70
- package/dist/resilience/circuit-breaker-factory.d.ts +0 -19
- package/dist/resilience/circuit-breaker-factory.js +0 -51
- package/dist/resilience/circuit-breaker-stats.service.d.ts +0 -37
- package/dist/resilience/circuit-breaker-stats.service.js +0 -69
- package/dist/resilience/circuit-breaker.interface.d.ts +0 -59
- package/dist/resilience/circuit-breaker.interface.js +0 -25
- package/dist/resilience/circuit-breaker.module.d.ts +0 -2
- package/dist/resilience/circuit-breaker.module.js +0 -21
- package/dist/resilience/circuit-breaker.service.d.ts +0 -119
- package/dist/resilience/circuit-breaker.service.js +0 -234
- package/dist/resilience/index.d.ts +0 -3
- package/dist/resilience/index.js +0 -19
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CircuitBreakerStatsService = void 0;
|
|
4
|
-
const circuit_breaker_interface_1 = require("./circuit-breaker.interface");
|
|
5
|
-
/**
|
|
6
|
-
* Circuit Breaker Statistics Service
|
|
7
|
-
*
|
|
8
|
-
* Handles statistics and state management for circuit breakers
|
|
9
|
-
*/
|
|
10
|
-
class CircuitBreakerStatsService {
|
|
11
|
-
/**
|
|
12
|
-
* Get statistics for a circuit breaker
|
|
13
|
-
*
|
|
14
|
-
* @param instance - Circuit breaker instance
|
|
15
|
-
* @returns Circuit breaker statistics
|
|
16
|
-
*/
|
|
17
|
-
getStats(instance) {
|
|
18
|
-
const stats = instance.breaker.stats;
|
|
19
|
-
return {
|
|
20
|
-
fires: stats.fires,
|
|
21
|
-
failures: stats.failures,
|
|
22
|
-
successes: stats.successes,
|
|
23
|
-
rejects: stats.rejects,
|
|
24
|
-
timeouts: stats.timeouts,
|
|
25
|
-
cacheHits: stats.cacheHits,
|
|
26
|
-
cacheMisses: stats.cacheMisses,
|
|
27
|
-
semaphoreRejections: stats.semaphoreRejections,
|
|
28
|
-
percentiles: stats.percentiles,
|
|
29
|
-
latencyMean: stats.latencyMean,
|
|
30
|
-
latencyTimes: stats.latencyTimes,
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Get all circuit breaker statistics
|
|
35
|
-
*
|
|
36
|
-
* @param breakers - Map of circuit breaker instances
|
|
37
|
-
* @returns Map of circuit breaker names to their statistics
|
|
38
|
-
*/
|
|
39
|
-
getAllStats(breakers) {
|
|
40
|
-
const allStats = new Map();
|
|
41
|
-
for (const [name, instance] of breakers.entries()) {
|
|
42
|
-
allStats.set(name, this.getStats(instance));
|
|
43
|
-
}
|
|
44
|
-
return allStats;
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Get the current state of a circuit breaker
|
|
48
|
-
*
|
|
49
|
-
* @param breaker - Circuit breaker instance
|
|
50
|
-
* @returns Current circuit state
|
|
51
|
-
*/
|
|
52
|
-
getState(breaker) {
|
|
53
|
-
if (breaker.opened)
|
|
54
|
-
return circuit_breaker_interface_1.CircuitState.OPEN;
|
|
55
|
-
if (breaker.halfOpen)
|
|
56
|
-
return circuit_breaker_interface_1.CircuitState.HALF_OPEN;
|
|
57
|
-
return circuit_breaker_interface_1.CircuitState.CLOSED;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Check if a circuit breaker is open
|
|
61
|
-
*
|
|
62
|
-
* @param breaker - Circuit breaker instance
|
|
63
|
-
* @returns True if circuit is open
|
|
64
|
-
*/
|
|
65
|
-
isOpen(breaker) {
|
|
66
|
-
return breaker.opened;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
exports.CircuitBreakerStatsService = CircuitBreakerStatsService;
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import CircuitBreaker from 'opossum';
|
|
2
|
-
export declare enum CircuitState {
|
|
3
|
-
CLOSED = "closed",
|
|
4
|
-
OPEN = "open",
|
|
5
|
-
HALF_OPEN = "halfOpen"
|
|
6
|
-
}
|
|
7
|
-
export interface CircuitBreakerOptions {
|
|
8
|
-
timeout?: number;
|
|
9
|
-
errorThresholdPercentage?: number;
|
|
10
|
-
resetTimeout?: number;
|
|
11
|
-
rollingCountTimeout?: number;
|
|
12
|
-
rollingCountBuckets?: number;
|
|
13
|
-
name?: string;
|
|
14
|
-
capacity?: number;
|
|
15
|
-
enableSnapshots?: boolean;
|
|
16
|
-
allowWarmUp?: boolean;
|
|
17
|
-
volumeThreshold?: number;
|
|
18
|
-
errorFilter?: (error: any) => boolean;
|
|
19
|
-
cache?: boolean;
|
|
20
|
-
cacheTTL?: number;
|
|
21
|
-
cacheGetKey?: (...args: any[]) => string;
|
|
22
|
-
}
|
|
23
|
-
export interface CircuitBreakerStats {
|
|
24
|
-
fires: number;
|
|
25
|
-
failures: number;
|
|
26
|
-
successes: number;
|
|
27
|
-
rejects: number;
|
|
28
|
-
timeouts: number;
|
|
29
|
-
cacheHits: number;
|
|
30
|
-
cacheMisses: number;
|
|
31
|
-
semaphoreRejections: number;
|
|
32
|
-
percentiles: {
|
|
33
|
-
[percentile: number]: number;
|
|
34
|
-
};
|
|
35
|
-
latencyMean: number;
|
|
36
|
-
latencyTimes: number[];
|
|
37
|
-
}
|
|
38
|
-
export declare enum CircuitBreakerEvent {
|
|
39
|
-
FIRE = "fire",
|
|
40
|
-
SUCCESS = "success",
|
|
41
|
-
FAILURE = "failure",
|
|
42
|
-
TIMEOUT = "timeout",
|
|
43
|
-
REJECT = "reject",
|
|
44
|
-
OPEN = "open",
|
|
45
|
-
CLOSE = "close",
|
|
46
|
-
HALF_OPEN = "halfOpen",
|
|
47
|
-
FALLBACK = "fallback",
|
|
48
|
-
SEMAPHORE_LOCKED = "semaphoreLocked",
|
|
49
|
-
HEALTH_CHECK_FAILED = "healthCheckFailed",
|
|
50
|
-
CACHE_HIT = "cacheHit",
|
|
51
|
-
CACHE_MISS = "cacheMiss"
|
|
52
|
-
}
|
|
53
|
-
export interface CircuitBreakerInstance<T = any, R = any> {
|
|
54
|
-
breaker: CircuitBreaker<T[], R>;
|
|
55
|
-
name: string;
|
|
56
|
-
options: CircuitBreakerOptions;
|
|
57
|
-
}
|
|
58
|
-
export type FallbackFunction<T = any> = (...args: any[]) => T | Promise<T>;
|
|
59
|
-
export type HealthCheckFunction = () => Promise<boolean>;
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CircuitBreakerEvent = exports.CircuitState = void 0;
|
|
4
|
-
var CircuitState;
|
|
5
|
-
(function (CircuitState) {
|
|
6
|
-
CircuitState["CLOSED"] = "closed";
|
|
7
|
-
CircuitState["OPEN"] = "open";
|
|
8
|
-
CircuitState["HALF_OPEN"] = "halfOpen";
|
|
9
|
-
})(CircuitState || (exports.CircuitState = CircuitState = {}));
|
|
10
|
-
var CircuitBreakerEvent;
|
|
11
|
-
(function (CircuitBreakerEvent) {
|
|
12
|
-
CircuitBreakerEvent["FIRE"] = "fire";
|
|
13
|
-
CircuitBreakerEvent["SUCCESS"] = "success";
|
|
14
|
-
CircuitBreakerEvent["FAILURE"] = "failure";
|
|
15
|
-
CircuitBreakerEvent["TIMEOUT"] = "timeout";
|
|
16
|
-
CircuitBreakerEvent["REJECT"] = "reject";
|
|
17
|
-
CircuitBreakerEvent["OPEN"] = "open";
|
|
18
|
-
CircuitBreakerEvent["CLOSE"] = "close";
|
|
19
|
-
CircuitBreakerEvent["HALF_OPEN"] = "halfOpen";
|
|
20
|
-
CircuitBreakerEvent["FALLBACK"] = "fallback";
|
|
21
|
-
CircuitBreakerEvent["SEMAPHORE_LOCKED"] = "semaphoreLocked";
|
|
22
|
-
CircuitBreakerEvent["HEALTH_CHECK_FAILED"] = "healthCheckFailed";
|
|
23
|
-
CircuitBreakerEvent["CACHE_HIT"] = "cacheHit";
|
|
24
|
-
CircuitBreakerEvent["CACHE_MISS"] = "cacheMiss";
|
|
25
|
-
})(CircuitBreakerEvent || (exports.CircuitBreakerEvent = CircuitBreakerEvent = {}));
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
-
};
|
|
8
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.CircuitBreakerModule = void 0;
|
|
10
|
-
const common_1 = require("@nestjs/common");
|
|
11
|
-
const circuit_breaker_service_1 = require("./circuit-breaker.service");
|
|
12
|
-
let CircuitBreakerModule = class CircuitBreakerModule {
|
|
13
|
-
};
|
|
14
|
-
exports.CircuitBreakerModule = CircuitBreakerModule;
|
|
15
|
-
exports.CircuitBreakerModule = CircuitBreakerModule = __decorate([
|
|
16
|
-
(0, common_1.Global)(),
|
|
17
|
-
(0, common_1.Module)({
|
|
18
|
-
providers: [circuit_breaker_service_1.CircuitBreakerService],
|
|
19
|
-
exports: [circuit_breaker_service_1.CircuitBreakerService],
|
|
20
|
-
})
|
|
21
|
-
], CircuitBreakerModule);
|
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
import { OnModuleDestroy } from '@nestjs/common';
|
|
2
|
-
import CircuitBreaker from 'opossum';
|
|
3
|
-
import { CircuitBreakerOptions, CircuitBreakerStats, CircuitState, FallbackFunction } from './circuit-breaker.interface';
|
|
4
|
-
export declare class CircuitBreakerService implements OnModuleDestroy {
|
|
5
|
-
private readonly logger;
|
|
6
|
-
private readonly breakers;
|
|
7
|
-
private readonly factory;
|
|
8
|
-
private readonly statsService;
|
|
9
|
-
/**
|
|
10
|
-
* Create a new circuit breaker instance
|
|
11
|
-
*
|
|
12
|
-
* @param name - Unique identifier for the circuit breaker
|
|
13
|
-
* @param action - The async function to be protected
|
|
14
|
-
* @param options - Circuit breaker configuration options
|
|
15
|
-
* @returns Circuit breaker instance
|
|
16
|
-
*/
|
|
17
|
-
createBreaker<T = any, R = any>(name: string, action: (...args: T[]) => Promise<R>, options?: CircuitBreakerOptions): CircuitBreaker<T[], R>;
|
|
18
|
-
/**
|
|
19
|
-
* Execute a protected function through the circuit breaker
|
|
20
|
-
*
|
|
21
|
-
* @param name - Circuit breaker name
|
|
22
|
-
* @param args - Arguments to pass to the protected function
|
|
23
|
-
* @returns Result of the protected function
|
|
24
|
-
*/
|
|
25
|
-
fire<T = any>(name: string, ...args: any[]): Promise<T>;
|
|
26
|
-
/**
|
|
27
|
-
* Set a fallback function for a circuit breaker
|
|
28
|
-
*
|
|
29
|
-
* @param name - Circuit breaker name
|
|
30
|
-
* @param fallback - Fallback function to execute when circuit is open
|
|
31
|
-
*/
|
|
32
|
-
setFallback<T = any>(name: string, fallback: FallbackFunction<T>): void;
|
|
33
|
-
/**
|
|
34
|
-
* Set a health check function for a circuit breaker
|
|
35
|
-
*
|
|
36
|
-
* @param name - Circuit breaker name
|
|
37
|
-
* @param healthCheck - Health check function
|
|
38
|
-
* @param interval - Health check interval in milliseconds (default: 5000)
|
|
39
|
-
*/
|
|
40
|
-
setHealthCheck(name: string, healthCheck: () => Promise<void>, interval?: number): void;
|
|
41
|
-
/**
|
|
42
|
-
* Note: Caching should be configured during circuit breaker creation via options.
|
|
43
|
-
* This is a limitation of the current Opossum version.
|
|
44
|
-
*/
|
|
45
|
-
/**
|
|
46
|
-
* Clear the cache for a circuit breaker
|
|
47
|
-
*
|
|
48
|
-
* @param name - Circuit breaker name
|
|
49
|
-
*/
|
|
50
|
-
clearCache(name: string): void;
|
|
51
|
-
/**
|
|
52
|
-
* Get the current state of a circuit breaker
|
|
53
|
-
*
|
|
54
|
-
* @param name - Circuit breaker name
|
|
55
|
-
* @returns Current circuit state
|
|
56
|
-
*/
|
|
57
|
-
getState(name: string): CircuitState;
|
|
58
|
-
/**
|
|
59
|
-
* Check if a circuit breaker is open
|
|
60
|
-
*
|
|
61
|
-
* @param name - Circuit breaker name
|
|
62
|
-
* @returns True if circuit is open
|
|
63
|
-
*/
|
|
64
|
-
isOpen(name: string): boolean;
|
|
65
|
-
/**
|
|
66
|
-
* Get statistics for a circuit breaker
|
|
67
|
-
*
|
|
68
|
-
* @param name - Circuit breaker name
|
|
69
|
-
* @returns Circuit breaker statistics
|
|
70
|
-
*/
|
|
71
|
-
getStats(name: string): CircuitBreakerStats;
|
|
72
|
-
/**
|
|
73
|
-
* Get all circuit breaker statistics
|
|
74
|
-
*
|
|
75
|
-
* @returns Map of circuit breaker names to their statistics
|
|
76
|
-
*/
|
|
77
|
-
getAllStats(): Map<string, CircuitBreakerStats>;
|
|
78
|
-
/**
|
|
79
|
-
* Manually open a circuit breaker
|
|
80
|
-
*
|
|
81
|
-
* @param name - Circuit breaker name
|
|
82
|
-
*/
|
|
83
|
-
open(name: string): void;
|
|
84
|
-
/**
|
|
85
|
-
* Manually close a circuit breaker
|
|
86
|
-
*
|
|
87
|
-
* @param name - Circuit breaker name
|
|
88
|
-
*/
|
|
89
|
-
close(name: string): void;
|
|
90
|
-
/**
|
|
91
|
-
* Reset a circuit breaker (clear stats and close)
|
|
92
|
-
*
|
|
93
|
-
* @param name - Circuit breaker name
|
|
94
|
-
*/
|
|
95
|
-
reset(name: string): void;
|
|
96
|
-
/**
|
|
97
|
-
* Remove a circuit breaker instance
|
|
98
|
-
*
|
|
99
|
-
* @param name - Circuit breaker name
|
|
100
|
-
*/
|
|
101
|
-
remove(name: string): void;
|
|
102
|
-
/**
|
|
103
|
-
* Get all circuit breaker names
|
|
104
|
-
*
|
|
105
|
-
* @returns Array of circuit breaker names
|
|
106
|
-
*/
|
|
107
|
-
getBreakerNames(): string[];
|
|
108
|
-
/**
|
|
109
|
-
* Check if a circuit breaker exists
|
|
110
|
-
*
|
|
111
|
-
* @param name - Circuit breaker name
|
|
112
|
-
* @returns True if circuit breaker exists
|
|
113
|
-
*/
|
|
114
|
-
hasBreaker(name: string): boolean;
|
|
115
|
-
/**
|
|
116
|
-
* Cleanup on module destroy
|
|
117
|
-
*/
|
|
118
|
-
onModuleDestroy(): Promise<void>;
|
|
119
|
-
}
|
|
@@ -1,234 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
-
};
|
|
8
|
-
var CircuitBreakerService_1;
|
|
9
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
-
exports.CircuitBreakerService = void 0;
|
|
11
|
-
const common_1 = require("@nestjs/common");
|
|
12
|
-
const circuit_breaker_factory_1 = require("./circuit-breaker-factory");
|
|
13
|
-
const circuit_breaker_stats_service_1 = require("./circuit-breaker-stats.service");
|
|
14
|
-
let CircuitBreakerService = CircuitBreakerService_1 = class CircuitBreakerService {
|
|
15
|
-
logger = new common_1.Logger(CircuitBreakerService_1.name);
|
|
16
|
-
breakers = new Map();
|
|
17
|
-
factory = new circuit_breaker_factory_1.CircuitBreakerFactory();
|
|
18
|
-
statsService = new circuit_breaker_stats_service_1.CircuitBreakerStatsService();
|
|
19
|
-
/**
|
|
20
|
-
* Create a new circuit breaker instance
|
|
21
|
-
*
|
|
22
|
-
* @param name - Unique identifier for the circuit breaker
|
|
23
|
-
* @param action - The async function to be protected
|
|
24
|
-
* @param options - Circuit breaker configuration options
|
|
25
|
-
* @returns Circuit breaker instance
|
|
26
|
-
*/
|
|
27
|
-
createBreaker(name, action, options = {}) {
|
|
28
|
-
if (this.breakers.has(name)) {
|
|
29
|
-
this.logger.warn(`Circuit breaker '${name}' already exists. Returning existing instance.`);
|
|
30
|
-
return this.breakers.get(name).breaker;
|
|
31
|
-
}
|
|
32
|
-
const instance = this.factory.create(name, action, options);
|
|
33
|
-
this.breakers.set(name, instance);
|
|
34
|
-
return instance.breaker;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Execute a protected function through the circuit breaker
|
|
38
|
-
*
|
|
39
|
-
* @param name - Circuit breaker name
|
|
40
|
-
* @param args - Arguments to pass to the protected function
|
|
41
|
-
* @returns Result of the protected function
|
|
42
|
-
*/
|
|
43
|
-
async fire(name, ...args) {
|
|
44
|
-
const instance = this.breakers.get(name);
|
|
45
|
-
if (!instance) {
|
|
46
|
-
throw new Error(`Circuit breaker '${name}' not found. Create it first using createBreaker()`);
|
|
47
|
-
}
|
|
48
|
-
return instance.breaker.fire(...args);
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Set a fallback function for a circuit breaker
|
|
52
|
-
*
|
|
53
|
-
* @param name - Circuit breaker name
|
|
54
|
-
* @param fallback - Fallback function to execute when circuit is open
|
|
55
|
-
*/
|
|
56
|
-
setFallback(name, fallback) {
|
|
57
|
-
const instance = this.breakers.get(name);
|
|
58
|
-
if (!instance) {
|
|
59
|
-
throw new Error(`Circuit breaker '${name}' not found`);
|
|
60
|
-
}
|
|
61
|
-
instance.breaker.fallback(fallback);
|
|
62
|
-
this.logger.log(`Fallback function set for circuit breaker '${name}'`);
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Set a health check function for a circuit breaker
|
|
66
|
-
*
|
|
67
|
-
* @param name - Circuit breaker name
|
|
68
|
-
* @param healthCheck - Health check function
|
|
69
|
-
* @param interval - Health check interval in milliseconds (default: 5000)
|
|
70
|
-
*/
|
|
71
|
-
setHealthCheck(name, healthCheck, interval = 5000) {
|
|
72
|
-
const instance = this.breakers.get(name);
|
|
73
|
-
if (!instance) {
|
|
74
|
-
throw new Error(`Circuit breaker '${name}' not found`);
|
|
75
|
-
}
|
|
76
|
-
instance.breaker.healthCheck(healthCheck, interval);
|
|
77
|
-
this.logger.log(`Health check set for circuit breaker '${name}' with ${interval}ms interval`);
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Note: Caching should be configured during circuit breaker creation via options.
|
|
81
|
-
* This is a limitation of the current Opossum version.
|
|
82
|
-
*/
|
|
83
|
-
/**
|
|
84
|
-
* Clear the cache for a circuit breaker
|
|
85
|
-
*
|
|
86
|
-
* @param name - Circuit breaker name
|
|
87
|
-
*/
|
|
88
|
-
clearCache(name) {
|
|
89
|
-
const instance = this.breakers.get(name);
|
|
90
|
-
if (!instance) {
|
|
91
|
-
throw new Error(`Circuit breaker '${name}' not found`);
|
|
92
|
-
}
|
|
93
|
-
instance.breaker.clearCache();
|
|
94
|
-
this.logger.debug(`Cache cleared for circuit breaker '${name}'`);
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Get the current state of a circuit breaker
|
|
98
|
-
*
|
|
99
|
-
* @param name - Circuit breaker name
|
|
100
|
-
* @returns Current circuit state
|
|
101
|
-
*/
|
|
102
|
-
getState(name) {
|
|
103
|
-
const instance = this.breakers.get(name);
|
|
104
|
-
if (!instance) {
|
|
105
|
-
throw new Error(`Circuit breaker '${name}' not found`);
|
|
106
|
-
}
|
|
107
|
-
return this.statsService.getState(instance.breaker);
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Check if a circuit breaker is open
|
|
111
|
-
*
|
|
112
|
-
* @param name - Circuit breaker name
|
|
113
|
-
* @returns True if circuit is open
|
|
114
|
-
*/
|
|
115
|
-
isOpen(name) {
|
|
116
|
-
const instance = this.breakers.get(name);
|
|
117
|
-
if (!instance) {
|
|
118
|
-
throw new Error(`Circuit breaker '${name}' not found`);
|
|
119
|
-
}
|
|
120
|
-
return this.statsService.isOpen(instance.breaker);
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* Get statistics for a circuit breaker
|
|
124
|
-
*
|
|
125
|
-
* @param name - Circuit breaker name
|
|
126
|
-
* @returns Circuit breaker statistics
|
|
127
|
-
*/
|
|
128
|
-
getStats(name) {
|
|
129
|
-
const instance = this.breakers.get(name);
|
|
130
|
-
if (!instance) {
|
|
131
|
-
throw new Error(`Circuit breaker '${name}' not found`);
|
|
132
|
-
}
|
|
133
|
-
return this.statsService.getStats(instance);
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Get all circuit breaker statistics
|
|
137
|
-
*
|
|
138
|
-
* @returns Map of circuit breaker names to their statistics
|
|
139
|
-
*/
|
|
140
|
-
getAllStats() {
|
|
141
|
-
return this.statsService.getAllStats(this.breakers);
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Manually open a circuit breaker
|
|
145
|
-
*
|
|
146
|
-
* @param name - Circuit breaker name
|
|
147
|
-
*/
|
|
148
|
-
open(name) {
|
|
149
|
-
const instance = this.breakers.get(name);
|
|
150
|
-
if (!instance) {
|
|
151
|
-
throw new Error(`Circuit breaker '${name}' not found`);
|
|
152
|
-
}
|
|
153
|
-
instance.breaker.open();
|
|
154
|
-
this.logger.warn(`Circuit breaker '${name}' manually opened`);
|
|
155
|
-
}
|
|
156
|
-
/**
|
|
157
|
-
* Manually close a circuit breaker
|
|
158
|
-
*
|
|
159
|
-
* @param name - Circuit breaker name
|
|
160
|
-
*/
|
|
161
|
-
close(name) {
|
|
162
|
-
const instance = this.breakers.get(name);
|
|
163
|
-
if (!instance) {
|
|
164
|
-
throw new Error(`Circuit breaker '${name}' not found`);
|
|
165
|
-
}
|
|
166
|
-
instance.breaker.close();
|
|
167
|
-
this.logger.log(`Circuit breaker '${name}' manually closed`);
|
|
168
|
-
}
|
|
169
|
-
/**
|
|
170
|
-
* Reset a circuit breaker (clear stats and close)
|
|
171
|
-
*
|
|
172
|
-
* @param name - Circuit breaker name
|
|
173
|
-
*/
|
|
174
|
-
reset(name) {
|
|
175
|
-
const instance = this.breakers.get(name);
|
|
176
|
-
if (!instance) {
|
|
177
|
-
throw new Error(`Circuit breaker '${name}' not found`);
|
|
178
|
-
}
|
|
179
|
-
instance.breaker.close();
|
|
180
|
-
this.logger.log(`Circuit breaker '${name}' reset`);
|
|
181
|
-
}
|
|
182
|
-
/**
|
|
183
|
-
* Remove a circuit breaker instance
|
|
184
|
-
*
|
|
185
|
-
* @param name - Circuit breaker name
|
|
186
|
-
*/
|
|
187
|
-
remove(name) {
|
|
188
|
-
const instance = this.breakers.get(name);
|
|
189
|
-
if (!instance) {
|
|
190
|
-
return;
|
|
191
|
-
}
|
|
192
|
-
instance.breaker.shutdown();
|
|
193
|
-
this.breakers.delete(name);
|
|
194
|
-
this.logger.log(`Circuit breaker '${name}' removed`);
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* Get all circuit breaker names
|
|
198
|
-
*
|
|
199
|
-
* @returns Array of circuit breaker names
|
|
200
|
-
*/
|
|
201
|
-
getBreakerNames() {
|
|
202
|
-
return Array.from(this.breakers.keys());
|
|
203
|
-
}
|
|
204
|
-
/**
|
|
205
|
-
* Check if a circuit breaker exists
|
|
206
|
-
*
|
|
207
|
-
* @param name - Circuit breaker name
|
|
208
|
-
* @returns True if circuit breaker exists
|
|
209
|
-
*/
|
|
210
|
-
hasBreaker(name) {
|
|
211
|
-
return this.breakers.has(name);
|
|
212
|
-
}
|
|
213
|
-
/**
|
|
214
|
-
* Cleanup on module destroy
|
|
215
|
-
*/
|
|
216
|
-
async onModuleDestroy() {
|
|
217
|
-
this.logger.log('Shutting down all circuit breakers...');
|
|
218
|
-
for (const [name, instance] of this.breakers.entries()) {
|
|
219
|
-
try {
|
|
220
|
-
instance.breaker.shutdown();
|
|
221
|
-
this.logger.log(`Circuit breaker '${name}' shut down successfully`);
|
|
222
|
-
}
|
|
223
|
-
catch (error) {
|
|
224
|
-
this.logger.error(`Error shutting down circuit breaker '${name}':`, error);
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
this.breakers.clear();
|
|
228
|
-
this.logger.log('All circuit breakers shut down');
|
|
229
|
-
}
|
|
230
|
-
};
|
|
231
|
-
exports.CircuitBreakerService = CircuitBreakerService;
|
|
232
|
-
exports.CircuitBreakerService = CircuitBreakerService = CircuitBreakerService_1 = __decorate([
|
|
233
|
-
(0, common_1.Injectable)()
|
|
234
|
-
], CircuitBreakerService);
|
package/dist/resilience/index.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./circuit-breaker.module"), exports);
|
|
18
|
-
__exportStar(require("./circuit-breaker.service"), exports);
|
|
19
|
-
__exportStar(require("./circuit-breaker.interface"), exports);
|