@seidor-cloud-produtos/orbit-backend-lib 1.101.3 → 1.101.5
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.
|
@@ -4,13 +4,17 @@ export type MethodOptions = {
|
|
|
4
4
|
};
|
|
5
5
|
export type CacheOptions = {
|
|
6
6
|
throwOnError?: boolean;
|
|
7
|
+
contingencyClients?: CacheClient[];
|
|
7
8
|
};
|
|
8
9
|
export declare abstract class Cache {
|
|
9
|
-
protected
|
|
10
|
+
protected client: CacheClient;
|
|
11
|
+
protected elegibleClients: CacheClient[];
|
|
10
12
|
protected DEFAULT_EXPIRATION_SECONDS: number;
|
|
11
13
|
private options?;
|
|
12
14
|
constructor(options?: CacheOptions);
|
|
15
|
+
private setElegibleClients;
|
|
13
16
|
private setOptions;
|
|
17
|
+
private setRunningClient;
|
|
14
18
|
start(options?: MethodOptions): Promise<void>;
|
|
15
19
|
get(keyCache: string, options?: MethodOptions): Promise<any>;
|
|
16
20
|
set(keyCache: string, data: any, expiration?: number, options?: MethodOptions): Promise<void>;
|
|
@@ -18,7 +22,7 @@ export declare abstract class Cache {
|
|
|
18
22
|
getKeys(pattern?: string, options?: MethodOptions): Promise<string[] | null>;
|
|
19
23
|
delBatch(patterns: string[], options?: MethodOptions): Promise<void>;
|
|
20
24
|
flush(): Promise<void>;
|
|
21
|
-
isRunning(options?: MethodOptions): Promise<
|
|
25
|
+
isRunning(options?: MethodOptions, isContingency?: boolean): Promise<boolean>;
|
|
22
26
|
close(options?: MethodOptions): Promise<void>;
|
|
23
27
|
protected handleException(e: Error): Promise<void>;
|
|
24
28
|
}
|
|
@@ -5,11 +5,21 @@ const tslib_1 = require("tslib");
|
|
|
5
5
|
const env_1 = require("../../infra/environment/env");
|
|
6
6
|
const logger_1 = tslib_1.__importDefault(require("../logger"));
|
|
7
7
|
class Cache {
|
|
8
|
+
client;
|
|
9
|
+
elegibleClients;
|
|
8
10
|
DEFAULT_EXPIRATION_SECONDS = env_1.env.DEFAULT_CACHE_EXPIRATION;
|
|
9
11
|
options;
|
|
10
12
|
constructor(options) {
|
|
11
|
-
if (options)
|
|
13
|
+
if (options) {
|
|
12
14
|
this.setOptions(options);
|
|
15
|
+
this.setElegibleClients(options.contingencyClients);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
setElegibleClients(contingencyClients) {
|
|
19
|
+
this.elegibleClients = [
|
|
20
|
+
this.client,
|
|
21
|
+
...(contingencyClients || []),
|
|
22
|
+
];
|
|
13
23
|
}
|
|
14
24
|
setOptions(options) {
|
|
15
25
|
this.options = {
|
|
@@ -17,6 +27,27 @@ class Cache {
|
|
|
17
27
|
throwOnError: options.throwOnError === undefined ? true : options.throwOnError,
|
|
18
28
|
};
|
|
19
29
|
}
|
|
30
|
+
async setRunningClient() {
|
|
31
|
+
if (this.elegibleClients.length === 1) {
|
|
32
|
+
return this;
|
|
33
|
+
}
|
|
34
|
+
let isRunning = false;
|
|
35
|
+
for (let i = 0; i < this.elegibleClients.length; i++) {
|
|
36
|
+
const currentClient = this.elegibleClients[i];
|
|
37
|
+
try {
|
|
38
|
+
await currentClient.start();
|
|
39
|
+
isRunning = await currentClient.isRunning();
|
|
40
|
+
}
|
|
41
|
+
catch (e) { }
|
|
42
|
+
if (isRunning) {
|
|
43
|
+
await this.client.close();
|
|
44
|
+
this.client = currentClient;
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
logger_1.default.warning(`Changed cache client to ${this.client.constructor.name}`);
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
20
51
|
async start(options) {
|
|
21
52
|
try {
|
|
22
53
|
await this.client.start();
|
|
@@ -25,6 +56,9 @@ class Cache {
|
|
|
25
56
|
if (options?.throwOnError || this.options?.throwOnError) {
|
|
26
57
|
await this.handleException(e);
|
|
27
58
|
}
|
|
59
|
+
else {
|
|
60
|
+
await this.setRunningClient();
|
|
61
|
+
}
|
|
28
62
|
return;
|
|
29
63
|
}
|
|
30
64
|
logger_1.default.debug()?.info('💾 Cache Connected');
|
|
@@ -93,16 +127,21 @@ class Cache {
|
|
|
93
127
|
await this.handleException(e);
|
|
94
128
|
}
|
|
95
129
|
}
|
|
96
|
-
async isRunning(options) {
|
|
130
|
+
async isRunning(options, isContingency = false) {
|
|
131
|
+
let isRunning = false;
|
|
97
132
|
try {
|
|
98
|
-
|
|
133
|
+
isRunning = await this.client.isRunning();
|
|
99
134
|
}
|
|
100
135
|
catch (e) {
|
|
101
136
|
if (options?.throwOnError || this.options?.throwOnError) {
|
|
102
137
|
await this.handleException(e);
|
|
103
138
|
}
|
|
104
|
-
return false;
|
|
105
139
|
}
|
|
140
|
+
if (!isRunning && !isContingency) {
|
|
141
|
+
await this.setRunningClient();
|
|
142
|
+
return await this.isRunning(options, true);
|
|
143
|
+
}
|
|
144
|
+
return isRunning;
|
|
106
145
|
}
|
|
107
146
|
async close(options) {
|
|
108
147
|
logger_1.default.debug()?.info('💾 Cache closed');
|
|
@@ -118,7 +157,7 @@ class Cache {
|
|
|
118
157
|
}
|
|
119
158
|
async handleException(e) {
|
|
120
159
|
try {
|
|
121
|
-
|
|
160
|
+
logger_1.default.error(JSON.stringify(e));
|
|
122
161
|
}
|
|
123
162
|
catch { }
|
|
124
163
|
throw e;
|