@cap-js-community/event-queue 0.2.0 → 0.2.1

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/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  [![npm version](https://img.shields.io/npm/v/@cap-js-community/event-queue)](https://www.npmjs.com/package/@cap-js-community/event-queue)
4
4
  [![monthly downloads](https://img.shields.io/npm/dm/@cap-js-community/event-queue)](https://www.npmjs.com/package/@cap-js-community/event-queue)
5
5
  [![REUSE status](https://api.reuse.software/badge/github.com/cap-js-community/event-queue)](https://api.reuse.software/info/github.com/cap-js-community/event-queue)
6
- [![CI Main](https://github.com/cap-js-community/event-queue/actions/workflows/ci-main.yml/badge.svg)](https://github.com/cap-js-community/event-queue/commits/main)
6
+ [![CI Main](https://github.com/cap-js-community/event-queue/actions/workflows/main-ci.yml/badge.svg)](https://github.com/cap-js-community/event-queue/commits/main)
7
7
 
8
8
  The Event-Queue is a framework built on top of CAP Node.js, specifically designed to enable efficient and streamlined
9
9
  asynchronous event processing in a multi-tenancy environment. With a strong emphasis on load balancing, this package
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cap-js-community/event-queue",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "An event queue that enables secure transactional processing of asynchronous 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/initialize.js CHANGED
@@ -12,7 +12,8 @@ const EventQueueError = require("./EventQueueError");
12
12
  const runner = require("./runner");
13
13
  const dbHandler = require("./dbHandler");
14
14
  const { getConfigInstance } = require("./config");
15
- const { initEventQueueRedisSubscribe } = require("./redisPubSub");
15
+ const { initEventQueueRedisSubscribe, closeSubscribeClient } = require("./redisPubSub");
16
+ const { closeMainClient } = require("./shared/redis");
16
17
 
17
18
  const readFileAsync = promisify(fs.readFile);
18
19
 
@@ -82,6 +83,7 @@ const initialize = async ({
82
83
  }
83
84
 
84
85
  registerEventProcessors();
86
+ registerCdsShutdown();
85
87
  logger.info("event queue initialized", {
86
88
  registerAsEventProcessor: configInstance.registerAsEventProcessor,
87
89
  multiTenancyEnabled: configInstance.isMultiTenancy,
@@ -181,6 +183,12 @@ const mixConfigVarsWithEnv = (...args) => {
181
183
  });
182
184
  };
183
185
 
186
+ const registerCdsShutdown = () => {
187
+ cds.on("shutdown", async () => {
188
+ await Promise.allSettled([closeMainClient(), closeSubscribeClient()]);
189
+ });
190
+ };
191
+
184
192
  module.exports = {
185
193
  initialize,
186
194
  };
@@ -67,7 +67,19 @@ const broadcastEvent = async (tenantId, type, subType) => {
67
67
  }
68
68
  };
69
69
 
70
+ const closeSubscribeClient = async () => {
71
+ try {
72
+ const client = await subscriberClientPromise;
73
+ if (client?.quit) {
74
+ await client.quit();
75
+ }
76
+ } catch (err) {
77
+ // ignore errors during shutdown
78
+ }
79
+ };
80
+
70
81
  module.exports = {
71
82
  initEventQueueRedisSubscribe,
72
83
  broadcastEvent,
84
+ closeSubscribeClient,
73
85
  };
@@ -29,9 +29,18 @@ const _createClientBase = () => {
29
29
  if (env.isOnCF) {
30
30
  try {
31
31
  const credentials = env.getRedisCredentialsFromEnv();
32
- // NOTE: settings the user explicitly to empty resolves auth problems, see
33
- // https://github.com/go-redis/redis/issues/1343
32
+ const redisIsCluster = credentials.cluster_mode;
34
33
  const url = credentials.uri.replace(/(?<=rediss:\/\/)[\w-]+?(?=:)/, "");
34
+ if (redisIsCluster) {
35
+ return redis.createCluster({
36
+ rootNodes: [{ url }],
37
+ // https://github.com/redis/node-redis/issues/1782
38
+ defaults: {
39
+ password: credentials.password,
40
+ socket: { tls: credentials.tls },
41
+ },
42
+ });
43
+ }
35
44
  return redis.createClient({ url });
36
45
  } catch (err) {
37
46
  throw EventQueueError.redisConnectionFailure(err);
@@ -87,9 +96,21 @@ const publishMessage = async (channel, message) => {
87
96
 
88
97
  const _localReconnectStrategy = () => EventQueueError.redisNoReconnect();
89
98
 
99
+ const closeMainClient = async () => {
100
+ try {
101
+ const client = await mainClientPromise;
102
+ if (client?.quit) {
103
+ await client.quit();
104
+ }
105
+ } catch (err) {
106
+ // ignore errors during shutdown
107
+ }
108
+ };
109
+
90
110
  module.exports = {
91
111
  createClientAndConnect,
92
112
  createMainClientAndConnect,
93
113
  subscribeRedisChannel,
94
114
  publishMessage,
115
+ closeMainClient,
95
116
  };