@cap-js-community/event-queue 1.3.4 → 1.3.6
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 +6 -1
- package/src/redisPubSub.js +29 -13
- package/src/runner.js +14 -10
- package/src/shared/redis.js +23 -12
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cap-js-community/event-queue",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.6",
|
|
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": [
|
|
@@ -70,6 +70,11 @@
|
|
|
70
70
|
"eventQueue": {
|
|
71
71
|
"[production]": {
|
|
72
72
|
"disableRedis": false
|
|
73
|
+
},
|
|
74
|
+
"[test]": {
|
|
75
|
+
"registerAsEventProcessor": false,
|
|
76
|
+
"isRunnerDeactivated": true,
|
|
77
|
+
"updatePeriodicEvents": false
|
|
73
78
|
}
|
|
74
79
|
},
|
|
75
80
|
"requires": {
|
package/src/redisPubSub.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
const { promisify } = require("util");
|
|
4
|
+
|
|
3
5
|
const cds = require("@sap/cds");
|
|
4
6
|
|
|
5
7
|
const redis = require("./shared/redis");
|
|
@@ -10,7 +12,10 @@ const { getSubdomainForTenantId } = require("./shared/cdsHelper");
|
|
|
10
12
|
|
|
11
13
|
const EVENT_MESSAGE_CHANNEL = "EVENT_QUEUE_MESSAGE_CHANNEL";
|
|
12
14
|
const COMPONENT_NAME = "/eventQueue/redisPubSub";
|
|
15
|
+
const TRIES_FOR_PUBLISH_PERIODIC_EVENT = 10;
|
|
16
|
+
const SLEEP_TIME_FOR_PUBLISH_PERIODIC_EVENT = 30 * 1000;
|
|
13
17
|
|
|
18
|
+
const wait = promisify(setTimeout);
|
|
14
19
|
let subscriberClientPromise;
|
|
15
20
|
|
|
16
21
|
const initEventQueueRedisSubscribe = () => {
|
|
@@ -107,23 +112,34 @@ const broadcastEvent = async (tenantId, type, subType) => {
|
|
|
107
112
|
}
|
|
108
113
|
return;
|
|
109
114
|
}
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
115
|
+
const eventConfig = config.getEventConfig(type, subType);
|
|
116
|
+
for (let i = 0; i < TRIES_FOR_PUBLISH_PERIODIC_EVENT; i++) {
|
|
117
|
+
const result = await checkLockExistsAndReturnValue(
|
|
118
|
+
new cds.EventContext({ tenant: tenantId }),
|
|
119
|
+
[type, subType].join("##")
|
|
120
|
+
);
|
|
121
|
+
if (result) {
|
|
122
|
+
logger.debug("skip publish redis event as no lock is available", {
|
|
123
|
+
type,
|
|
124
|
+
subType,
|
|
125
|
+
index: i,
|
|
126
|
+
isPeriodic: eventConfig.isPeriodic,
|
|
127
|
+
waitInterval: SLEEP_TIME_FOR_PUBLISH_PERIODIC_EVENT,
|
|
128
|
+
});
|
|
129
|
+
if (!eventConfig.isPeriodic) {
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
await wait(SLEEP_TIME_FOR_PUBLISH_PERIODIC_EVENT);
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
logger.debug("publishing redis event", {
|
|
136
|
+
tenantId,
|
|
116
137
|
type,
|
|
117
138
|
subType,
|
|
118
139
|
});
|
|
119
|
-
|
|
140
|
+
await redis.publishMessage(EVENT_MESSAGE_CHANNEL, JSON.stringify({ tenantId, type, subType }));
|
|
141
|
+
break;
|
|
120
142
|
}
|
|
121
|
-
logger.debug("publishing redis event", {
|
|
122
|
-
tenantId,
|
|
123
|
-
type,
|
|
124
|
-
subType,
|
|
125
|
-
});
|
|
126
|
-
await redis.publishMessage(EVENT_MESSAGE_CHANNEL, JSON.stringify({ tenantId, type, subType }));
|
|
127
143
|
} catch (err) {
|
|
128
144
|
logger.error("publish event failed!", err, {
|
|
129
145
|
tenantId,
|
package/src/runner.js
CHANGED
|
@@ -68,19 +68,23 @@ const _scheduleFunction = async (singleRunFn, periodicFn) => {
|
|
|
68
68
|
|
|
69
69
|
const _multiTenancyRedis = async () => {
|
|
70
70
|
const logger = cds.log(COMPONENT_NAME);
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
71
|
+
try {
|
|
72
|
+
const emptyContext = new cds.EventContext({});
|
|
73
|
+
logger.info("executing event queue run for multi instance and tenant");
|
|
74
|
+
const tenantIds = await cdsHelper.getAllTenantIds();
|
|
75
|
+
await _checkPeriodicEventUpdate(tenantIds);
|
|
75
76
|
|
|
76
|
-
|
|
77
|
+
const runId = await _acquireRunId(emptyContext);
|
|
77
78
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
79
|
+
if (!runId) {
|
|
80
|
+
logger.error("could not acquire runId, skip processing events!");
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
82
83
|
|
|
83
|
-
|
|
84
|
+
return await _executeEventsAllTenants(tenantIds, runId);
|
|
85
|
+
} catch (err) {
|
|
86
|
+
logger.info("executing event queue run for multi instance and tenant failed", err);
|
|
87
|
+
}
|
|
84
88
|
};
|
|
85
89
|
|
|
86
90
|
const _checkPeriodicEventUpdate = async (tenantIds) => {
|
package/src/shared/redis.js
CHANGED
|
@@ -6,9 +6,11 @@ const { getEnvInstance } = require("./env");
|
|
|
6
6
|
const EventQueueError = require("../EventQueueError");
|
|
7
7
|
|
|
8
8
|
const COMPONENT_NAME = "/eventQueue/shared/redis";
|
|
9
|
+
const LOG_AFTER_SEC = 5;
|
|
9
10
|
|
|
10
11
|
let mainClientPromise;
|
|
11
12
|
const subscriberChannelClientPromise = {};
|
|
13
|
+
let lastErrorLog = Date.now();
|
|
12
14
|
|
|
13
15
|
const createMainClientAndConnect = () => {
|
|
14
16
|
if (mainClientPromise) {
|
|
@@ -18,8 +20,9 @@ const createMainClientAndConnect = () => {
|
|
|
18
20
|
const errorHandlerCreateClient = (err) => {
|
|
19
21
|
cds.log(COMPONENT_NAME).error("error from redis client for pub/sub failed", err);
|
|
20
22
|
mainClientPromise = null;
|
|
21
|
-
setTimeout(createMainClientAndConnect,
|
|
23
|
+
setTimeout(createMainClientAndConnect, LOG_AFTER_SEC * 1000).unref();
|
|
22
24
|
};
|
|
25
|
+
|
|
23
26
|
mainClientPromise = createClientAndConnect(errorHandlerCreateClient);
|
|
24
27
|
return mainClientPromise;
|
|
25
28
|
};
|
|
@@ -47,29 +50,37 @@ const _createClientBase = () => {
|
|
|
47
50
|
};
|
|
48
51
|
|
|
49
52
|
const createClientAndConnect = async (errorHandlerCreateClient) => {
|
|
50
|
-
let client = null;
|
|
51
|
-
try {
|
|
52
|
-
client = _createClientBase();
|
|
53
|
-
} catch (err) {
|
|
54
|
-
throw EventQueueError.redisConnectionFailure(err);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
client.on("error", errorHandlerCreateClient);
|
|
58
|
-
|
|
59
53
|
try {
|
|
54
|
+
const client = _createClientBase();
|
|
60
55
|
await client.connect();
|
|
56
|
+
client.on("error", (err) => {
|
|
57
|
+
const dateNow = Date.now();
|
|
58
|
+
if (dateNow - lastErrorLog > LOG_AFTER_SEC * 1000) {
|
|
59
|
+
cds.log(COMPONENT_NAME).error("error from redis client for pub/sub failed", err);
|
|
60
|
+
lastErrorLog = dateNow;
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
client.on("reconnecting", () => {
|
|
65
|
+
const dateNow = Date.now();
|
|
66
|
+
if (dateNow - lastErrorLog > LOG_AFTER_SEC * 1000) {
|
|
67
|
+
cds.log(COMPONENT_NAME).info("redis client trying reconnect...");
|
|
68
|
+
lastErrorLog = dateNow;
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
return client;
|
|
61
72
|
} catch (err) {
|
|
62
73
|
errorHandlerCreateClient(err);
|
|
63
74
|
}
|
|
64
|
-
return client;
|
|
65
75
|
};
|
|
66
76
|
|
|
67
77
|
const subscribeRedisChannel = (channel, subscribeHandler) => {
|
|
68
78
|
const errorHandlerCreateClient = (err) => {
|
|
69
79
|
cds.log(COMPONENT_NAME).error(`error from redis client for pub/sub failed for channel ${channel}`, err);
|
|
70
80
|
subscriberChannelClientPromise[channel] = null;
|
|
71
|
-
setTimeout(() => subscribeRedisChannel(channel, subscribeHandler),
|
|
81
|
+
setTimeout(() => subscribeRedisChannel(channel, subscribeHandler), LOG_AFTER_SEC * 1000).unref();
|
|
72
82
|
};
|
|
83
|
+
|
|
73
84
|
subscriberChannelClientPromise[channel] = createClientAndConnect(errorHandlerCreateClient)
|
|
74
85
|
.then((client) => {
|
|
75
86
|
cds.log(COMPONENT_NAME).info("subscribe redis client connected channel", { channel });
|