@cap-js-community/event-queue 1.3.5 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cap-js-community/event-queue",
3
- "version": "1.3.5",
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": [
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
- const emptyContext = new cds.EventContext({});
72
- logger.info("executing event queue run for multi instance and tenant");
73
- const tenantIds = await cdsHelper.getAllTenantIds();
74
- await _checkPeriodicEventUpdate(tenantIds);
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
- const runId = await _acquireRunId(emptyContext);
77
+ const runId = await _acquireRunId(emptyContext);
77
78
 
78
- if (!runId) {
79
- logger.error("could not acquire runId, skip processing events!");
80
- return;
81
- }
79
+ if (!runId) {
80
+ logger.error("could not acquire runId, skip processing events!");
81
+ return;
82
+ }
82
83
 
83
- return await _executeEventsAllTenants(tenantIds, runId);
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) => {
@@ -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, 5 * 1000).unref();
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), 5 * 1000).unref();
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 });