feat: update cdragon cache every 30min

This commit is contained in:
Valentin Kaelin 2023-09-22 22:37:15 +02:00
parent d88d09d905
commit d3125c4bb4
7 changed files with 69 additions and 17 deletions

View file

@ -5,7 +5,8 @@
"@adonisjs/core/build/commands/index.js",
"@adonisjs/repl/build/commands",
"@adonisjs/lucid/build/commands",
"@rocketseat/adonis-bull/build/commands"
"@rocketseat/adonis-bull/build/commands",
"@stouder-io/adonis-scheduler/build/commands/MakeTask"
],
"exceptionHandlerNamespace": "App/Exceptions/Handler",
"aliases": {
@ -32,7 +33,8 @@
"@adonisjs/core",
"@adonisjs/lucid",
"@adonisjs/redis",
"@rocketseat/adonis-bull"
"@rocketseat/adonis-bull",
"@stouder-io/adonis-scheduler"
],
"aceProviders": [
"@adonisjs/repl"

View file

@ -649,6 +649,26 @@
"alias": "p"
}
]
},
"make:task": {
"settings": {
"loadApp": false,
"stayAlive": false
},
"commandPath": "@stouder-io/adonis-scheduler/build/commands/MakeTask",
"commandName": "make:task",
"description": "Make a new task",
"args": [
{
"type": "string",
"propertyName": "name",
"name": "name",
"required": true,
"description": "Name of the job class"
}
],
"aliases": [],
"flags": []
}
},
"aliases": {}

View file

@ -29,8 +29,10 @@ class CDragonService {
public readonly BASE_URL =
'https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/'
private setupCache<T extends Identifiable>(dto: T[]) {
return dto.reduce((obj, item) => ((obj[item.id] = item), obj), {})
private setupCache<T extends Identifiable>(key: string, dto: T[]) {
if (dto.length === 0) return
this[key] = dto.reduce((obj, item) => ((obj[item.id] = item), obj), {})
}
/**
@ -45,19 +47,23 @@ class CDragonService {
* Get global Context with CDragon Data
*/
public async getContext() {
const items = await Jax.CDragon.items()
const champions = await Jax.CDragon.champions()
const perks = await Jax.CDragon.perks()
const perkstyles = await Jax.CDragon.perkstyles()
const summonerSpells = await Jax.CDragon.summonerSpells()
const championRoles = await RoleIdentificationService.pullData().catch(() => {})
const [items, champions, perks, perkstyles, summonerSpells, championRoles] = await Promise.all([
Jax.CDragon.items(),
Jax.CDragon.champions(),
Jax.CDragon.perks(),
Jax.CDragon.perkstyles(),
Jax.CDragon.summonerSpells(),
RoleIdentificationService.pullData().catch(() => {}),
])
this.champions = this.setupCache(champions)
this.items = this.setupCache(items)
this.perks = this.setupCache(perks)
this.perkstyles = this.setupCache(perkstyles.styles)
this.summonerSpells = this.setupCache(summonerSpells)
this.championRoles = championRoles as ChampionsPlayRate
this.setupCache('champions', champions)
this.setupCache('items', items)
this.setupCache('perks', perks)
this.setupCache('perkstyles', perkstyles.styles)
this.setupCache('summonerSpells', summonerSpells)
if (championRoles) {
this.championRoles = championRoles
}
}
}

View file

@ -0,0 +1,11 @@
import { TaskContract } from '@ioc:StouderIO/Scheduler'
import CDragonService from 'App/Services/CDragonService'
export default class UpdateCDragonCache implements TaskContract {
public readonly name: string = 'UpdateCDragonCache'
public readonly cron: string = '*/30 * * * *' // every 30 minutes
public async run(): Promise<void> {
await CDragonService.getContext()
}
}

View file

@ -13,6 +13,7 @@
"@adonisjs/redis": "^7.3.4",
"@adonisjs/repl": "^3.1.11",
"@rocketseat/adonis-bull": "^1.0.4",
"@stouder-io/adonis-scheduler": "^1.0.3",
"got": "^11.8.5",
"luxon": "^3.4.3",
"pg": "^8.11.3",
@ -1037,6 +1038,16 @@
"url": "https://github.com/sindresorhus/is?sponsor=1"
}
},
"node_modules/@stouder-io/adonis-scheduler": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/@stouder-io/adonis-scheduler/-/adonis-scheduler-1.0.3.tgz",
"integrity": "sha512-F1MTFU18HyBrh7ekNJoUhUMaUgzD89juIWbn2IQQieGYg7XnTVoRcbwS1zE5W8SFdrga2+w0+4EUidCdqiamVg==",
"dependencies": {
"@poppinss/utils": "^5.0.0",
"cron-parser": "^4.7.0",
"slash": "^3.0.0"
}
},
"node_modules/@szmarczak/http-timer": {
"version": "4.0.6",
"resolved": "https://registry.npmjs.org/@szmarczak/http-timer/-/http-timer-4.0.6.tgz",

View file

@ -29,6 +29,7 @@
"@adonisjs/redis": "^7.3.4",
"@adonisjs/repl": "^3.1.11",
"@rocketseat/adonis-bull": "^1.0.4",
"@stouder-io/adonis-scheduler": "^1.0.3",
"got": "^11.8.5",
"luxon": "^3.4.3",
"pg": "^8.11.3",

View file

@ -30,7 +30,8 @@
"@adonisjs/repl",
"@adonisjs/lucid",
"@adonisjs/redis",
"@rocketseat/adonis-bull"
"@rocketseat/adonis-bull",
"@stouder-io/adonis-scheduler"
]
}
}