refactor: retry cdragon requests if it fails

This commit is contained in:
Valentin Kaelin 2020-09-29 18:41:13 +02:00
parent e73b231db2
commit b21c7ff416
3 changed files with 25 additions and 8 deletions

View file

@ -1,10 +1,16 @@
const { promisify } = require('util')
const got = require('got') const got = require('got')
const Logger = use('Logger')
const Redis = use('Redis') const Redis = use('Redis')
class CDragonRequest { class CDragonRequest {
constructor(endpoint, cacheTime) { constructor(config, endpoint, cacheTime) {
this.config = config
this.endpoint = endpoint this.endpoint = endpoint
this.cacheTime = cacheTime this.cacheTime = cacheTime
this.retries = config.requestOptions.retriesBeforeAbort
this.sleep = promisify(setTimeout)
} }
// https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/v1/items.json // https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/v1/items.json
@ -26,7 +32,14 @@ class CDragonRequest {
await Redis.set(url, response.body, 'EX', this.cacheTime) await Redis.set(url, response.body, 'EX', this.cacheTime)
return JSON.parse(response.body) return JSON.parse(response.body)
} catch (error) { } catch (error) {
console.log(error.response.body); this.retries--
Logger.transport('file').error('CDragon Error : ', error)
if (this.retries > 0) {
await this.sleep(this.config.requestOptions.delayBeforeRetry)
return this.execute()
}
} }
} }

View file

@ -1,24 +1,28 @@
const CDragonRequest = require('../CDragonRequest') const CDragonRequest = require('../CDragonRequest')
class CDragonEndpoint { class CDragonEndpoint {
constructor(config) {
this.config = config
}
champions() { champions() {
return new CDragonRequest('champion-summary.json', 36000).execute() return new CDragonRequest(this.config, 'champion-summary.json', 36000).execute()
} }
items() { items() {
return new CDragonRequest('items.json', 36000).execute() return new CDragonRequest(this.config, 'items.json', 36000).execute()
} }
perks() { perks() {
return new CDragonRequest('perks.json', 36000).execute() return new CDragonRequest(this.config, 'perks.json', 36000).execute()
} }
perkstyles() { perkstyles() {
return new CDragonRequest('perkstyles.json', 36000).execute() return new CDragonRequest(this.config, 'perkstyles.json', 36000).execute()
} }
summonerSpells() { summonerSpells() {
return new CDragonRequest('summoner-spells.json', 36000).execute() return new CDragonRequest(this.config, 'summoner-spells.json', 36000).execute()
} }
} }

View file

@ -22,7 +22,7 @@ class Jax {
this.Spectator = new SpectatorEndpoint(this.config, this.limiter) this.Spectator = new SpectatorEndpoint(this.config, this.limiter)
this.Summoner = new SummonerEndpoint(this.config, this.limiter) this.Summoner = new SummonerEndpoint(this.config, this.limiter)
this.CDragon = new CDragonEndpoint() this.CDragon = new CDragonEndpoint(this.config)
} }
set regionName(regionName) { set regionName(regionName) {