@cap-js-community/event-queue 1.5.2 → 1.5.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/cds-plugin.js +1 -1
- package/package.json +1 -1
- package/src/initialize.js +20 -16
- package/src/redis/redisPub.js +4 -4
- package/src/runner/runner.js +42 -27
- package/src/shared/WorkerQueue.js +1 -1
package/cds-plugin.js
CHANGED
|
@@ -5,6 +5,6 @@ const cds = require("@sap/cds");
|
|
|
5
5
|
const eventQueue = require("./src");
|
|
6
6
|
const COMPONENT_NAME = "/eventQueue/plugin";
|
|
7
7
|
|
|
8
|
-
if (!cds.build?.register && cds.env.eventQueue) {
|
|
8
|
+
if (!cds.build?.register && Object.keys(cds.env.eventQueue ?? {}).length) {
|
|
9
9
|
module.exports = eventQueue.initialize().catch((err) => cds.log(COMPONENT_NAME).error(err));
|
|
10
10
|
}
|
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.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
|
"types": "src/index.d.ts",
|
package/src/initialize.js
CHANGED
|
@@ -131,22 +131,7 @@ const readConfigFromFile = async (configFilepath) => {
|
|
|
131
131
|
};
|
|
132
132
|
|
|
133
133
|
const registerEventProcessors = () => {
|
|
134
|
-
|
|
135
|
-
cds.connect
|
|
136
|
-
.to("cds.xt.DeploymentService")
|
|
137
|
-
.then((ds) => {
|
|
138
|
-
cds.log(COMPONENT).info("event-queue unsubscribe handler registered", {
|
|
139
|
-
redisEnabled: config.redisEnabled,
|
|
140
|
-
});
|
|
141
|
-
ds.after("unsubscribe", async (_, req) => {
|
|
142
|
-
const { tenant } = req.data;
|
|
143
|
-
config.handleUnsubscribe(tenant);
|
|
144
|
-
});
|
|
145
|
-
})
|
|
146
|
-
.catch(
|
|
147
|
-
() => {} // ignore errors as the DeploymentService is most of the time only available in the mtx sidecar
|
|
148
|
-
);
|
|
149
|
-
});
|
|
134
|
+
_registerUnsubscribe();
|
|
150
135
|
config.redisEnabled && config.attachRedisUnsubscribeHandler();
|
|
151
136
|
|
|
152
137
|
if (!config.registerAsEventProcessor) {
|
|
@@ -215,6 +200,25 @@ const registerCleanupForDevDb = async () => {
|
|
|
215
200
|
}
|
|
216
201
|
};
|
|
217
202
|
|
|
203
|
+
const _registerUnsubscribe = () => {
|
|
204
|
+
cds.on("listening", () => {
|
|
205
|
+
cds.connect
|
|
206
|
+
.to("cds.xt.DeploymentService")
|
|
207
|
+
.then((ds) => {
|
|
208
|
+
cds.log(COMPONENT).info("event-queue unsubscribe handler registered", {
|
|
209
|
+
redisEnabled: config.redisEnabled,
|
|
210
|
+
});
|
|
211
|
+
ds.after("unsubscribe", async (_, req) => {
|
|
212
|
+
const { tenant } = req.data;
|
|
213
|
+
config.handleUnsubscribe(tenant);
|
|
214
|
+
});
|
|
215
|
+
})
|
|
216
|
+
.catch(
|
|
217
|
+
() => {} // ignore errors as the DeploymentService is most of the time only available in the mtx sidecar
|
|
218
|
+
);
|
|
219
|
+
});
|
|
220
|
+
};
|
|
221
|
+
|
|
218
222
|
module.exports = {
|
|
219
223
|
initialize,
|
|
220
224
|
};
|
package/src/redis/redisPub.js
CHANGED
|
@@ -87,11 +87,11 @@ const _processLocalWithoutRedis = async (tenantId, events) => {
|
|
|
87
87
|
};
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
-
|
|
91
|
-
|
|
90
|
+
for (const { type, subType } of events) {
|
|
91
|
+
await cds.tx(context, async ({ context }) => {
|
|
92
92
|
await runEventCombinationForTenant(context, type, subType, { shouldTrace: true });
|
|
93
|
-
}
|
|
94
|
-
}
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
95
|
}
|
|
96
96
|
};
|
|
97
97
|
|
package/src/runner/runner.js
CHANGED
|
@@ -22,7 +22,7 @@ const EVENT_QUEUE_RUN_ID = "EVENT_QUEUE_RUN_ID";
|
|
|
22
22
|
const EVENT_QUEUE_RUN_TS = "EVENT_QUEUE_RUN_TS";
|
|
23
23
|
const EVENT_QUEUE_RUN_REDIS_CHECK = "EVENT_QUEUE_RUN_REDIS_CHECK";
|
|
24
24
|
const EVENT_QUEUE_UPDATE_PERIODIC_EVENTS = "EVENT_QUEUE_UPDATE_PERIODIC_EVENTS";
|
|
25
|
-
|
|
25
|
+
let OFFSET_FIRST_RUN = 10 * 1000;
|
|
26
26
|
|
|
27
27
|
let tenantIdHash;
|
|
28
28
|
let singleRunDone;
|
|
@@ -160,7 +160,6 @@ const _executeEventsAllTenantsRedis = async (tenantIds) => {
|
|
|
160
160
|
|
|
161
161
|
const _executeEventsAllTenants = async (tenantIds, runId) => {
|
|
162
162
|
const promises = [];
|
|
163
|
-
|
|
164
163
|
for (const tenantId of tenantIds) {
|
|
165
164
|
const id = cds.utils.uuid();
|
|
166
165
|
let tenantContext;
|
|
@@ -254,32 +253,46 @@ const _executePeriodicEventsAllTenants = async (tenantIds) => {
|
|
|
254
253
|
}
|
|
255
254
|
};
|
|
256
255
|
|
|
257
|
-
const _singleTenantDb = async (
|
|
258
|
-
|
|
259
|
-
|
|
256
|
+
const _singleTenantDb = async () => {
|
|
257
|
+
const id = cds.utils.uuid();
|
|
258
|
+
const events = await trace(
|
|
259
|
+
{ id },
|
|
260
|
+
"fetch-openEvents-and-authInfo",
|
|
261
|
+
async () => {
|
|
262
|
+
return await cds.tx({}, async (tx) => {
|
|
263
|
+
return await openEvents.getOpenQueueEntries(tx);
|
|
264
|
+
});
|
|
265
|
+
},
|
|
266
|
+
{ newRootSpan: true }
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
return await Promise.allSettled(
|
|
270
|
+
events.map(async (openEvent) => {
|
|
271
|
+
const eventConfig = config.getEventConfig(openEvent.type, openEvent.subType);
|
|
260
272
|
const label = `${eventConfig.type}_${eventConfig.subType}`;
|
|
261
|
-
const user = new cds.User.Privileged(config.userId);
|
|
262
|
-
const tenantContext = {
|
|
263
|
-
tenant: tenantId,
|
|
264
|
-
user,
|
|
265
|
-
};
|
|
266
273
|
return await WorkerQueue.instance.addToQueue(eventConfig.load, label, eventConfig.priority, async () => {
|
|
267
|
-
return await cds.tx(
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
274
|
+
return await cds.tx({}, async ({ context }) => {
|
|
275
|
+
await trace(
|
|
276
|
+
context,
|
|
277
|
+
label,
|
|
278
|
+
async () => {
|
|
279
|
+
try {
|
|
280
|
+
const lockId = `${label}`;
|
|
281
|
+
const couldAcquireLock = await distributedLock.acquireLock(context, lockId, {
|
|
282
|
+
expiryTime: eventQueueConfig.runInterval * 0.95,
|
|
283
|
+
});
|
|
284
|
+
if (!couldAcquireLock) {
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
await runEventCombinationForTenant(context, eventConfig.type, eventConfig.subType, {
|
|
288
|
+
skipWorkerPool: true,
|
|
289
|
+
});
|
|
290
|
+
} catch (err) {
|
|
291
|
+
cds.log(COMPONENT_NAME).error("executing event-queue run for tenant failed");
|
|
292
|
+
}
|
|
293
|
+
},
|
|
294
|
+
{ newRootSpan: true }
|
|
295
|
+
);
|
|
283
296
|
});
|
|
284
297
|
});
|
|
285
298
|
})
|
|
@@ -387,7 +400,8 @@ const _multiTenancyPeriodicEvents = async (tenantIds) => {
|
|
|
387
400
|
}
|
|
388
401
|
};
|
|
389
402
|
|
|
390
|
-
const _checkPeriodicEventsSingleTenantOneTime = () =>
|
|
403
|
+
const _checkPeriodicEventsSingleTenantOneTime = async () =>
|
|
404
|
+
eventQueueConfig.updatePeriodicEvents &&
|
|
391
405
|
cds.tx({}, async (tx) => await periodicEvents.checkAndInsertPeriodicEvents(tx.context));
|
|
392
406
|
|
|
393
407
|
const _checkPeriodicEventsSingleTenant = async (context) => {
|
|
@@ -425,5 +439,6 @@ module.exports = {
|
|
|
425
439
|
_acquireRunId,
|
|
426
440
|
EVENT_QUEUE_RUN_TS,
|
|
427
441
|
clearHash: () => (tenantIdHash = null),
|
|
442
|
+
setOffsetFirstRun: (value) => (OFFSET_FIRST_RUN = value),
|
|
428
443
|
},
|
|
429
444
|
};
|
|
@@ -113,7 +113,7 @@ class WorkerQueue {
|
|
|
113
113
|
}
|
|
114
114
|
|
|
115
115
|
#checkForNext() {
|
|
116
|
-
if (!this.#queue.length
|
|
116
|
+
if (!Object.values(this.#queue).some((queue) => queue.length) || this.#runningLoad === this.#concurrencyLimit) {
|
|
117
117
|
return;
|
|
118
118
|
}
|
|
119
119
|
|