Browse Source

feat: cleanup longJobs in Redis on startup

Kristian Vos 2 years ago
parent
commit
be451a54bc
1 changed files with 28 additions and 0 deletions
  1. 28 0
      backend/logic/cache/index.js

+ 28 - 0
backend/logic/cache/index.js

@@ -1,3 +1,4 @@
+import async from "async";
 import config from "config";
 import redis from "redis";
 import mongoose from "mongoose";
@@ -64,6 +65,15 @@ class _CacheModule extends CoreClass {
 				}
 			});
 
+			// TODO move to a better place
+			CacheModule.runJob("KEYS", { pattern: "longJobs.*" }).then(keys => {
+				async.eachLimit(keys, 1, (key, next) => {
+					CacheModule.runJob("DEL", { key }).finally(() => {
+						next();
+					});
+				});
+			});
+
 			this.client.on("error", err => {
 				if (this.getStatus() === "INITIALIZING") reject(err);
 				if (this.getStatus() === "LOCKDOWN") return;
@@ -409,6 +419,24 @@ class _CacheModule extends CoreClass {
 		});
 	}
 
+	/**
+	 * Gets a list of keys in Redis with a matching pattern
+	 *
+	 * @param {object} payload - object containing payload
+	 * @param {string} payload.pattern -  pattern to search for
+	 * @returns {Promise} - returns a promise (resolve, reject)
+	 */
+	KEYS(payload) {
+		return new Promise((resolve, reject) => {
+			const { pattern } = payload;
+
+			CacheModule.client.KEYS(pattern, (err, keys) => {
+				if (err) return reject(new Error(err));
+				return resolve(keys);
+			});
+		});
+	}
+
 	/**
 	 * Returns a redis schema
 	 *