From 0fa8288a0d1a7515b9863a6d7df03d5530466a24 Mon Sep 17 00:00:00 2001 From: Anand Chowdhary Date: Mon, 30 Sep 2019 10:01:09 -0700 Subject: [PATCH] :sparkles: Handle Redis errors --- package.json | 5 +++-- src/helpers/errors.ts | 11 +++++++++++ src/helpers/redis.ts | 20 +++++++++++++++++++- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 3d3aab201..9ea889662 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "staart-manager", - "version": "1.1.29", + "version": "1.1.30", "main": "index.js", "repository": "git@github.com:AnandChowdhary/staart.git", "author": "Anand Chowdhary ", @@ -77,6 +77,7 @@ "@types/stripe": "^6.32.5", "@types/validator": "^10.11.3", "@types/yaml": "^1.0.2", + "chalk": "^2.4.2", "concurrently": "^4.1.2", "coveralls": "^3.0.6", "dotenv": "^8.1.0", @@ -144,5 +145,5 @@ "setup" ], "snyk": true, - "staart-version": "1.1.29" + "staart-version": "1.1.30" } \ No newline at end of file diff --git a/src/helpers/errors.ts b/src/helpers/errors.ts index 355099b5a..03b9d2fc6 100644 --- a/src/helpers/errors.ts +++ b/src/helpers/errors.ts @@ -1,5 +1,6 @@ import { ErrorCode } from "../interfaces/enum"; import { HTTPError } from "../interfaces/general"; +import chalk from "chalk"; import Joi from "@hapi/joi"; /** @@ -38,3 +39,13 @@ export const sendError = (error: string) => { console.log("Backup error", error); return { status: 500, code: error } as HTTPError; }; + +export const logError = (category: string, error: string, level: 1 | 2 = 2) => { + if (level === 1) + return console.log( + `${chalk.bold.red("❌ ERROR")} ${chalk.red(category)}: ${error}` + ); + console.log( + `${chalk.bold.yellow("⚠️ WARNING")} ${chalk.yellow(category)}: ${error}` + ); +}; diff --git a/src/helpers/redis.ts b/src/helpers/redis.ts index 160d63949..33ff19fe2 100644 --- a/src/helpers/redis.ts +++ b/src/helpers/redis.ts @@ -1,6 +1,24 @@ import { createHandyClient } from "handy-redis"; import { REDIS_URL } from "../config"; +import { logError } from "./errors"; export const redis = createHandyClient({ - url: REDIS_URL + url: REDIS_URL, + retry_strategy: options => { + if (options.error && options.error.code === "ECONNREFUSED") { + logError("Redis connection failed", "Server refused the connection"); + } + + if (options.total_retry_time > 1000 * 60 * 60) { + logError("Redis connection failed", "Total retry time exhausted"); + } + + if (options.attempt > 10) { + logError("Redis connection failed", "Max number of attempts exceeded"); + return 43200; + } + + // Reconnect after this time + return Math.min(options.attempt * 100, 3000); + } });