@clairejs/server 3.20.0 → 3.20.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
CHANGED
|
@@ -31,20 +31,22 @@ let DefaultHttpRequestHandler = class DefaultHttpRequestHandler extends Abstract
|
|
|
31
31
|
}
|
|
32
32
|
async handleRequest(endpoint, req) {
|
|
33
33
|
//-- supply correct order of params into handler
|
|
34
|
-
const params =
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
34
|
+
const params = !endpoint.params
|
|
35
|
+
? [req]
|
|
36
|
+
: Object.values(endpoint.params).map((p) => {
|
|
37
|
+
switch (p.source) {
|
|
38
|
+
case "body":
|
|
39
|
+
return req.getBody();
|
|
40
|
+
case "params":
|
|
41
|
+
return req.getParams();
|
|
42
|
+
case "queries":
|
|
43
|
+
return req.getQuery();
|
|
44
|
+
case "headers":
|
|
45
|
+
return req.headers;
|
|
46
|
+
case "raw":
|
|
47
|
+
return req;
|
|
48
|
+
}
|
|
49
|
+
});
|
|
48
50
|
const response = await endpoint.controller[endpoint.name](...params);
|
|
49
51
|
//-- validate response value against response dto
|
|
50
52
|
if (endpoint.responseDto) {
|
|
@@ -29,7 +29,7 @@ export declare class LocalJobScheduler extends AbstractJobScheduler implements I
|
|
|
29
29
|
/**
|
|
30
30
|
* The time to lock active scheduler
|
|
31
31
|
*/
|
|
32
|
-
protected readonly
|
|
32
|
+
protected readonly keyRetentionDurationSeconds: number;
|
|
33
33
|
protected intervals: any[];
|
|
34
34
|
private mutexHoldInterval?;
|
|
35
35
|
private isActive;
|
|
@@ -55,7 +55,7 @@ export declare class LocalJobScheduler extends AbstractJobScheduler implements I
|
|
|
55
55
|
/**
|
|
56
56
|
* The time to lock active scheduler
|
|
57
57
|
*/
|
|
58
|
-
|
|
58
|
+
keyRetentionDurationSeconds?: number);
|
|
59
59
|
private sendJob;
|
|
60
60
|
private processMessage;
|
|
61
61
|
private extendMutexKey;
|
|
@@ -32,7 +32,7 @@ let LocalJobScheduler = class LocalJobScheduler extends AbstractJobScheduler {
|
|
|
32
32
|
holdMutexKey;
|
|
33
33
|
uniqueIdKey;
|
|
34
34
|
multiClientChannel;
|
|
35
|
-
|
|
35
|
+
keyRetentionDurationSeconds;
|
|
36
36
|
intervals = [];
|
|
37
37
|
mutexHoldInterval;
|
|
38
38
|
isActive = false;
|
|
@@ -58,7 +58,7 @@ let LocalJobScheduler = class LocalJobScheduler extends AbstractJobScheduler {
|
|
|
58
58
|
/**
|
|
59
59
|
* The time to lock active scheduler
|
|
60
60
|
*/
|
|
61
|
-
|
|
61
|
+
keyRetentionDurationSeconds = 30) {
|
|
62
62
|
super(logger, db);
|
|
63
63
|
this.logger = logger;
|
|
64
64
|
this.db = db;
|
|
@@ -69,7 +69,7 @@ let LocalJobScheduler = class LocalJobScheduler extends AbstractJobScheduler {
|
|
|
69
69
|
this.holdMutexKey = holdMutexKey;
|
|
70
70
|
this.uniqueIdKey = uniqueIdKey;
|
|
71
71
|
this.multiClientChannel = multiClientChannel;
|
|
72
|
-
this.
|
|
72
|
+
this.keyRetentionDurationSeconds = keyRetentionDurationSeconds;
|
|
73
73
|
}
|
|
74
74
|
sendJob(type, messageId, data) {
|
|
75
75
|
this.subscribeClient.publish(this.multiClientChannel, JSON.stringify({ type, messageId, data }));
|
|
@@ -116,7 +116,7 @@ let LocalJobScheduler = class LocalJobScheduler extends AbstractJobScheduler {
|
|
|
116
116
|
if (!this.redisClient) {
|
|
117
117
|
return;
|
|
118
118
|
}
|
|
119
|
-
await this.redisClient.setex(this.holdMutexKey, this.
|
|
119
|
+
await this.redisClient.setex(this.holdMutexKey, this.keyRetentionDurationSeconds, 1);
|
|
120
120
|
this.logger.debug("Scheduler extends mutex key");
|
|
121
121
|
}
|
|
122
122
|
async afterJob({ at, ...job }, tx) {
|
|
@@ -139,15 +139,15 @@ let LocalJobScheduler = class LocalJobScheduler extends AbstractJobScheduler {
|
|
|
139
139
|
//-- try to claim active scheduler
|
|
140
140
|
const locker = new Redlock([this.redisClient]);
|
|
141
141
|
try {
|
|
142
|
-
const lock = await locker.acquire([this.lockMutexKey], this.
|
|
142
|
+
const lock = await locker.acquire([this.lockMutexKey], this.keyRetentionDurationSeconds * 1000);
|
|
143
143
|
this.isActive = true;
|
|
144
144
|
await this.extendMutexKey();
|
|
145
145
|
await lock.release();
|
|
146
|
-
this.logger.debug("
|
|
146
|
+
this.logger.debug("Became active scheduler");
|
|
147
147
|
//-- actively hold the mutex key
|
|
148
148
|
this.mutexHoldInterval = setInterval(() => {
|
|
149
149
|
this.extendMutexKey();
|
|
150
|
-
}, Math.trunc((this.
|
|
150
|
+
}, Math.trunc((this.keyRetentionDurationSeconds * 1000) / 2) + 1);
|
|
151
151
|
}
|
|
152
152
|
catch (err) {
|
|
153
153
|
this.logger.info("Failed to lock mutex key, ignore", err);
|
|
@@ -35,10 +35,10 @@ let ClaireServer = class ClaireServer extends ClaireApp {
|
|
|
35
35
|
}
|
|
36
36
|
//-- save mounted endpoint info in server store to prevent circular dependency when resolving http request handler in controller
|
|
37
37
|
for (const endpoint of mountedEndpointInfo) {
|
|
38
|
-
this.logger.
|
|
38
|
+
this.logger.debug(`Mounting: ${endpoint.method}:${endpoint.mount}`);
|
|
39
39
|
}
|
|
40
40
|
getGlobalStore().mountedEndpointInfo = mountedEndpointInfo;
|
|
41
|
-
this.logger.
|
|
41
|
+
this.logger.info("Claire server initing");
|
|
42
42
|
//-- handle exceptions
|
|
43
43
|
this.logger.debug(`Setting up exception handlers`);
|
|
44
44
|
process.on("SIGTERM", () => {
|