Skip to content

Commit

Permalink
Better caching
Browse files Browse the repository at this point in the history
  • Loading branch information
IceeMC committed Feb 26, 2022
1 parent e0ab97f commit 970bf97
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 25 deletions.
17 changes: 9 additions & 8 deletions src/Routes/bots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ router.post(
error = true;
errors.push(res.__("common.error.bot.arr.clientIDTooLong"));
}

await discord.bot.api.users(req.body.clientID).get()
.then(() => {
error = true;
Expand Down Expand Up @@ -543,7 +543,7 @@ router.post(
status: 400,
errors: [res.__("common.error.bot.arr.noBot")]
});

await global.db.collection<delBot>("bots").insertOne({
_id: req.body.id,
clientID: req.body.clientID,
Expand Down Expand Up @@ -1490,7 +1490,7 @@ router.post(
status: 400,
errors: [res.__("common.error.bot.arr.notPublic")]
});

await global.db.collection("bots").updateOne(
{ _id: req.params.id },
{
Expand Down Expand Up @@ -1645,7 +1645,7 @@ router.post(
.catch((e) => {
console.error(e);
});

return res.status(200).json({
error: false,
status: 200,
Expand Down Expand Up @@ -1789,6 +1789,7 @@ router.get("/:id", variables, async (req: Request, res: Response) => {
staffServer: settings.guild.staff,
webUrl: settings.website.url,
req: req,
// @ts-ignore
editors: (editors.filter(editor => editor !== '')),
votes: bot.votes.positive.length - bot.votes.negative.length,
functions,
Expand Down Expand Up @@ -2394,7 +2395,7 @@ router.post(
error = true;
errors.push(res.__("common.error.bot.arr.noScopes"));
}

if (req.body.clientID) {
if (isNaN(req.body.clientID) || req.body.clientID.includes(" ")) {
error = true;
Expand Down Expand Up @@ -2812,7 +2813,7 @@ router.post(
status: 400,
errors: [res.__("common.error.bot.arr.notPublic")]
});

await global.db.collection("bots").updateOne(
{ _id: req.params.id },
{
Expand Down Expand Up @@ -2965,7 +2966,7 @@ router.post(
.catch((e) => {
console.error(e);
});

return res.status(200).json({
error: false,
status: 200,
Expand Down Expand Up @@ -4031,7 +4032,7 @@ router.get(
status: 400,
errors: [res.__("common.error.bot.arr.notPublic")]
});

await global.db.collection("bots").updateOne(
{ _id: req.params.id },
{
Expand Down
60 changes: 43 additions & 17 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import i18n from "i18n";
import * as settings from "../settings.json";
import { MongoClient } from "mongodb";
import { RedisOptions } from "ioredis";
import { hostname } from "os";

const app = express();

Expand Down Expand Up @@ -158,27 +159,52 @@ new Promise<void>((resolve, reject) => {
}

global.redis = new (require("ioredis"))(redisConfig);
const s = new (require("ioredis"))(redisConfig);

/*There is no point in flushing the DEL redis database, it's persistent as is, and will lead to problems.
- Ice*/

console.time("Redis");
await userCache.uploadUsers();
await botCache.uploadBots();
await serverCache.uploadServers();
await templateCache.uploadTemplates();
await auditCache.uploadAuditLogs();
await libCache.cacheLibs();
await announcementCache.updateCache();
await featuredCache.updateFeaturedServers();
await featuredCache.updateFeaturedTemplates();
await ddosMode.updateCache();
await tokenManager.tokenResetAll();
console.timeEnd("Redis");

console.time("Bot stats update");
await botStatsUpdate();
console.timeEnd("Bot stats update");
console.log("Attempting to acquire caching lock...");
const lock = await global.redis.get("cache_lock");
if (lock && lock != hostname()) { // We have a lock, but it is not held for us.
console.log(`Lock is currently held by ${lock}. Waiting for caching to finish before proceeding...`);
await new Promise<void>((res, _) => {
s.subscribe("cache_ready", err => {
if (err) {
console.error(`Subscription failed: ${err}, exiting...`);
process.exit();
}
});
s.on("message", (chan, m) => {
if (chan === "cache_ready" && m === "ready") {
res();
console.log("Caching has completed, app will continue starting.");
}
});
});
} else {
console.log("No one has the cache lock currently, acquiring it.");
await global.redis.setex("cache_lock", 300, hostname());
console.time("Redis");
await userCache.uploadUsers();
await botCache.uploadBots();
await serverCache.uploadServers();
await templateCache.uploadTemplates();
await auditCache.uploadAuditLogs();
await libCache.cacheLibs();
await announcementCache.updateCache();
await featuredCache.updateFeaturedServers();
await featuredCache.updateFeaturedTemplates();
await ddosMode.updateCache();
await tokenManager.tokenResetAll();
console.timeEnd("Redis");
console.time("Bot stats update");
await botStatsUpdate();
console.timeEnd("Bot stats update");
await global.redis.publish("cache_lock", "ready");
await global.redis.del("cache_lock");
console.log("Dropped cache lock!");
}

await discord.bot.login(settings.secrets.discord.token);

Expand Down

0 comments on commit 970bf97

Please sign in to comment.