diff --git a/src/cache/redis.js b/src/cache/redis.js index 29078db..f809d6b 100644 --- a/src/cache/redis.js +++ b/src/cache/redis.js @@ -1,9 +1,9 @@ import redis from "redis"; import Configuration from "../config/configuration.js"; +import redisMockClient from "./redisMock.js"; const configuration = Configuration.getInstance(); let client; -const mockCache = {}; try { console.log("Trying to connect with redis.."); // eslint-disable-line no-console @@ -20,21 +20,7 @@ try { client.quit(); console.error("Unable to connect to redis, setting up redis-mock."); // eslint-disable-line no-console - client = { - get(key, callback) { - console.log(`redis-dummy get: ${key}`); // eslint-disable-line no-console - const hit = mockCache[key]; - return Promise.resolve().then(callback(null, JSON.parse(hit))); - }, - set(key, json, callback) { - console.log(`redis-dummy set: ${key}`); // eslint-disable-line no-console - mockCache[key] = JSON.stringify(json); - return Promise.resolve().then(callback(null, "OK")); - }, - expire(key, TTL) { - console.log(`redis-dummy expire: ${key} with TTL ${TTL}`); // eslint-disable-line no-console - } - }; + client = redisMockClient; }); } catch (e) {} diff --git a/src/cache/redisMock.js b/src/cache/redisMock.js new file mode 100644 index 0000000..f7ca3df --- /dev/null +++ b/src/cache/redisMock.js @@ -0,0 +1,19 @@ +const mockCache = {}; + +const redisMockClient = { + get(key, callback) { + // console.log(`redis-dummy get: ${key}`); // eslint-disable-line no-console + const hit = mockCache[key] || null; + return Promise.resolve(callback(null, hit)); + }, + set(key, json, callback) { + // console.log(`redis-dummy set: ${key}`); // eslint-disable-line no-console + mockCache[key] = JSON.stringify(json); + return Promise.resolve(callback(null, "OK")); + }, + expire(key, TTL) { + console.log(`redis-dummy expire: ${key} with TTL ${TTL}`); // eslint-disable-line no-console + } +}; + +export default redisMockClient; diff --git a/src/plex/plex.js b/src/plex/plex.js index 5b1f841..01906c8 100644 --- a/src/plex/plex.js +++ b/src/plex/plex.js @@ -117,7 +117,7 @@ class Plex { headers: { Accept: "application/json" } }; - return new Promise((resolve, reject) => + return new Promise((resolve, reject) => { this.cache .get(cacheKey) .then(machineInfo => resolve(machineInfo?.machineIdentifier)) @@ -133,8 +133,8 @@ class Plex { } reject(new PlexUnexpectedError()); - }) - ); + }); + }); } async existsInPlex(tmdb) { @@ -186,18 +186,24 @@ class Plex { const url = `http://${this.plexIP}:${ this.plexPort - }/hubs/search?query=${fixedEncodeURIComponent(query)}`; + }/hubs/search?query=${fixedEncodeURIComponent(query)}&X-Plex-Token=${ + this.token + }`; + const options = { timeout: 20000, headers: { Accept: "application/json" } }; - return new Promise((resolve, reject) => + return new Promise((resolve, reject) => { this.cache .get(cacheKey) - .catch(() => fetch(url, options)) // else fetch fresh data - .then(successfullResponse) - .then(results => this.cache.set(cacheKey, results, 21600)) // 6 hours + .catch(() => { + // else fetch fresh data + return fetch(url, options) + .then(successfullResponse) + .then(results => this.cache.set(cacheKey, results, 21600)); // 6 hours + }) .then(mapResults) .then(resolve) .catch(error => { @@ -206,8 +212,8 @@ class Plex { } reject(new PlexUnexpectedError()); - }) - ); + }); + }); } // this is not guarenteed to work, but if we see a movie or @@ -222,7 +228,7 @@ class Plex { // TODO improve cache key matching by lowercasing it on the backend. // what do we actually need to check for if the key was deleted or not // it might be an error or another response code. - console.log("Unable to delete, key might not exists"); + console.log("Unable to delete, key might not exists"); // eslint-disable-line no-console return response === 1; }); }