Skip to content

Commit

Permalink
feat: add ability to force hydration from cli arg
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiTenno committed Feb 10, 2021
1 parent 5289eb5 commit cbfef56
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/notifications/Notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const { embeds } = require('./NotifierUtils');
const Broadcaster = require('./Broadcaster');
const logger = require('../Logger');

require('colors');

const {
createGroupedArray, apiBase, apiCdnBase, platforms, captures,
} = require('../CommonFunctions');
Expand Down Expand Up @@ -143,7 +145,7 @@ class Notifier {
messageManager,
workerCache,
});
logger.info('[N] Ready');
logger.info(`[${'N'.cyan}] Ready`);

platforms.forEach((p) => {
beats[p] = {
Expand Down
4 changes: 3 additions & 1 deletion src/notifications/TwitchNotifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const Broadcaster = require('./Broadcaster');
const { platforms } = require('../CommonFunctions');
const logger = require('../Logger');

require('colors');

/**
* Watches for Twitch go-lives and broadcasts them
*/
Expand Down Expand Up @@ -52,7 +54,7 @@ class TwitchNotifier {
logger.error(`initialzation error: ${e.message}`);
return;
}
logger.info('[Twitch] Ready');
logger.info(`[${'Twitch'.purple}] Ready`);
}

/**
Expand Down
16 changes: 10 additions & 6 deletions src/notifications/Worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const flatCache = require('flat-cache');
const Job = require('cron').CronJob;
require('colors');

const Notifier = require('./Notifier');
const FeedsNotifier = require('./FeedsNotifier');
Expand All @@ -13,9 +14,7 @@ const Rest = require('../tools/RESTWrapper');
const Database = require('../settings/Database');

const { logger, platforms } = require('./NotifierUtils');

const { emojify, games } = require('../CommonFunctions');

const cachedEvents = require('../resources/cachedEvents');

const activePlatforms = (process.env.PLATFORMS || 'pc').split(',');
Expand All @@ -27,6 +26,10 @@ const deps = {};

let timeout;

const forceHydrate = (process.argv[2] || '').includes('-hydrate');

logger.info(`forceHydrate: ${forceHydrate}`);

class Worker {
constructor() {
/**
Expand Down Expand Up @@ -66,13 +69,12 @@ class Worker {
deps.workerCache = flatCache.load('worker',
require('path').resolve('../../.cache'));

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

let hydrateEvents = false;
let hydrateEvents = forceHydrate;
for (const cachedEvent of cachedEvents) {
for (const platform of activePlatforms) {
if (!deps.workerCache.getKey(`${cachedEvent}:${platform}`)) {
Expand All @@ -81,6 +83,8 @@ class Worker {
}
}
if (hydrateEvents) await this.hydrateQueries();
const eDate = Date.now();
logger.info(`[${'DB'.brightMagenta}] hydration took ${String(eDate-sDate).red}ms`)

// 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));
Expand Down

0 comments on commit cbfef56

Please sign in to comment.