Skip to content

Commit

Permalink
feat: heartbeat
Browse files Browse the repository at this point in the history
  • Loading branch information
Timeraa committed Aug 7, 2024
1 parent 699c406 commit 20f1caf
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 7 deletions.
8 changes: 7 additions & 1 deletion apps/master/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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" });
Expand Down Expand Up @@ -44,7 +45,12 @@ mainLog("Connecting to Redis...");
await redis.connect();
mainLog("Connected!");

await Promise.all([updateScience(), calculatePresenceUsage()]);
await Promise.all([
updateScience(),
calculatePresenceUsage(),
updateHeartbeats()
]);

new CronJob("* * * * *", updateScience).start();
new CronJob("* * * * *", calculatePresenceUsage).start();
new CronJob("* * * * *", updateHeartbeats).start();
57 changes: 57 additions & 0 deletions apps/master/src/util/updateHeartbeats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import pLimit from "p-limit";
import { mainLog, mongo, redis } from "../index.js";

const limit = pLimit(1);
export default async function () {
limit(async () => {
let count = 0;
const log = mainLog.extend("updateHeartbeats");

let cursor = "";
do {
const result = await redis.hscan(
"pmd-api.heartbeatUpdates",
cursor,
"COUNT",
1000
);
cursor = 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) continue;
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() }
},
upsert: true
}
}))
);
log(
"Batch %s: Inserted %s entries, Updated %s entries",
Math.floor(count / 1000) + 1,
res.upsertedCount,
res.modifiedCount
);
} while (cursor !== "0");

if (count > 0) log("Updated %s entries", count);
});
}
10 changes: 4 additions & 6 deletions apps/worker/src/v4/fields/heartbeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,11 @@ export async function resolver(
extension: params.extension
};

//! Disabled for now
/* await redis.setex(
`pmd-api.heartbeatUpdates.${data.identifier}`,
// 5 minutes
300,
await redis.hset(
"pmd-api.heartbeatUpdates",
data.identifier,
JSON.stringify(data)
); */
);

return data;
}
Expand Down

0 comments on commit 20f1caf

Please sign in to comment.