Skip to content

Commit

Permalink
Clan cache warmup #520 (#526)
Browse files Browse the repository at this point in the history
* create crawler

* Add crawler to cron scheduler

* change indentation for a sane line length

* cleanup - add comment

* cleanup wordpress cache job

* more clean

* add opts to cache service - logging for testing

* undo opts to cache service

* remove logging

* change ttl on clanService
  • Loading branch information
beckpaul authored Dec 6, 2023
1 parent e954663 commit 0d21d36
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/backend/AppKernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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 () {
Expand Down
35 changes: 35 additions & 0 deletions src/backend/cron-jobs/clanCacheCrawler.js
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 2 additions & 1 deletion src/backend/cron-jobs/leaderboardCacheCrawler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/backend/cron-jobs/wordpressCacheCrawler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/backend/services/ClanService.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { MutexService } = require('./MutexService')
const clanTTL = 60 * 5
const clanTTL = 60 * 60

class ClanService {
constructor (cacheService, dataRepository, lockTimeout = 3000) {
Expand Down

0 comments on commit 0d21d36

Please sign in to comment.