Skip to content

Commit

Permalink
Showing 2 changed files with 23 additions and 8 deletions.
25 changes: 18 additions & 7 deletions src/Util/Services/discord.ts
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@ import * as settings from "../../../settings.json";
import moment from "moment";
import { PresenceUpdateStatus } from "discord-api-types/v8";
import * as botCache from "./botCaching";
import { hostname } from "os";

const prefix = "statuses";

@@ -65,13 +66,23 @@ bot.on("ready", async () => {

await uploadStatuses();

botCache.getAllBots().then(bots => {
const botsToFetch = []
bots.forEach(bot => {
if (!guilds.main.members.cache.has(bot._id)) botsToFetch.push(bot._id)
})
guilds.main.members.fetch({user: botsToFetch})
})
const lock = await global.redis.get("fetch_lock");
if (lock != hostname()) {
console.log(`Skipping discord caching. The instance which holds the lock is: ${lock}`);
} else {
console.time("Bot cache");
botCache.getAllBots().then(bots => {
const botsToFetch = []
bots.forEach(bot => {
if (!guilds.main.members.cache.has(bot._id)) botsToFetch.push(bot._id)
})
guilds.main.members.fetch({user: botsToFetch})
.then(x => console.log(`Retrieved ${}`))
.catch(() => null); // It is most likely that DEL has another instance running to handle this, so catch the error and ignore.
});
console.timeEnd("Bot cache");
}

});

bot.on("presenceUpdate", async (oldPresence, newPresence) => {
6 changes: 5 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
@@ -197,7 +197,10 @@ new Promise<void>((resolve, reject) => {
});
} else {
console.log("No one has the cache lock currently, acquiring it.");
await global.redis.setex("cache_lock", 60, hostname());
// 300 seconds is a good rule of thumb, it is expected that DEL has another instance running.
await global.redis.setex("fetch_lock", 300, hostname());
console.log("Also acquired the discord lock!");
await global.redis.setex("cache_lock", 300, hostname());
console.time("Redis");
await userCache.uploadUsers();
await botCache.uploadBots();
@@ -216,6 +219,7 @@ new Promise<void>((resolve, reject) => {
console.timeEnd("Bot stats update");
await global.redis.publish("cache_lock", "ready");
await global.redis.del("cache_lock");
await global.redis.del("fetch_lock");
console.log("Dropped cache lock!");
}

0 comments on commit 9d1f918

Please sign in to comment.