@cap-js-community/event-queue 1.5.1 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cap-js-community/event-queue",
3
- "version": "1.5.1",
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.4.0",
53
- "@cap-js/sqlite": "^1.7.1",
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": "^27.9.0",
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",
@@ -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
- if (config.registerAsEventProcessor) {
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
- for (const { type, subType } of events) {
50
- const eventConfig = config.getEventConfig(type, subType);
51
- for (let i = 0; i < TRIES_FOR_PUBLISH_PERIODIC_EVENT; i++) {
52
- const result = await checkLockExistsAndReturnValue(
53
- new cds.EventContext({ tenant: tenantId }),
54
- [type, subType].join("##")
55
- );
56
- if (result) {
57
- logger.debug("skip publish redis event as no lock is available", {
58
- type,
59
- subType,
60
- index: i,
61
- isPeriodic: eventConfig.isPeriodic,
62
- waitInterval: SLEEP_TIME_FOR_PUBLISH_PERIODIC_EVENT,
63
- });
64
- if (!eventConfig.isPeriodic) {
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
- logger.debug("publishing redis event", {
71
- tenantId,
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,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
- delete this.#eventsByTenants[tenantId][timeoutId];
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
- // Convert the timeout object to a primitive timeout id to avoid circular dependencies between the callback of setTimeout
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) {