@cap-js-community/event-queue
Advanced tools
Comparing version 1.3.4 to 1.3.5
{ | ||
"name": "@cap-js-community/event-queue", | ||
"version": "1.3.4", | ||
"version": "1.3.5", | ||
"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.", | ||
@@ -73,2 +73,7 @@ "main": "src/index.js", | ||
"disableRedis": false | ||
}, | ||
"[test]": { | ||
"registerAsEventProcessor": false, | ||
"isRunnerDeactivated": true, | ||
"updatePeriodicEvents": false | ||
} | ||
@@ -75,0 +80,0 @@ }, |
"use strict"; | ||
const { promisify } = require("util"); | ||
const cds = require("@sap/cds"); | ||
@@ -13,3 +15,6 @@ | ||
const COMPONENT_NAME = "/eventQueue/redisPubSub"; | ||
const TRIES_FOR_PUBLISH_PERIODIC_EVENT = 10; | ||
const SLEEP_TIME_FOR_PUBLISH_PERIODIC_EVENT = 30 * 1000; | ||
const wait = promisify(setTimeout); | ||
let subscriberClientPromise; | ||
@@ -111,19 +116,30 @@ | ||
} | ||
const result = await checkLockExistsAndReturnValue( | ||
new cds.EventContext({ tenant: tenantId }), | ||
[type, subType].join("##") | ||
); | ||
if (result) { | ||
logger.debug("skip publish redis event as no lock is available", { | ||
const eventConfig = config.getEventConfig(type, subType); | ||
for (let i = 0; i < TRIES_FOR_PUBLISH_PERIODIC_EVENT; i++) { | ||
const result = await checkLockExistsAndReturnValue( | ||
new cds.EventContext({ tenant: tenantId }), | ||
[type, subType].join("##") | ||
); | ||
if (result) { | ||
logger.debug("skip publish redis event as no lock is available", { | ||
type, | ||
subType, | ||
index: i, | ||
isPeriodic: eventConfig.isPeriodic, | ||
waitInterval: SLEEP_TIME_FOR_PUBLISH_PERIODIC_EVENT, | ||
}); | ||
if (!eventConfig.isPeriodic) { | ||
break; | ||
} | ||
await wait(SLEEP_TIME_FOR_PUBLISH_PERIODIC_EVENT); | ||
continue; | ||
} | ||
logger.debug("publishing redis event", { | ||
tenantId, | ||
type, | ||
subType, | ||
}); | ||
return; | ||
await redis.publishMessage(EVENT_MESSAGE_CHANNEL, JSON.stringify({ tenantId, type, subType })); | ||
break; | ||
} | ||
logger.debug("publishing redis event", { | ||
tenantId, | ||
type, | ||
subType, | ||
}); | ||
await redis.publishMessage(EVENT_MESSAGE_CHANNEL, JSON.stringify({ tenantId, type, subType })); | ||
} catch (err) { | ||
@@ -130,0 +146,0 @@ logger.error("publish event failed!", err, { |
162526
4000