@cap-js-community/event-queue 1.5.0 → 1.5.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/package.json +4 -4
- package/src/dbHandler.js +3 -3
- package/src/redis/redisPub.js +61 -53
- package/src/shared/eventScheduler.js +6 -3
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.2",
|
|
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/dbHandler.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const cds = require("@sap/cds");
|
|
4
4
|
|
|
5
|
-
const
|
|
5
|
+
const redisPub = require("./redis/redisPub");
|
|
6
6
|
const config = require("./config");
|
|
7
7
|
|
|
8
8
|
const COMPONENT_NAME = "/eventQueue/dbHandler";
|
|
@@ -25,7 +25,7 @@ const registerEventQueueDbHandler = (dbService) => {
|
|
|
25
25
|
req.tx._ = req.tx._ ?? {};
|
|
26
26
|
req.tx._.eventQueuePublishEvents = req.tx._.eventQueuePublishEvents ?? {};
|
|
27
27
|
const eventQueuePublishEvents = req.tx._.eventQueuePublishEvents;
|
|
28
|
-
const data = Array.isArray(req.
|
|
28
|
+
const data = Array.isArray(req.query.INSERT.entries) ? req.query.INSERT.entries : [req.query.INSERT.entries];
|
|
29
29
|
const eventCombinations = Object.keys(
|
|
30
30
|
data.reduce((result, event) => {
|
|
31
31
|
const key = [event.type, event.subType].join("##");
|
|
@@ -45,7 +45,7 @@ const registerEventQueueDbHandler = (dbService) => {
|
|
|
45
45
|
return { type, subType };
|
|
46
46
|
});
|
|
47
47
|
|
|
48
|
-
broadcastEvent(req.tenant, events).catch((err) => {
|
|
48
|
+
redisPub.broadcastEvent(req.tenant, events).catch((err) => {
|
|
49
49
|
cds.log(COMPONENT_NAME).error("db handler failure during broadcasting event", err, {
|
|
50
50
|
tenant: req.tenant,
|
|
51
51
|
events,
|
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
|
+
return await cds.tx(context, async ({ context }) => {
|
|
91
|
+
for (const { type, subType } of events) {
|
|
92
|
+
await runEventCombinationForTenant(context, type, subType, { shouldTrace: true });
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
|
|
90
98
|
module.exports = {
|
|
91
99
|
broadcastEvent,
|
|
92
100
|
};
|
|
@@ -28,8 +28,11 @@ class EventScheduler {
|
|
|
28
28
|
delaySeconds: (date.getTime() - Date.now()) / 1000,
|
|
29
29
|
});
|
|
30
30
|
this.#eventsByTenants[tenantId] ??= {};
|
|
31
|
-
const
|
|
32
|
-
|
|
31
|
+
const timeout = setTimeout(() => {
|
|
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];
|
|
33
36
|
delete this.#scheduledEvents[key];
|
|
34
37
|
redisPub.broadcastEvent(tenantId, { type, subType }).catch((err) => {
|
|
35
38
|
cds.log(COMPONENT_NAME).error("could not execute scheduled event", err, {
|
|
@@ -40,7 +43,7 @@ class EventScheduler {
|
|
|
40
43
|
});
|
|
41
44
|
});
|
|
42
45
|
}, relative).unref();
|
|
43
|
-
this.#eventsByTenants[tenantId][
|
|
46
|
+
this.#eventsByTenants[tenantId][timeout] = true;
|
|
44
47
|
}
|
|
45
48
|
|
|
46
49
|
clearForTenant(tenantId) {
|