Skip to content

Commit

Permalink
fix: queries, remove excess deps
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiTenno committed Feb 2, 2021
1 parent c9e3381 commit a4acf0d
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 97 deletions.
5 changes: 0 additions & 5 deletions package-lock.json

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

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
"lint": "npx eslint main.js src/",
"lint:fix": "npx eslint main.js src/ --fix",
"logs": "pm2 logs genesis",
"logs:worker": "pm2 logs worker",
"logs:worker": "pm2 logs /worker.*/",
"test": "npx eslint main.js src/",
"restart": "pm2 delete genesis && npm start && npm run logs",
"setup": "npm i",
"start": "pm2 start genesis.json",
"start:logs": "npm start && npm run logs",
"start:worker": "pm2 start worker.json",
"start:logs:worker": "npm run start:worker && pm2 logs worker",
"start:logs:worker": "npm run start:worker && npm run logs:worker",
"stop": "pm2 delete genesis",
"uninstall": "pm2 kill && rm -rf node_modules/ && npm uninstall -g pm2",
"update:genesis": "pm2 delete genesis && git checkout -- . && git pull && npm i && npm restart || npm start",
Expand Down Expand Up @@ -46,7 +46,6 @@
"@spectacles/rest": "^0.8.3",
"bluebird": "^3.7.2",
"bufferutil": "^4.0.3",
"byte-size": "^7.0.0",
"colors": "^1.4.0",
"cron": "^1.8.2",
"decache": "^4.6.0",
Expand Down
2 changes: 1 addition & 1 deletion src/embeds/ErrorEmbed.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class BuildEmbed extends BaseEmbed {
constructor(error) {
super();
this.color = 0xFF0000;
this.title = 'Error';
this.title = `Error - ${process.env.SCOPE}`;
this.description = error.message || error;
if (error.stack) {
const stack = error.stack.replace(new RegExp(
Expand Down
29 changes: 0 additions & 29 deletions src/notifications/Broadcaster.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,8 @@
'use strict';

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

function byteFmt() {
return `${this.value}${this.unit}`;
}

const clean = (channelId, index) => {
if (index % 1000) return;
if (global.gc) {
try {
const v8 = require('v8');
const before = v8.getHeapStatistics();
global.gc();
const after = v8.getHeapStatistics();

const entry = {
b: { u: bs(before.used_heap_size, byteFmt), l: bs(before.heap_size_limit, byteFmt) },
a: { u: bs(after.used_heap_size, byteFmt), l: bs(after.heap_size_limit, byteFmt) },
};

logger.silly(`${channelId} ======> ${String(entry.b.u).padEnd(7)} || ${String(entry.a.u).padEnd(7)}`);
} catch (e) {
logger.info(e);
}
}
};

/**
* Broadcast updates out to subscribing channels
* @param {Discord.Client} client bot client
Expand Down Expand Up @@ -88,12 +62,10 @@ class Broadcaster {
}
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(channelId, index);
continue;
}

Expand All @@ -110,7 +82,6 @@ class Broadcaster {
} else {
await this.webhook(ctx, { text: prepend, embed });
}
clean(channelId, index);
} catch (e) {
logger.error(e);
}
Expand Down
3 changes: 2 additions & 1 deletion src/notifications/FeedsNotifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const logger = require('../Logger');

class FeedsNotifier {
constructor({
client, settings, messageManager,
client, settings, messageManager, workerCache,
}) {
this.feeder = new RssFeedEmitter({ userAgent: `RSS Feed Emitter | ${client.user.username}` });

Expand All @@ -22,6 +22,7 @@ class FeedsNotifier {
client,
settings,
messageManager,
workerCache,
});
this.feeder.on('error', logger.debug);
}
Expand Down
20 changes: 10 additions & 10 deletions src/notifications/Notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ async function getThumbnailForItem(query, fWiki) {
return url;
}
}
try {
const articles = await warframe.getSearchList({ query: fq, limit: 1 });
const details = await warframe.getArticleDetails({ ids: articles.items.map(i => i.id) });
const item = Object.values(details.items)[0];
return item && item.thumbnail ? item.thumbnail.replace(/\/revision\/.*/, '') : undefined;
} catch (e) {
logger.error(e);
}
}
return undefined;
// try {
// const articles = await warframe.getSearchList({ query: fq, limit: 1 });
// const details = await warframe.getArticleDetails({ ids: articles.items.map(i => i.id) });
// const item = Object.values(details.items)[0];
// return item && item.thumbnail ? item.thumbnail.replace(/\/revision\/.*/, '') : undefined;
// } catch (e) {
// logger.error(e);
// }
}
return '';
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/notifications/TwitchNotifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ const logger = require('../Logger');
*/
class TwitchNotifier {
constructor({
client, settings, messageManager,
client, settings, messageManager, workerCache,
}) {
this.broadcaster = new Broadcaster({
client,
settings,
messageManager,
workerCache,
});

this.lastStartedAtTime = null;
Expand Down
92 changes: 46 additions & 46 deletions src/notifications/Worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,51 +24,6 @@ const deps = {};

let timeout;

const hydrateGuilds = async () => {
if (!deps.workerCache) return;
const guilds = await db.getGuilds();
if (guilds) {
deps.workerCache.setKey('guilds', guilds);
deps.workerCache.save(true);
}
};

const hydrateQueries = async () => {
if (!deps.workerCache) return;
for (const cachedEvent of cachedEvents) {
for (const platform of activePlatforms) {
deps.workerCache.setKey(`${cachedEvent}:${platform}`,
await db.getAgnosticNotifications(cachedEvent, platform));
}
}
deps.workerCache.save(true);
};

const initCache = async () => {
deps.workerCache = flatCache.load('worker',
require('path').resolve('../../.cache'));

// generate guild cache data if not present
const currentGuilds = deps.workerCache.getKey('guilds');
if (!currentGuilds) {
await hydrateGuilds();
}

let hydrateEvents = false;
for (const cachedEvent of cachedEvents) {
for (const platform of activePlatforms) {
if (!deps.workerCache.getKey(`${cachedEvent}:${platform}`)) {
hydrateEvents = true;
}
}
}
if (hydrateEvents) await hydrateQueries();

// refresh guild cache every hour... it's a heavy process, we don't want to do it much
deps.guildHydration = new Job('0 0 * * * *', hydrateGuilds);
deps.queryHydration = new Job('0 */10 * * * *', hydrateQueries);
};

class Worker {
constructor() {
/**
Expand All @@ -84,6 +39,51 @@ class Worker {
});
}

/* eslint-disable-next-line class-methods-use-this */
async hydrateGuilds() {
const guilds = await db.getGuilds();
if (guilds) {
deps.workerCache.setKey('guilds', guilds);
deps.workerCache.save(true);
}
}

/* eslint-disable-next-line class-methods-use-this */
async hydrateQueries() {
for (const cachedEvent of cachedEvents) {
for (const platform of activePlatforms) {
deps.workerCache.setKey(`${cachedEvent}:${platform}`,
await db.getAgnosticNotifications(cachedEvent, platform));
}
}
deps.workerCache.save(true);
}

async initCache() {
deps.workerCache = flatCache.load('worker',
require('path').resolve('../../.cache'));

// generate guild cache data if not present
const currentGuilds = deps.workerCache.getKey('guilds');
if (!currentGuilds) {
await this.hydrateGuilds();
}

let hydrateEvents = false;
for (const cachedEvent of cachedEvents) {
for (const platform of activePlatforms) {
if (!deps.workerCache.getKey(`${cachedEvent}:${platform}`)) {
hydrateEvents = true;
}
}
}
if (hydrateEvents) await this.hydrateQueries();

// refresh guild cache every hour... it's a heavy process, we don't want to do it much
deps.guildHydration = new Job('0 0 * * * *', this.hydrateGuilds.bind(this));
deps.queryHydration = new Job('0 */10 * * * *', this.hydrateQueries.bind(this));
}

/**
* Start the worker notifier systems
* @returns {Promise} [description]
Expand All @@ -97,7 +97,7 @@ class Worker {

await rest.init();
await deps.settings.init();
await initCache();
await this.initCache();

this.messageManager = new MessageManager(deps);
deps.messageManager = this.messageManager;
Expand Down
2 changes: 1 addition & 1 deletion src/settings/DatabaseQueries/PingsQueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class PingsQueries {
AND ws4.setting = "webhookName" AND ws4.val IS NOT NULL `)
.append(SQL` WHERE type_notifications.type = ${String(type)} `)
.append(items && items.length ? SQL` AND item_notifications.item IN (${items})
AND item_notifications.channel_id = settings.channel_id;` : SQL`;`);
AND item_notifications.channel_id = channels.id;` : SQL`;`);
return (await this.query(query))[0]
.map(o => o.channelId)
.filter(o => o);
Expand Down

0 comments on commit a4acf0d

Please sign in to comment.