Skip to content

Commit 1ef18c7

Browse files
committed
refactor and add tests
1 parent 487da95 commit 1ef18c7

File tree

10 files changed

+127
-52
lines changed

10 files changed

+127
-52
lines changed

docs/configure-event/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ they should be processed.
4141
## Parameters
4242

4343
| Property | Description | Default Value |
44-
|-------------------------------| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------- |
44+
| ----------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------- |
4545
| impl | Path of the implementation class associated with the event. | - |
4646
| type | Specifies the type of the event. | - |
4747
| subType | Specifies the subtype of the event, further categorizing the event type. | - |

docs/setup/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ such as the configuration file path, event processing behavior, load balancing,
6262
The table includes the parameter name, a description of its purpose, and the default value if not specified.
6363

6464
| Name | Description | Default | Can be changed at runtime |
65-
|:-------------------------------------| :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | :------------- | :------------------------ |
65+
| :----------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | :------------- | :------------------------ |
6666
| configFilePath | Path to the configuration file. | null | no |
6767
| events | Options to allow events in the configuration. E.g. via cds-env. | {} | no |
6868
| periodicEvents | Options to allow periodicEvents in the configuration. E.g. via cds-env | {} | no |

package-lock.json

Lines changed: 39 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cap-js-community/event-queue",
3-
"version": "2.0.0-beta.4",
3+
"version": "2.0.0-beta.5",
44
"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.",
55
"main": "src/index.js",
66
"types": "src/index.d.ts",

src/EventQueueProcessorBase.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const TRIES_FOR_EXCEEDED_EVENTS = 3;
2525
const EVENT_START_AFTER_HEADROOM = 3 * 1000;
2626
const SUFFIX_PERIODIC = "_PERIODIC";
2727

28-
const ALLOWED_FIELDS_FOR_UPDATE = ["status", "startAfter"];
28+
const ALLOWED_FIELDS_FOR_UPDATE = ["status", "startAfter", "error"];
2929

3030
class EventQueueProcessorBase {
3131
#eventsWithExceededTries = [];
@@ -453,6 +453,7 @@ class EventQueueProcessorBase {
453453

454454
for (const { ids, data } of Object.values(updateData)) {
455455
if (!("status" in data)) {
456+
// TODO: Can this still happen?
456457
this.logger.error("can't find status value in return value of event-processing. Setting event to done", {
457458
ids,
458459
// NOTE: use first id as same return values are clustered
@@ -474,14 +475,17 @@ class EventQueueProcessorBase {
474475
data.startAfter = this.#normalizeDate(data.startAfter);
475476
}
476477

478+
if (data.error) {
479+
data.error = this.#error2String(data.error);
480+
}
481+
477482
if (!data.startAfter && [EventProcessingStatus.Error, EventProcessingStatus.Open].includes(data.status)) {
478483
data.startAfter = new Date(
479484
Date.now() +
480485
(EventProcessingStatus.Error ? this.#eventConfig.retryFailedAfter : this.#eventConfig.retryOpenAfter)
481486
);
482487
}
483488

484-
// TODO: check that startAfter is only allowed for tbp events
485489
if (data.startAfter) {
486490
this.#eventSchedulerInstance.scheduleEvent(
487491
this.__context.tenant,
@@ -504,6 +508,28 @@ class EventQueueProcessorBase {
504508
});
505509
}
506510

511+
#error2String(error) {
512+
return JSON.stringify(error, (_, value) => this.#errorReplacer(value));
513+
}
514+
515+
#errorReplacer(value) {
516+
if (!(value instanceof Error)) {
517+
return value;
518+
}
519+
520+
const plain = {
521+
name: value.name,
522+
message: value.message,
523+
stack: value.stack,
524+
};
525+
526+
for (const key in value) {
527+
plain[key] = value[key];
528+
}
529+
530+
return plain;
531+
}
532+
507533
#normalizeDate(value) {
508534
if (value instanceof Date) {
509535
return value;

src/outbox/EventQueueGenericOutboxHandler.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,13 @@ class EventQueueGenericOutboxHandler extends EventQueueBaseClass {
348348
this.logger.error("error processing outboxed service call", err, {
349349
serviceName: this.eventSubType,
350350
});
351-
return queueEntries.map((queueEntry) => [queueEntry.ID, EventProcessingStatus.Error]);
351+
return queueEntries.map((queueEntry) => [
352+
queueEntry.ID,
353+
{
354+
status: EventProcessingStatus.Error,
355+
error: err,
356+
},
357+
]);
352358
}
353359
}
354360

test/__snapshots__/eventQueueOutbox.test.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ exports[`event-queue outbox monkeyPatchCAPOutbox=true req error should be caught
278278
[
279279
[
280280
"error processing outboxed service call",
281-
[Error: error occured],
281+
[Error: error occurred],
282282
{
283283
"serviceName": "NotificationService",
284284
},
@@ -290,7 +290,7 @@ exports[`event-queue outbox monkeyPatchCAPOutbox=true req reject should be caugh
290290
[
291291
[
292292
"error processing outboxed service call",
293-
[Error: error occured],
293+
[Error: error occurred],
294294
{
295295
"serviceName": "NotificationService",
296296
},

test/asset/outboxProject/srv/service/service.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ class NotificationService extends cds.Service {
1515
});
1616

1717
this.on("rejectEvent", (req) => {
18-
req.reject(404, "error occured");
18+
req.reject(404, "error occurred");
1919
});
2020

2121
this.on("errorEvent", (req) => {
22-
req.error(404, "error occured");
22+
req.error(404, "error occurred");
2323
});
2424
}
2525
}

test/asset/outboxProject/srv/service/standard-service.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,37 +69,49 @@ class StandardService extends cds.Service {
6969
data: req.data,
7070
user: req.user.id,
7171
startAfter: req.data.startAfter,
72+
error: req.data.error,
7273
});
7374

7475
if (req.data.startAfter) {
7576
req.data.startAfter = new Date(req.data.startAfter);
7677
}
7778

78-
return { status: req.data.status ?? 3, startAfter: req.data.startAfter };
79+
return {
80+
status: req.data.status ?? 3,
81+
startAfter: req.data.startAfter,
82+
...(req.data.errorMessage && { error: new Error(req.data.errorMessage) }),
83+
};
7984
});
8085

8186
this.on("asArrayTuple", (req) => {
8287
cds.log(this.name).info(req.event, {
8388
data: req.data,
8489
user: req.user.id,
90+
error: req.data.error,
8591
});
8692

8793
return req.eventQueue.queueEntries.map(({ ID }) => [
8894
ID,
89-
{ startAfter: req.data.startAfter, status: 3 ?? req.data.status },
95+
{
96+
startAfter: req.data.startAfter,
97+
status: 3 ?? req.data.status,
98+
...(req.data.errorMessage && { error: new Error(req.data.errorMessage) }),
99+
},
90100
]);
91101
});
92102

93103
this.on("asArrayWithId", (req) => {
94104
cds.log(this.name).info(req.event, {
95105
data: req.data,
96106
user: req.user.id,
107+
error: req.data.error,
97108
});
98109

99110
return req.eventQueue.queueEntries.map(({ ID }) => ({
100111
ID,
101112
...((req.data.status || req.data.status === 0) && { status: req.data.status }),
102113
startAfter: req.data.startAfter,
114+
...(req.data.errorMessage && { error: new Error(req.data.errorMessage) }),
103115
}));
104116
});
105117
}

0 commit comments

Comments
 (0)