From b813a55e362c99b30d94b1d149a0ca1308b125f4 Mon Sep 17 00:00:00 2001 From: Bas950 Date: Sat, 21 Dec 2024 19:06:12 +0100 Subject: [PATCH] chore: remove updateHeartbeats --- apps/master/src/index.ts | 8 +-- apps/master/src/util/updateHeartbeats.ts | 67 ------------------------ 2 files changed, 1 insertion(+), 74 deletions(-) delete mode 100644 apps/master/src/util/updateHeartbeats.ts diff --git a/apps/master/src/index.ts b/apps/master/src/index.ts index 72479fc3..79abc810 100644 --- a/apps/master/src/index.ts +++ b/apps/master/src/index.ts @@ -10,7 +10,6 @@ import { Redis } from "ioredis"; import calculatePresenceUsage from "./util/calculatePresenceUsage.js"; import updateScience from "./util/updateScience.js"; -import updateHeartbeats from "./util/updateHeartbeats.js"; if (process.env.NODE_ENV !== "production") (await import("dotenv")).config({ path: "../../../.env" }); @@ -45,12 +44,7 @@ mainLog("Connecting to Redis..."); await redis.connect(); mainLog("Connected!"); -await Promise.all([ - updateScience(), - calculatePresenceUsage(), - updateHeartbeats() -]); +await Promise.all([updateScience(), calculatePresenceUsage()]); new CronJob("* * * * *", updateScience).start(); new CronJob("* * * * *", calculatePresenceUsage).start(); -new CronJob("*/1 * * * * *", updateHeartbeats).start(); diff --git a/apps/master/src/util/updateHeartbeats.ts b/apps/master/src/util/updateHeartbeats.ts deleted file mode 100644 index 53cc2eb9..00000000 --- a/apps/master/src/util/updateHeartbeats.ts +++ /dev/null @@ -1,67 +0,0 @@ -import pLimit from "p-limit"; -import { mainLog, mongo, redis } from "../index.js"; - -const BATCH_LIMIT = pLimit(Number.parseInt(process.env.BATCH_LIMIT || "5")); -const BATCH_SIZE = Number.parseInt(process.env.BATCH_SIZE || "1000"); - -const limit = pLimit(1); -export default async function () { - return limit(async () => { - const log = mainLog.extend("updateHeartbeats"); - let count = 0; - - const processBatch = async (cursor: string) => { - const result = await redis.hscan( - "pmd-api.heartbeatUpdates", - cursor, - "COUNT", - BATCH_SIZE - ); - const newCursor = result[0]; - - let batchToDelete = []; - let data = []; - for (let i = 0; i < result[1].length; i += 2) { - const identifier = result[1][i]; - data.push(JSON.parse(result[1][i + 1])); - - batchToDelete.push(identifier); - count++; - } - - if (batchToDelete.length) { - await redis.hdel("pmd-api.heartbeatUpdates", ...batchToDelete); - - const res = await mongo - .db("PreMiD") - .collection("heartbeats") - .bulkWrite( - data.map(d => ({ - updateOne: { - filter: { identifier: d.identifier }, - update: { - $set: { ...d, updated: new Date(d.updated) } - }, - upsert: true - } - })) - ); - log( - "Batch %s: Inserted %s entries, Updated %s entries", - Math.floor(count / BATCH_SIZE) + 1, - res.upsertedCount, - res.modifiedCount - ); - } - - return newCursor; - }; - - let cursor = "0"; - do { - cursor = await BATCH_LIMIT(() => processBatch(cursor)); - } while (cursor !== "0"); - - if (count > 0) log("Updated %s entries", count); - }); -}