@cap-js-community/event-queue 1.5.1 → 1.5.3
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/cds-plugin.js +1 -1
- package/package.json +4 -4
- package/src/initialize.js +20 -16
- package/src/redis/redisPub.js +61 -53
- package/src/runner/runner.js +42 -27
- package/src/shared/WorkerQueue.js +1 -1
- package/src/shared/eventScheduler.js +5 -7
package/cds-plugin.js
CHANGED
|
@@ -5,6 +5,6 @@ const cds = require("@sap/cds");
|
|
|
5
5
|
const eventQueue = require("./src");
|
|
6
6
|
const COMPONENT_NAME = "/eventQueue/plugin";
|
|
7
7
|
|
|
8
|
-
if (!cds.build?.register && cds.env.eventQueue) {
|
|
8
|
+
if (!cds.build?.register && Object.keys(cds.env.eventQueue ?? {}).length) {
|
|
9
9
|
module.exports = eventQueue.initialize().catch((err) => cds.log(COMPONENT_NAME).error(err));
|
|
10
10
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cap-js-community/event-queue",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.3",
|
|
4
4
|
"description": "An event queue that enables secure transactional processing of asynchronous and periodic events, featuring instant event processing with Redis Pub/Sub and load distribution across all application instances.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"types": "src/index.d.ts",
|
|
@@ -49,13 +49,13 @@
|
|
|
49
49
|
"yaml": "^2.4.2"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
|
-
"@cap-js/hana": "^0.
|
|
53
|
-
"@cap-js/sqlite": "^1.7.
|
|
52
|
+
"@cap-js/hana": "^1.0.0",
|
|
53
|
+
"@cap-js/sqlite": "^1.7.2",
|
|
54
54
|
"@sap/cds": "^7.9.2",
|
|
55
55
|
"@sap/cds-dk": "^7.8.0",
|
|
56
56
|
"eslint": "^8.57.0",
|
|
57
57
|
"eslint-config-prettier": "^9.1.0",
|
|
58
|
-
"eslint-plugin-jest": "^
|
|
58
|
+
"eslint-plugin-jest": "^28.6.0",
|
|
59
59
|
"eslint-plugin-node": "^11.1.0",
|
|
60
60
|
"express": "^4.18.2",
|
|
61
61
|
"hdb": "^0.19.7",
|
package/src/initialize.js
CHANGED
|
@@ -131,22 +131,7 @@ const readConfigFromFile = async (configFilepath) => {
|
|
|
131
131
|
};
|
|
132
132
|
|
|
133
133
|
const registerEventProcessors = () => {
|
|
134
|
-
|
|
135
|
-
cds.connect
|
|
136
|
-
.to("cds.xt.DeploymentService")
|
|
137
|
-
.then((ds) => {
|
|
138
|
-
cds.log(COMPONENT).info("event-queue unsubscribe handler registered", {
|
|
139
|
-
redisEnabled: config.redisEnabled,
|
|
140
|
-
});
|
|
141
|
-
ds.after("unsubscribe", async (_, req) => {
|
|
142
|
-
const { tenant } = req.data;
|
|
143
|
-
config.handleUnsubscribe(tenant);
|
|
144
|
-
});
|
|
145
|
-
})
|
|
146
|
-
.catch(
|
|
147
|
-
() => {} // ignore errors as the DeploymentService is most of the time only available in the mtx sidecar
|
|
148
|
-
);
|
|
149
|
-
});
|
|
134
|
+
_registerUnsubscribe();
|
|
150
135
|
config.redisEnabled && config.attachRedisUnsubscribeHandler();
|
|
151
136
|
|
|
152
137
|
if (!config.registerAsEventProcessor) {
|
|
@@ -215,6 +200,25 @@ const registerCleanupForDevDb = async () => {
|
|
|
215
200
|
}
|
|
216
201
|
};
|
|
217
202
|
|
|
203
|
+
const _registerUnsubscribe = () => {
|
|
204
|
+
cds.on("listening", () => {
|
|
205
|
+
cds.connect
|
|
206
|
+
.to("cds.xt.DeploymentService")
|
|
207
|
+
.then((ds) => {
|
|
208
|
+
cds.log(COMPONENT).info("event-queue unsubscribe handler registered", {
|
|
209
|
+
redisEnabled: config.redisEnabled,
|
|
210
|
+
});
|
|
211
|
+
ds.after("unsubscribe", async (_, req) => {
|
|
212
|
+
const { tenant } = req.data;
|
|
213
|
+
config.handleUnsubscribe(tenant);
|
|
214
|
+
});
|
|
215
|
+
})
|
|
216
|
+
.catch(
|
|
217
|
+
() => {} // ignore errors as the DeploymentService is most of the time only available in the mtx sidecar
|
|
218
|
+
);
|
|
219
|
+
});
|
|
220
|
+
};
|
|
221
|
+
|
|
218
222
|
module.exports = {
|
|
219
223
|
initialize,
|
|
220
224
|
};
|
package/src/redis/redisPub.js
CHANGED
|
@@ -9,6 +9,7 @@ const { checkLockExistsAndReturnValue } = require("../shared/distributedLock");
|
|
|
9
9
|
const config = require("../config");
|
|
10
10
|
const common = require("../shared/common");
|
|
11
11
|
const { runEventCombinationForTenant } = require("../runner/runnerHelper");
|
|
12
|
+
const trace = require("../shared/openTelemetry");
|
|
12
13
|
|
|
13
14
|
const EVENT_MESSAGE_CHANNEL = "EVENT_QUEUE_MESSAGE_CHANNEL";
|
|
14
15
|
const COMPONENT_NAME = "/eventQueue/redisPub";
|
|
@@ -19,67 +20,53 @@ const wait = promisify(setTimeout);
|
|
|
19
20
|
|
|
20
21
|
const broadcastEvent = async (tenantId, events) => {
|
|
21
22
|
const logger = cds.log(COMPONENT_NAME);
|
|
23
|
+
|
|
24
|
+
if (!config.isEventQueueActive) {
|
|
25
|
+
cds.log(COMPONENT_NAME).info("event-queue is deactivated, broadcasting is skipped!");
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
22
29
|
events = Array.isArray(events) ? events : [events];
|
|
23
30
|
try {
|
|
24
|
-
if (!config.isEventQueueActive) {
|
|
25
|
-
cds.log(COMPONENT_NAME).info("Skipping processing because runner is deactivated!", {});
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
31
|
if (!config.redisEnabled) {
|
|
29
|
-
|
|
30
|
-
let context = {};
|
|
31
|
-
if (tenantId) {
|
|
32
|
-
const user = await cds.tx({ tenant: tenantId }, async () => {
|
|
33
|
-
return new cds.User.Privileged({ id: config.userId, authInfo: await common.getAuthInfo(tenantId) });
|
|
34
|
-
});
|
|
35
|
-
context = {
|
|
36
|
-
tenant: tenantId,
|
|
37
|
-
user,
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return await cds.tx(context, async ({ context }) => {
|
|
42
|
-
for (const { type, subType } of events) {
|
|
43
|
-
await runEventCombinationForTenant(context, type, subType, { shouldTrace: true });
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
}
|
|
32
|
+
await _processLocalWithoutRedis(tenantId, events);
|
|
47
33
|
return;
|
|
48
34
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
35
|
+
await cds.tx({ tenant: tenantId }, async ({ context }) => {
|
|
36
|
+
await trace(context, "broadcast-inserted-events", async () => {
|
|
37
|
+
for (const { type, subType } of events) {
|
|
38
|
+
const eventConfig = config.getEventConfig(type, subType);
|
|
39
|
+
for (let i = 0; i < TRIES_FOR_PUBLISH_PERIODIC_EVENT; i++) {
|
|
40
|
+
const result = await checkLockExistsAndReturnValue(context, [type, subType].join("##"));
|
|
41
|
+
if (result) {
|
|
42
|
+
logger.debug("skip publish redis event as no lock is available", {
|
|
43
|
+
type,
|
|
44
|
+
subType,
|
|
45
|
+
index: i,
|
|
46
|
+
isPeriodic: eventConfig.isPeriodic,
|
|
47
|
+
waitInterval: SLEEP_TIME_FOR_PUBLISH_PERIODIC_EVENT,
|
|
48
|
+
});
|
|
49
|
+
if (!eventConfig.isPeriodic) {
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
await wait(SLEEP_TIME_FOR_PUBLISH_PERIODIC_EVENT);
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
logger.debug("publishing redis event", {
|
|
56
|
+
tenantId,
|
|
57
|
+
type,
|
|
58
|
+
subType,
|
|
59
|
+
});
|
|
60
|
+
await redis.publishMessage(
|
|
61
|
+
config.redisOptions,
|
|
62
|
+
EVENT_MESSAGE_CHANNEL,
|
|
63
|
+
JSON.stringify({ lockId: cds.utils.uuid(), tenantId, type, subType })
|
|
64
|
+
);
|
|
65
65
|
break;
|
|
66
66
|
}
|
|
67
|
-
await wait(SLEEP_TIME_FOR_PUBLISH_PERIODIC_EVENT);
|
|
68
|
-
continue;
|
|
69
67
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
type,
|
|
73
|
-
subType,
|
|
74
|
-
});
|
|
75
|
-
await redis.publishMessage(
|
|
76
|
-
config.redisOptions,
|
|
77
|
-
EVENT_MESSAGE_CHANNEL,
|
|
78
|
-
JSON.stringify({ lockId: cds.utils.uuid(), tenantId, type, subType })
|
|
79
|
-
);
|
|
80
|
-
break;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
68
|
+
});
|
|
69
|
+
});
|
|
83
70
|
} catch (err) {
|
|
84
71
|
logger.error("publish events failed!", err, {
|
|
85
72
|
tenantId,
|
|
@@ -87,6 +74,27 @@ const broadcastEvent = async (tenantId, events) => {
|
|
|
87
74
|
}
|
|
88
75
|
};
|
|
89
76
|
|
|
77
|
+
const _processLocalWithoutRedis = async (tenantId, events) => {
|
|
78
|
+
if (config.registerAsEventProcessor) {
|
|
79
|
+
let context = {};
|
|
80
|
+
if (tenantId) {
|
|
81
|
+
const user = await cds.tx({ tenant: tenantId }, async () => {
|
|
82
|
+
return new cds.User.Privileged({ id: config.userId, authInfo: await common.getAuthInfo(tenantId) });
|
|
83
|
+
});
|
|
84
|
+
context = {
|
|
85
|
+
tenant: tenantId,
|
|
86
|
+
user,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
for (const { type, subType } of events) {
|
|
91
|
+
await cds.tx(context, async ({ context }) => {
|
|
92
|
+
await runEventCombinationForTenant(context, type, subType, { shouldTrace: true });
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
|
|
90
98
|
module.exports = {
|
|
91
99
|
broadcastEvent,
|
|
92
100
|
};
|
package/src/runner/runner.js
CHANGED
|
@@ -22,7 +22,7 @@ const EVENT_QUEUE_RUN_ID = "EVENT_QUEUE_RUN_ID";
|
|
|
22
22
|
const EVENT_QUEUE_RUN_TS = "EVENT_QUEUE_RUN_TS";
|
|
23
23
|
const EVENT_QUEUE_RUN_REDIS_CHECK = "EVENT_QUEUE_RUN_REDIS_CHECK";
|
|
24
24
|
const EVENT_QUEUE_UPDATE_PERIODIC_EVENTS = "EVENT_QUEUE_UPDATE_PERIODIC_EVENTS";
|
|
25
|
-
|
|
25
|
+
let OFFSET_FIRST_RUN = 10 * 1000;
|
|
26
26
|
|
|
27
27
|
let tenantIdHash;
|
|
28
28
|
let singleRunDone;
|
|
@@ -160,7 +160,6 @@ const _executeEventsAllTenantsRedis = async (tenantIds) => {
|
|
|
160
160
|
|
|
161
161
|
const _executeEventsAllTenants = async (tenantIds, runId) => {
|
|
162
162
|
const promises = [];
|
|
163
|
-
|
|
164
163
|
for (const tenantId of tenantIds) {
|
|
165
164
|
const id = cds.utils.uuid();
|
|
166
165
|
let tenantContext;
|
|
@@ -254,32 +253,46 @@ const _executePeriodicEventsAllTenants = async (tenantIds) => {
|
|
|
254
253
|
}
|
|
255
254
|
};
|
|
256
255
|
|
|
257
|
-
const _singleTenantDb = async (
|
|
258
|
-
|
|
259
|
-
|
|
256
|
+
const _singleTenantDb = async () => {
|
|
257
|
+
const id = cds.utils.uuid();
|
|
258
|
+
const events = await trace(
|
|
259
|
+
{ id },
|
|
260
|
+
"fetch-openEvents-and-authInfo",
|
|
261
|
+
async () => {
|
|
262
|
+
return await cds.tx({}, async (tx) => {
|
|
263
|
+
return await openEvents.getOpenQueueEntries(tx);
|
|
264
|
+
});
|
|
265
|
+
},
|
|
266
|
+
{ newRootSpan: true }
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
return await Promise.allSettled(
|
|
270
|
+
events.map(async (openEvent) => {
|
|
271
|
+
const eventConfig = config.getEventConfig(openEvent.type, openEvent.subType);
|
|
260
272
|
const label = `${eventConfig.type}_${eventConfig.subType}`;
|
|
261
|
-
const user = new cds.User.Privileged(config.userId);
|
|
262
|
-
const tenantContext = {
|
|
263
|
-
tenant: tenantId,
|
|
264
|
-
user,
|
|
265
|
-
};
|
|
266
273
|
return await WorkerQueue.instance.addToQueue(eventConfig.load, label, eventConfig.priority, async () => {
|
|
267
|
-
return await cds.tx(
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
274
|
+
return await cds.tx({}, async ({ context }) => {
|
|
275
|
+
await trace(
|
|
276
|
+
context,
|
|
277
|
+
label,
|
|
278
|
+
async () => {
|
|
279
|
+
try {
|
|
280
|
+
const lockId = `${label}`;
|
|
281
|
+
const couldAcquireLock = await distributedLock.acquireLock(context, lockId, {
|
|
282
|
+
expiryTime: eventQueueConfig.runInterval * 0.95,
|
|
283
|
+
});
|
|
284
|
+
if (!couldAcquireLock) {
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
await runEventCombinationForTenant(context, eventConfig.type, eventConfig.subType, {
|
|
288
|
+
skipWorkerPool: true,
|
|
289
|
+
});
|
|
290
|
+
} catch (err) {
|
|
291
|
+
cds.log(COMPONENT_NAME).error("executing event-queue run for tenant failed");
|
|
292
|
+
}
|
|
293
|
+
},
|
|
294
|
+
{ newRootSpan: true }
|
|
295
|
+
);
|
|
283
296
|
});
|
|
284
297
|
});
|
|
285
298
|
})
|
|
@@ -387,7 +400,8 @@ const _multiTenancyPeriodicEvents = async (tenantIds) => {
|
|
|
387
400
|
}
|
|
388
401
|
};
|
|
389
402
|
|
|
390
|
-
const _checkPeriodicEventsSingleTenantOneTime = () =>
|
|
403
|
+
const _checkPeriodicEventsSingleTenantOneTime = async () =>
|
|
404
|
+
eventQueueConfig.updatePeriodicEvents &&
|
|
391
405
|
cds.tx({}, async (tx) => await periodicEvents.checkAndInsertPeriodicEvents(tx.context));
|
|
392
406
|
|
|
393
407
|
const _checkPeriodicEventsSingleTenant = async (context) => {
|
|
@@ -425,5 +439,6 @@ module.exports = {
|
|
|
425
439
|
_acquireRunId,
|
|
426
440
|
EVENT_QUEUE_RUN_TS,
|
|
427
441
|
clearHash: () => (tenantIdHash = null),
|
|
442
|
+
setOffsetFirstRun: (value) => (OFFSET_FIRST_RUN = value),
|
|
428
443
|
},
|
|
429
444
|
};
|
|
@@ -113,7 +113,7 @@ class WorkerQueue {
|
|
|
113
113
|
}
|
|
114
114
|
|
|
115
115
|
#checkForNext() {
|
|
116
|
-
if (!this.#queue.length
|
|
116
|
+
if (!Object.values(this.#queue).some((queue) => queue.length) || this.#runningLoad === this.#concurrencyLimit) {
|
|
117
117
|
return;
|
|
118
118
|
}
|
|
119
119
|
|
|
@@ -28,9 +28,11 @@ class EventScheduler {
|
|
|
28
28
|
delaySeconds: (date.getTime() - Date.now()) / 1000,
|
|
29
29
|
});
|
|
30
30
|
this.#eventsByTenants[tenantId] ??= {};
|
|
31
|
-
let timeoutId;
|
|
32
31
|
const timeout = setTimeout(() => {
|
|
33
|
-
|
|
32
|
+
// NOTE: needed due to a bug in node.js which is leaking memory; will be fixed in v22.4.0
|
|
33
|
+
// https://github.com/nodejs/node/pull/53337/files
|
|
34
|
+
clearTimeout(timeout);
|
|
35
|
+
delete this.#eventsByTenants[tenantId][timeout];
|
|
34
36
|
delete this.#scheduledEvents[key];
|
|
35
37
|
redisPub.broadcastEvent(tenantId, { type, subType }).catch((err) => {
|
|
36
38
|
cds.log(COMPONENT_NAME).error("could not execute scheduled event", err, {
|
|
@@ -41,11 +43,7 @@ class EventScheduler {
|
|
|
41
43
|
});
|
|
42
44
|
});
|
|
43
45
|
}, relative).unref();
|
|
44
|
-
|
|
45
|
-
// and the closure. The usage of the timeout object in the callback, leads to a deadlock for the garbage collector
|
|
46
|
-
// as the timeout object has a reference to the callback of setTimeout.
|
|
47
|
-
timeoutId = String(timeout);
|
|
48
|
-
this.#eventsByTenants[tenantId][timeoutId] = true;
|
|
46
|
+
this.#eventsByTenants[tenantId][timeout] = true;
|
|
49
47
|
}
|
|
50
48
|
|
|
51
49
|
clearForTenant(tenantId) {
|