If redis fails to connect use a mock client to not crash
This commit is contained in:
@@ -2,6 +2,10 @@
|
|||||||
"database": {
|
"database": {
|
||||||
"host": "../shows.db"
|
"host": "../shows.db"
|
||||||
},
|
},
|
||||||
|
"redis": {
|
||||||
|
"host": "localhost",
|
||||||
|
"port": 6379
|
||||||
|
},
|
||||||
"webserver": {
|
"webserver": {
|
||||||
"port": 31459,
|
"port": 31459,
|
||||||
"origins": []
|
"origins": []
|
||||||
|
|||||||
75
seasoned_api/src/cache/redis.js
vendored
75
seasoned_api/src/cache/redis.js
vendored
@@ -1,39 +1,46 @@
|
|||||||
const redis = require("redis")
|
const { promisify } = require("util");
|
||||||
const client = redis.createClient()
|
const configuration = require("../config/configuration").getInstance();
|
||||||
|
|
||||||
class Cache {
|
let client;
|
||||||
/**
|
|
||||||
* Retrieve an unexpired cache entry by key.
|
|
||||||
* @param {String} key of the cache entry
|
|
||||||
* @returns {Promise}
|
|
||||||
*/
|
|
||||||
get(key) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
client.get(key, (error, reply) => {
|
|
||||||
if (reply == null) {
|
|
||||||
return reject();
|
|
||||||
}
|
|
||||||
|
|
||||||
resolve(JSON.parse(reply));
|
try {
|
||||||
|
const redis = require("redis");
|
||||||
|
console.log("Trying to connect with redis..");
|
||||||
|
const host = configuration.get("redis", "host");
|
||||||
|
const port = configuration.get("redis", "port");
|
||||||
|
|
||||||
|
console.log(`redis://${host}:${port}`);
|
||||||
|
client = redis.createClient({
|
||||||
|
url: `redis://${host}:${port}`
|
||||||
});
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
client.on("connect", () => console.log("Redis connection established!"));
|
||||||
* Insert cache entry with key and value.
|
|
||||||
* @param {String} key of the cache entry
|
client.on("error", function (err) {
|
||||||
* @param {String} value of the cache entry
|
client.quit();
|
||||||
* @param {Number} timeToLive the number of seconds before entry expires
|
console.error("Unable to connect to redis, setting up redis-mock.");
|
||||||
* @returns {Object}
|
|
||||||
*/
|
client = {
|
||||||
set(key, value, timeToLive = 10800) {
|
get: function () {
|
||||||
|
console.log("redis-dummy get", arguments[0]);
|
||||||
|
return Promise.resolve();
|
||||||
|
},
|
||||||
|
set: function () {
|
||||||
|
console.log("redis-dummy set", arguments[0]);
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
|
} catch (e) {}
|
||||||
|
|
||||||
|
function set(key, value, TTL = 10800) {
|
||||||
if (value == null || key == null) return null;
|
if (value == null || key == null) return null;
|
||||||
|
|
||||||
const json = JSON.stringify(value);
|
const json = JSON.stringify(value);
|
||||||
client.set(key, json, (error, reply) => {
|
client.set(key, json, (error, reply) => {
|
||||||
if (reply == "OK") {
|
if (reply == "OK") {
|
||||||
// successfully set value with key, now set TTL for key
|
// successfully set value with key, now set TTL for key
|
||||||
client.expire(key, timeToLive, e => {
|
client.expire(key, TTL, e => {
|
||||||
if (e)
|
if (e)
|
||||||
console.error(
|
console.error(
|
||||||
"Unexpected error while setting expiration for key:",
|
"Unexpected error while setting expiration for key:",
|
||||||
@@ -47,6 +54,20 @@ class Cache {
|
|||||||
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function get() {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
client.get(key, (error, reply) => {
|
||||||
|
if (reply == null) {
|
||||||
|
return reject();
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Cache;
|
resolve(JSON.parse(reply));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
get,
|
||||||
|
set
|
||||||
|
};
|
||||||
|
|||||||
@@ -5,8 +5,7 @@ const PythonShell = require("python-shell");
|
|||||||
|
|
||||||
const establishedDatabase = require("../database/database");
|
const establishedDatabase = require("../database/database");
|
||||||
|
|
||||||
const RedisCache = require("../cache/redis");
|
const cache = require("../cache/redis");
|
||||||
const cache = new RedisCache();
|
|
||||||
|
|
||||||
function getMagnetFromURL(url) {
|
function getMagnetFromURL(url) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
|||||||
@@ -5,8 +5,7 @@ const convertPlexToEpisode = require("./convertPlexToEpisode");
|
|||||||
|
|
||||||
const { Movie, Show, Person } = require("../tmdb/types");
|
const { Movie, Show, Person } = require("../tmdb/types");
|
||||||
|
|
||||||
const RedisCache = require("../cache/redis");
|
const redisCache = require("../cache/redis");
|
||||||
const redisCache = new RedisCache();
|
|
||||||
|
|
||||||
const sanitize = string => string.toLowerCase().replace(/[^\w]/gi, "");
|
const sanitize = string => string.toLowerCase().replace(/[^\w]/gi, "");
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
const moviedb = require("km-moviedb");
|
const moviedb = require("km-moviedb");
|
||||||
const RedisCache = require("../cache/redis");
|
const redisCache = require("../cache/redis");
|
||||||
const redisCache = new RedisCache();
|
|
||||||
|
|
||||||
const { Movie, Show, Person, Credits, ReleaseDates } = require("./types");
|
const { Movie, Show, Person, Credits, ReleaseDates } = require("./types");
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user