Skip to content

Commit

Permalink
feat: cache guilds & common large queries to flatfiles
Browse files Browse the repository at this point in the history
hydrate caches on startup if not present
rehydrate caches hourly, queries every 10 minutes
  • Loading branch information
TobiTenno committed Jan 31, 2021
1 parent 836185f commit 6c4371a
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 125 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ jspm_packages
#ignore prod pm2 config
genesis.json
worker.json
worker.dev.json
.idea/
mypm2.json

Expand All @@ -50,4 +51,4 @@ mypm2.json
commands.json

# Docker
docker-compose.yaml
docker-compose.yaml
88 changes: 60 additions & 28 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@
"bufferutil": "^4.0.3",
"byte-size": "^7.0.0",
"colors": "^1.4.0",
"cron": "^1.8.2",
"decache": "^4.6.0",
"discord-giveaways": "^3.2.3",
"discord.js": "^12.5.1",
"erlpack": "github:discordapp/erlpack",
"flat-cache": "^3.0.4",
"json-query": "^2.2.2",
"moment": "^2.29.1",
"ms": "^2.1.3",
Expand Down
4 changes: 2 additions & 2 deletions src/WorldStateCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const EventEmitter = require('events');
const { apiBase } = require('./CommonFunctions.js');
const fetch = require('./resources/Fetcher');

const logger = require('./Logger');

const worldStateURLs = {
Expand All @@ -22,7 +23,6 @@ class WorldStateCache extends EventEmitter {
this.updating = null;
this.platform = platform;
this.updateInterval = setInterval(() => this.update(), timeout);
this.logger = logger;
this.update();
}

Expand All @@ -42,7 +42,7 @@ class WorldStateCache extends EventEmitter {
return this.currentData;
} catch (err) {
this.updating = undefined;
this.logger.error(err);
logger.error(err);
}
return this.updating;
}
Expand Down
2 changes: 1 addition & 1 deletion src/embeds/EarthCycleEmbed.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class EarthCycleEmbed extends BaseEmbed {
constructor(bot, state) {
super();

this.title = `Worldstate - ${state.isCetus ? 'Plains of Eidolon' : 'Earth'} Cycle - ${state.isDay ? 'Day' : 'Night'}time`;
this.title = `Worldstate - ${state.isCetus ? 'PoE' : 'Earth'} - ${state.isDay ? 'Day' : 'Night'}`;
this.color = state.isDay ? 0xB64624 : 0x000066;
this.thumbnail = {
url: state.isCetus ? ostron : earth,
Expand Down
29 changes: 20 additions & 9 deletions src/notifications/Broadcaster.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const bs = require('byte-size');
const logger = require('../Logger');
const cachedEvents = require('../resources/cachedEvents');

function byteFmt() {
return `${this.value}${this.unit}`;
Expand Down Expand Up @@ -37,12 +38,13 @@ const clean = (channelId, index) => {
*/
class Broadcaster {
constructor({
client = undefined, settings = undefined, messageManager = undefined,
client = undefined, settings = undefined, messageManager = undefined, workerCache = undefined,
}) {
this.client = client;
this.settings = settings;
this.webhook = messageManager.webhook;
this.shards = process.env.SHARDS;
this.workerCache = workerCache;
}

wrap(embed, ctx) {
Expand Down Expand Up @@ -72,22 +74,31 @@ class Broadcaster {
* @returns {Array.<Object>} values for successes
*/
async broadcast(embed, platform, type, items = []) {
logger.silly(`broadcasting ${type}`);
delete embed.bot;
const guilds = await this.settings.getGuilds();

const channels = await this.settings.getAgnosticNotifications(type, platform, items);
for (const result of channels) {
const index = channels.indexOf(result);
const ctx = await this.settings.getCommandContext(result.channelId);
const guilds = this.workerCache.getKey('guilds');

const channels = cachedEvents.includes(type)
? this.workerCache.getKey(`${type}:${platform}`)
: await this.settings.getAgnosticNotifications(type, platform, items);
if (!channels.length) {
logger.silly(`No channels on ${platform} tracking ${type}... continuing`);
return;
}
for (const channelId of channels) {
if (typeof channelId === 'undefined' || !channelId.length) continue;
const index = channels.indexOf(channelId);
const ctx = await this.settings.getCommandContext(channelId);

// localeCompare should return 0 if equal, so non-zero's will be truthy
if (embed.locale && ctx.language.localeCompare(embed.locale)) {
clean(result.channelId, index);
clean(channelId, index);
continue;
}

const glist = Object.entries(guilds)
.filter(([, g]) => g.channels && g.channels.includes(result.channelId))[0];
.filter(([, g]) => g.channels && g.channels.includes(channelId))[0];
const guild = glist && glist.length ? glist[1] : null;

if (!guild) continue;
Expand All @@ -99,7 +110,7 @@ class Broadcaster {
} else {
await this.webhook(ctx, { text: prepend, embed });
}
clean(result.channelId, index);
clean(channelId, index);
} catch (e) {
logger.error(e);
}
Expand Down
Loading

0 comments on commit 6c4371a

Please sign in to comment.