Skip to content

Commit

Permalink
client closes on cds shutdown (#38)
Browse files Browse the repository at this point in the history
* client closes on cds shutdown

* let closeClientBase throw
  • Loading branch information
rlindner81 authored Nov 27, 2023
1 parent 17f7097 commit fe56792
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
8 changes: 8 additions & 0 deletions cds-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const cds = require("@sap/cds");
const cdsPackage = require("@sap/cds/package.json");
const { initializeFeatures, getFeaturesKeys, getFeatureValue } = require("./src/singleton");
const { closeMainClient, closeSubscriberClient } = require("./src/redisWrapper");

const FEATURE_KEY_REGEX = /\/fts\/([^\s/]+)$/;

Expand Down Expand Up @@ -52,12 +53,19 @@ const _registerFeatureProvider = () => {
);
};

const _registerClientCloseOnShutdown = () => {
cds.on("shutdown", async () => {
await Promise.allSettled([closeMainClient(), closeSubscriberClient()]);
});
};

const activate = async () => {
const envFeatureToggles = cds.env.featureToggles;
if (!envFeatureToggles?.config && !envFeatureToggles?.configFile) {
return;
}
_overwriteServiceAccessRoles(envFeatureToggles);
_registerClientCloseOnShutdown();

// TODO for the "cds build" use case, this initialize makes no sense
await initializeFeatures({
Expand Down
40 changes: 28 additions & 12 deletions src/redisWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,20 +128,20 @@ const _createClientAndConnect = async (errorHandler) => {
return client;
};

const _closeClientBase = async (client) => {
if (client?.isOpen) {
await client.quit();
}
};

const _clientErrorHandlerBase = async (client, err, clientName) => {
_logErrorOnEvent(new VError({ name: VERROR_CLUSTER_NAME, cause: err, info: { clientName } }, "caught error event"));
if (client.isOpen) {
let quitError = null;
try {
await client.quit();
} catch (err) {
quitError = err;
}
if (quitError) {
_logErrorOnEvent(
new VError({ name: VERROR_CLUSTER_NAME, cause: quitError, info: { clientName } }, "error during client quit")
);
}
try {
await _closeClientBase(client);
} catch (closeError) {
_logErrorOnEvent(
new VError({ name: VERROR_CLUSTER_NAME, cause: closeError, info: { clientName } }, "error during client close")
);
}
};

Expand All @@ -165,6 +165,13 @@ const getMainClient = async () => {
return mainClient;
};

/**
* Closes the main Redis client if it is open.
*
* @private
*/
const closeMainClient = async () => await _closeClientBase(mainClient);

/**
* Lazily create a client to be used as a subscriber. Subscriber clients are in a special state and cannot be used for
* other commands.
Expand All @@ -184,6 +191,13 @@ const getSubscriberClient = async () => {
return subscriberClient;
};

/**
* Closes the subscriber Redis client if it is open.
*
* @private
*/
const closeSubscriberClient = async () => await _closeClientBase(subscriberClient);

const _clientExec = async (functionName, argsObject) => {
if (!mainClient) {
mainClient = await getMainClient();
Expand Down Expand Up @@ -513,7 +527,9 @@ const getIntegrationMode = async () => {
module.exports = {
REDIS_INTEGRATION_MODE: INTEGRATION_MODE,
getMainClient,
closeMainClient,
getSubscriberClient,
closeSubscriberClient,
getIntegrationMode,
sendCommand,
type,
Expand Down

0 comments on commit fe56792

Please sign in to comment.