diff --git a/src/backend/AppKernel.js b/src/backend/AppKernel.js index b0c12564..bafc21ac 100644 --- a/src/backend/AppKernel.js +++ b/src/backend/AppKernel.js @@ -11,6 +11,7 @@ const flash = require('connect-flash') const FileStore = require('session-file-store')(session) const wordpressCacheCrawler = require('./cron-jobs/wordpressCacheCrawler') const leaderboardCacheCrawler = require('./cron-jobs/leaderboardCacheCrawler') +const clanCacheCrawler = require('./cron-jobs/clanCacheCrawler') const defaultRouter = require('./routes/views/defaultRouter') const authRouter = require('./routes/views/auth') const staticMarkdownRouter = require('./routes/views/staticMarkdownRouter') @@ -122,6 +123,7 @@ class AppKernel { startCronJobs () { this.schedulers.push(leaderboardCacheCrawler(this.appContainer.get('LeaderboardService'))) this.schedulers.push(wordpressCacheCrawler(this.appContainer.get('WordpressService'))) + this.schedulers.push(clanCacheCrawler(this.appContainer.get('ClanService'))) } loadControllers () { diff --git a/src/backend/cron-jobs/clanCacheCrawler.js b/src/backend/cron-jobs/clanCacheCrawler.js new file mode 100644 index 00000000..b1ae1e86 --- /dev/null +++ b/src/backend/cron-jobs/clanCacheCrawler.js @@ -0,0 +1,35 @@ +const Scheduler = require('../services/Scheduler') + +const successHandler = (name) => { + console.debug('[debug] Cache updated', { name }) +} +const errorHandler = (e, name) => { + console.error(e.toString(), { name, entrypoint: 'clanCacheCrawler.js' }) + console.error(e.stack) +} + +const warmupClans = async (clanService) => { + try { + await clanService.getAll(true) + .then(() => successHandler('clanService::getAll(global)')) + .catch((e) => errorHandler(e, 'clanService::getAll(global)')) + } catch (e) { + console.error('Error: clanCacheCrawler::warmupClans failed with "' + e.toString() + '"', + { entrypoint: 'clanCacheCrawler.js' }) + console.error(e.stack) + } +} + +/** + * @param {clanService} clanService + * @return {Scheduler[]} + */ +module.exports = (clanService) => { + warmupClans(clanService).then(() => {}) + + const clansScheduler = new Scheduler('createClanCache', // Refresh cache every 59 minutes + () => warmupClans(clanService).then(() => {}), 60 * 59 * 1000) + clansScheduler.start() + + return clansScheduler +} diff --git a/src/backend/cron-jobs/leaderboardCacheCrawler.js b/src/backend/cron-jobs/leaderboardCacheCrawler.js index 7c15f351..8e818a5b 100644 --- a/src/backend/cron-jobs/leaderboardCacheCrawler.js +++ b/src/backend/cron-jobs/leaderboardCacheCrawler.js @@ -38,7 +38,8 @@ const warmupLeaderboard = async (leaderboardService) => { module.exports = (leaderboardService) => { warmupLeaderboard(leaderboardService).then(() => {}) - const leaderboardScheduler = new Scheduler('createLeaderboardCaches', () => warmupLeaderboard(leaderboardService).then(() => {}), 60 * 59 * 1000) + const leaderboardScheduler = new Scheduler('createLeaderboardCaches', + () => warmupLeaderboard(leaderboardService).then(() => {}), 60 * 59 * 1000) leaderboardScheduler.start() return leaderboardScheduler diff --git a/src/backend/cron-jobs/wordpressCacheCrawler.js b/src/backend/cron-jobs/wordpressCacheCrawler.js index d570dddc..abd5e7d1 100644 --- a/src/backend/cron-jobs/wordpressCacheCrawler.js +++ b/src/backend/cron-jobs/wordpressCacheCrawler.js @@ -37,13 +37,13 @@ const warmupWordpressCache = (wordpressService) => { /** * @param {WordpressService} wordpressService - * @param {LeaderboardService} leaderboardService * @return {Scheduler[]} */ -module.exports = (wordpressService, leaderboardService) => { +module.exports = (wordpressService) => { warmupWordpressCache(wordpressService) - const wordpressScheduler = new Scheduler('createWordpressCaches', () => warmupWordpressCache(wordpressService), 60 * 59 * 1000) + const wordpressScheduler = new Scheduler('createWordpressCaches', + () => warmupWordpressCache(wordpressService), 60 * 59 * 1000) wordpressScheduler.start() return wordpressScheduler diff --git a/src/backend/services/ClanService.js b/src/backend/services/ClanService.js index 5a80b7fe..f95b345c 100644 --- a/src/backend/services/ClanService.js +++ b/src/backend/services/ClanService.js @@ -1,5 +1,5 @@ const { MutexService } = require('./MutexService') -const clanTTL = 60 * 5 +const clanTTL = 60 * 60 class ClanService { constructor (cacheService, dataRepository, lockTimeout = 3000) {