@cap-js-community/event-queue 1.6.2 → 1.6.4

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.6.2",
3
+ "version": "1.6.4",
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",
@@ -63,9 +63,6 @@
63
63
  "prettier": "^2.8.8",
64
64
  "sqlite3": "^5.1.7"
65
65
  },
66
- "peerDependencies": {
67
- "@cap-js/telemetry": "^0.2.2"
68
- },
69
66
  "homepage": "https://cap-js-community.github.io/event-queue/",
70
67
  "repository": {
71
68
  "type": "git",
@@ -384,38 +384,46 @@ class EventQueueProcessorBase {
384
384
  * The function accepts no arguments as there are dedicated functions to set the status of events (e.g. setEventStatus)
385
385
  */
386
386
  async persistEventStatus(tx, { skipChecks, statusMap = this.__statusMap } = {}) {
387
- return await trace(this.baseContext, "persist-event-status", async () => {
388
- this.logger.debug("entering persistEventStatus", {
387
+ this.logger.debug("entering persistEventStatus", {
388
+ eventType: this.#eventType,
389
+ eventSubType: this.#eventSubType,
390
+ });
391
+ this.#ensureOnlySelectedQueueEntries(statusMap);
392
+ if (!skipChecks) {
393
+ this.#ensureEveryQueueEntryHasStatus();
394
+ }
395
+ this.#ensureEveryStatusIsAllowed(statusMap);
396
+
397
+ const { success, failed, exceeded, invalidAttempts } = Object.entries(statusMap).reduce(
398
+ (result, [notificationEntityId, processingStatus]) => {
399
+ this.__commitedStatusMap[notificationEntityId] = processingStatus;
400
+ if (processingStatus === EventProcessingStatus.Open) {
401
+ result.invalidAttempts.push(notificationEntityId);
402
+ } else if (processingStatus === EventProcessingStatus.Done) {
403
+ result.success.push(notificationEntityId);
404
+ } else if (processingStatus === EventProcessingStatus.Error) {
405
+ result.failed.push(notificationEntityId);
406
+ } else if (processingStatus === EventProcessingStatus.Exceeded) {
407
+ result.exceeded.push(notificationEntityId);
408
+ }
409
+ return result;
410
+ },
411
+ {
412
+ success: [],
413
+ failed: [],
414
+ exceeded: [],
415
+ invalidAttempts: [],
416
+ }
417
+ );
418
+ if (![success, failed, exceeded, invalidAttempts].some((statusArray) => statusArray.length)) {
419
+ this.logger.debug("exiting persistEventStatus", {
389
420
  eventType: this.#eventType,
390
421
  eventSubType: this.#eventSubType,
391
422
  });
392
- this.#ensureOnlySelectedQueueEntries(statusMap);
393
- if (!skipChecks) {
394
- this.#ensureEveryQueueEntryHasStatus();
395
- }
396
- this.#ensureEveryStatusIsAllowed(statusMap);
397
-
398
- const { success, failed, exceeded, invalidAttempts } = Object.entries(statusMap).reduce(
399
- (result, [notificationEntityId, processingStatus]) => {
400
- this.__commitedStatusMap[notificationEntityId] = processingStatus;
401
- if (processingStatus === EventProcessingStatus.Open) {
402
- result.invalidAttempts.push(notificationEntityId);
403
- } else if (processingStatus === EventProcessingStatus.Done) {
404
- result.success.push(notificationEntityId);
405
- } else if (processingStatus === EventProcessingStatus.Error) {
406
- result.failed.push(notificationEntityId);
407
- } else if (processingStatus === EventProcessingStatus.Exceeded) {
408
- result.exceeded.push(notificationEntityId);
409
- }
410
- return result;
411
- },
412
- {
413
- success: [],
414
- failed: [],
415
- exceeded: [],
416
- invalidAttempts: [],
417
- }
418
- );
423
+ return;
424
+ }
425
+
426
+ return await trace(this.baseContext, "persist-event-status", async () => {
419
427
  this.logger.debug("persistEventStatus for entries", {
420
428
  eventType: this.#eventType,
421
429
  eventSubType: this.#eventSubType,
@@ -455,10 +463,6 @@ class EventQueueProcessorBase {
455
463
  .where("ID IN", eventIds)
456
464
  );
457
465
  }
458
- this.logger.debug("exiting persistEventStatus", {
459
- eventType: this.#eventType,
460
- eventSubType: this.#eventSubType,
461
- });
462
466
  });
463
467
  }
464
468
 
@@ -916,7 +920,7 @@ class EventQueueProcessorBase {
916
920
  return;
917
921
  }
918
922
  try {
919
- await trace(this.baseContext, "persist-release-lock", async () => {
923
+ await trace(this.baseContext, "release-lock", async () => {
920
924
  await distributedLock.releaseLock(this.context, [this.#eventType, this.#eventSubType].join("##"));
921
925
  });
922
926
  } catch (err) {
@@ -1156,6 +1160,10 @@ class EventQueueProcessorBase {
1156
1160
  get isPeriodicEvent() {
1157
1161
  return this.#eventConfig.isPeriodic;
1158
1162
  }
1163
+
1164
+ get eventConfig() {
1165
+ return this.#eventConfig;
1166
+ }
1159
1167
  }
1160
1168
 
1161
1169
  module.exports = EventQueueProcessorBase;
package/src/config.js CHANGED
@@ -282,6 +282,7 @@ class Config {
282
282
  checkForNextChunk: config.checkForNextChunk,
283
283
  deleteFinishedEventsAfterDays: config.deleteFinishedEventsAfterDays,
284
284
  appNames: config.appNames,
285
+ useEventQueueUser: config.useEventQueueUser,
285
286
  internalEvent: true,
286
287
  };
287
288
  eventConfig._appNameMap = eventConfig.appNames
package/src/index.d.ts CHANGED
@@ -63,6 +63,24 @@ type EventConfigType = {
63
63
  isPeriodic: boolean;
64
64
  };
65
65
 
66
+ export type EventConfig = {
67
+ type: string;
68
+ subType: string;
69
+ load: number;
70
+ impl: string;
71
+ selectMaxChunkSize: number | undefined;
72
+ parallelEventProcessing: boolean;
73
+ retryAttempts: number | undefined;
74
+ transactionMode: string | undefined;
75
+ processAfterCommit: boolean | undefined;
76
+ eventOutdatedCheck: boolean | undefined;
77
+ checkForNextChunk: boolean | undefined;
78
+ deleteFinishedEventsAfterDays: number | undefined;
79
+ appNames: string[] | undefined;
80
+ useEventQueueUser: boolean | undefined;
81
+ internalEvent: true;
82
+ };
83
+
66
84
  // Define Status Type
67
85
  type Status = 0 | 1 | 2 | 3 | 4;
68
86
 
@@ -126,6 +144,7 @@ export declare class EventQueueProcessorBase {
126
144
  get isPeriodicEvent(): boolean;
127
145
  get eventType(): String;
128
146
  get eventSubType(): String;
147
+ get eventConfig(): EventConfig;
129
148
  }
130
149
 
131
150
  export function publishEvent(
@@ -5,6 +5,7 @@ const cds = require("@sap/cds");
5
5
  const EventQueueBaseClass = require("../EventQueueProcessorBase");
6
6
  const { EventProcessingStatus } = require("../constants");
7
7
  const common = require("../shared/common");
8
+ const config = require("../config");
8
9
 
9
10
  const COMPONENT_NAME = "/eventQueue/outbox/generic";
10
11
 
@@ -18,7 +19,8 @@ class EventQueueGenericOutboxHandler extends EventQueueBaseClass {
18
19
  let status = EventProcessingStatus.Done;
19
20
  try {
20
21
  const service = await cds.connect.to(this.eventSubType);
21
- const userId = payload.contextUser;
22
+ const { useEventQueueUser } = this.eventConfig;
23
+ const userId = useEventQueueUser ? config.userId : payload.contextUser;
22
24
  const msg = payload._fromSend ? new cds.Request(payload) : new cds.Event(payload);
23
25
  const invocationFn = payload._fromSend ? "send" : "emit";
24
26
  delete msg._fromSend;
@@ -10,6 +10,8 @@ try {
10
10
 
11
11
  const config = require("../config");
12
12
 
13
+ const COMPONENT_NAME = "/shared/openTelemetry";
14
+
13
15
  const trace = async (context, label, fn, { attributes = {}, newRootSpan = false } = {}) => {
14
16
  if (!config.enableCAPTelemetry || !otel || !cds._telemetry?.tracer) {
15
17
  return fn();
@@ -34,8 +36,14 @@ const trace = async (context, label, fn, { attributes = {}, newRootSpan = false
34
36
  throw e;
35
37
  };
36
38
  const onDone = () => {
37
- if (span.status.code !== otel.SpanStatusCode.UNSET && !span.ended) {
38
- span.end();
39
+ try {
40
+ if (span.status?.code !== otel.SpanStatusCode.UNSET && !span.ended) {
41
+ span.end?.();
42
+ }
43
+ } catch (err) {
44
+ cds.log(COMPONENT_NAME).error("error in tracing", err, {
45
+ span,
46
+ });
39
47
  }
40
48
  };
41
49