@cap-js-community/event-queue 1.3.1 → 1.3.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cap-js-community/event-queue",
3
- "version": "1.3.1",
3
+ "version": "1.3.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
  "files": [
@@ -44,12 +44,12 @@
44
44
  "dependencies": {
45
45
  "redis": "4.6.13",
46
46
  "verror": "1.10.1",
47
- "yaml": "2.3.4"
47
+ "yaml": "2.4.0"
48
48
  },
49
49
  "devDependencies": {
50
- "@cap-js/hana": "^0.0.5",
50
+ "@cap-js/hana": "^0.0.6",
51
51
  "@cap-js/sqlite": "^1.5.0",
52
- "@sap/cds": "^7.5.3",
52
+ "@sap/cds": "^7.7.0",
53
53
  "@sap/cds-dk": "^7.5.1",
54
54
  "eslint": "^8.56.0",
55
55
  "eslint-config-prettier": "^9.1.0",
package/src/config.js CHANGED
@@ -28,7 +28,7 @@ const BASE_PERIODIC_EVENTS = [
28
28
  subType: "DELETE_EVENTS",
29
29
  priority: Priorities.Low,
30
30
  impl: "./housekeeping/EventQueueDeleteEvents",
31
- load: 1,
31
+ load: 20,
32
32
  interval: 86400, // 1 day,
33
33
  internalEvent: true,
34
34
  },
package/src/initialize.js CHANGED
@@ -30,7 +30,7 @@ const CONFIG_VARS = [
30
30
  ["registerAsEventProcessor", true],
31
31
  ["processEventsAfterPublish", true],
32
32
  ["isEventQueueActive", true],
33
- ["runInterval", 5 * 60 * 1000],
33
+ ["runInterval", 25 * 60 * 1000],
34
34
  ["tableNameEventQueue", BASE_TABLES.EVENT],
35
35
  ["tableNameEventLock", BASE_TABLES.LOCK],
36
36
  ["disableRedis", false],
@@ -84,21 +84,19 @@ const initialize = async ({
84
84
  );
85
85
 
86
86
  const logger = cds.log(COMPONENT);
87
- config.fileContent = await readConfigFromFile(config.configFilePath);
88
87
  config.checkRedisEnabled();
88
+ cds.on("connect", (service) => {
89
+ if (service.name === "db") {
90
+ config.processEventsAfterPublish && dbHandler.registerEventQueueDbHandler(service);
91
+ config.cleanupLocksAndEventsForDev && registerCleanupForDevDb().catch(() => {});
92
+ registerEventProcessors();
93
+ }
94
+ });
95
+ config.fileContent = await readConfigFromFile(config.configFilePath);
89
96
 
90
- if (config.processEventsAfterPublish) {
91
- cds.on("connect", (service) => {
92
- if (service.name === "db") {
93
- dbHandler.registerEventQueueDbHandler(service);
94
- config.cleanupLocksAndEventsForDev && registerCleanupForDevDb().catch(() => {});
95
- }
96
- });
97
- }
98
97
  !config.skipCsnCheck && (await csnCheck());
99
98
 
100
99
  monkeyPatchCAPOutbox();
101
- registerEventProcessors();
102
100
  registerCdsShutdown();
103
101
  logger.info("event queue initialized", {
104
102
  registerAsEventProcessor: config.registerAsEventProcessor,
@@ -68,21 +68,30 @@ function unboxed(srv) {
68
68
  return srv[UNBOXED] || srv;
69
69
  }
70
70
 
71
- const _mapToEventAndPublish = async (context, name, msg) => {
71
+ const _mapToEventAndPublish = async (context, name, req) => {
72
+ let startAfter;
73
+ for (const header in req.headers ?? {}) {
74
+ if (header.toLocaleLowerCase() === "x-eventqueue-startafter") {
75
+ startAfter = req.headers[header];
76
+ delete req.headers[header];
77
+ break;
78
+ }
79
+ }
72
80
  const event = {
73
81
  contextUser: context.user.id,
74
- ...(msg._fromSend || (msg.reply && { _fromSend: true })), // send or emit
75
- ...(msg.inbound && { inbound: msg.inbound }),
76
- ...(msg.event && { event: msg.event }),
77
- ...(msg.data && { data: msg.data }),
78
- ...(msg.headers && { headers: msg.headers }),
79
- ...(msg.query && { query: msg.query }),
82
+ ...(req._fromSend || (req.reply && { _fromSend: true })), // send or emit
83
+ ...(req.inbound && { inbound: req.inbound }),
84
+ ...(req.event && { event: req.event }),
85
+ ...(req.data && { data: req.data }),
86
+ ...(req.headers && { headers: req.headers }),
87
+ ...(req.query && { query: req.query }),
80
88
  };
81
89
 
82
90
  await publishEvent(cds.tx(context), {
83
91
  type: CDS_EVENT_TYPE,
84
92
  subType: name,
85
93
  payload: JSON.stringify(event),
94
+ ...(startAfter && { startAfter }),
86
95
  });
87
96
  };
88
97
 
@@ -11,7 +11,6 @@ const { limiter } = require("./shared/common");
11
11
  const { executeInNewTransaction, TriggerRollback } = require("./shared/cdsHelper");
12
12
 
13
13
  const COMPONENT_NAME = "/eventQueue/processEventQueue";
14
- const MAX_EXECUTION_TIME = 5 * 60 * 1000;
15
14
 
16
15
  const processEventQueue = async (context, eventType, eventSubType, startTime = new Date()) => {
17
16
  let iterationCounter = 0;
@@ -115,7 +114,7 @@ const reevaluateShouldContinue = (eventTypeInstance, iterationCounter, startTime
115
114
  if (eventTypeInstance.emptyChunkSelected) {
116
115
  return false; // the last selected chunk was empty - no more data for processing
117
116
  }
118
- if (new Date(startTime.getTime() + MAX_EXECUTION_TIME) > new Date()) {
117
+ if (new Date(startTime.getTime() + config.runInterval) > new Date()) {
119
118
  return true;
120
119
  }
121
120
  eventTypeInstance.logTimeExceeded(iterationCounter);
@@ -57,6 +57,13 @@ async function executeInNewTransaction(context = {}, transactionTag, fn, args, {
57
57
  );
58
58
  } else {
59
59
  contextTx.context.user = user;
60
+ try {
61
+ contextTx.set?.({
62
+ "$user.id": user.id,
63
+ });
64
+ } catch {
65
+ /* empty */
66
+ }
60
67
  await fn(contextTx, ...parameters);
61
68
  }
62
69
  }