diff --git a/packages/auth/src/redis/index.js b/packages/auth/src/redis/index.js index 044a8bcaf9..7d9e9ad637 100644 --- a/packages/auth/src/redis/index.js +++ b/packages/auth/src/redis/index.js @@ -8,8 +8,24 @@ const STARTUP_TIMEOUT_MS = 5000 const CLUSTERED = false // for testing just generate the client once -let CONNECTED = false +let CLOSED = false let CLIENT = env.isTest() ? new Redis(getRedisOptions()) : null +// if in test always connected +let CONNECTED = !!env.isTest() + +function connectionError(timeout, err) { + // manually shut down, ignore errors + if (CLOSED) { + return + } + // always clear this on error + clearTimeout(timeout) + CONNECTED = false + console.error("Redis connection failed - " + err) + setTimeout(() => { + init() + }, RETRY_PERIOD_MS) +} /** * Inits the system, will error if unable to connect to redis cluster (may take up to 10 seconds) otherwise @@ -17,15 +33,7 @@ let CLIENT = env.isTest() ? new Redis(getRedisOptions()) : null */ function init() { let timeout - function errorOccurred(err) { - // always clear this on error - clearTimeout(timeout) - CONNECTED = false - console.error("Redis connection failed - " + err) - setTimeout(() => { - init() - }, RETRY_PERIOD_MS) - } + CLOSED = false // testing uses a single in memory client if (env.isTest() || (CLIENT && CONNECTED)) { return @@ -33,7 +41,7 @@ function init() { // start the timer - only allowed 5 seconds to connect timeout = setTimeout(() => { if (!CONNECTED) { - errorOccurred() + connectionError(timeout) } }, STARTUP_TIMEOUT_MS) @@ -49,10 +57,10 @@ function init() { } // attach handlers CLIENT.on("end", err => { - errorOccurred(err) + connectionError(timeout, err) }) CLIENT.on("error", err => { - errorOccurred(err) + connectionError(timeout, err) }) CLIENT.on("connect", () => { clearTimeout(timeout) @@ -122,12 +130,14 @@ class RedisWrapper { } async init() { + CLOSED = false init() await waitForConnection() return this } async finish() { + CLOSED = true CLIENT.disconnect() } diff --git a/packages/server/src/app.js b/packages/server/src/app.js index 9ec6c2c687..50df056b2a 100644 --- a/packages/server/src/app.js +++ b/packages/server/src/app.js @@ -73,10 +73,11 @@ if (env.isProd()) { const server = http.createServer(app.callback()) destroyable(server) -server.on("close", () => { +server.on("close", async () => { if (env.NODE_ENV !== "jest") { console.log("Server Closed") } + await redis.shutdown() }) module.exports = server.listen(env.PORT || 0, async () => { diff --git a/packages/server/src/utilities/redis.js b/packages/server/src/utilities/redis.js index 8e0f774f42..ae18b82e02 100644 --- a/packages/server/src/utilities/redis.js +++ b/packages/server/src/utilities/redis.js @@ -11,6 +11,11 @@ exports.init = async () => { debounceClient = await new Client(utils.Databases.DEBOUNCE).init() } +exports.shutdown = async () => { + await devAppClient.finish() + await debounceClient.finish() +} + exports.doesUserHaveLock = async (devAppId, user) => { const value = await devAppClient.get(devAppId) if (!value) {