From b21c7ff41630a218613f1f1377bb912570d98cff Mon Sep 17 00:00:00 2001 From: Valentin Kaelin Date: Tue, 29 Sep 2020 18:41:13 +0200 Subject: [PATCH] refactor: retry cdragon requests if it fails --- server/app/Services/Jax/src/CDragonRequest.js | 17 +++++++++++++++-- .../Jax/src/Endpoints/CDragonEndpoint.js | 14 +++++++++----- server/app/Services/Jax/src/Jax.js | 2 +- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/server/app/Services/Jax/src/CDragonRequest.js b/server/app/Services/Jax/src/CDragonRequest.js index d2466bf..4e12e9f 100644 --- a/server/app/Services/Jax/src/CDragonRequest.js +++ b/server/app/Services/Jax/src/CDragonRequest.js @@ -1,10 +1,16 @@ +const { promisify } = require('util') const got = require('got') +const Logger = use('Logger') const Redis = use('Redis') class CDragonRequest { - constructor(endpoint, cacheTime) { + constructor(config, endpoint, cacheTime) { + this.config = config this.endpoint = endpoint 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 @@ -26,7 +32,14 @@ class CDragonRequest { await Redis.set(url, response.body, 'EX', this.cacheTime) return JSON.parse(response.body) } 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() + } } } diff --git a/server/app/Services/Jax/src/Endpoints/CDragonEndpoint.js b/server/app/Services/Jax/src/Endpoints/CDragonEndpoint.js index 3a020f3..0a77196 100644 --- a/server/app/Services/Jax/src/Endpoints/CDragonEndpoint.js +++ b/server/app/Services/Jax/src/Endpoints/CDragonEndpoint.js @@ -1,24 +1,28 @@ const CDragonRequest = require('../CDragonRequest') class CDragonEndpoint { + constructor(config) { + this.config = config + } + champions() { - return new CDragonRequest('champion-summary.json', 36000).execute() + return new CDragonRequest(this.config, 'champion-summary.json', 36000).execute() } items() { - return new CDragonRequest('items.json', 36000).execute() + return new CDragonRequest(this.config, 'items.json', 36000).execute() } perks() { - return new CDragonRequest('perks.json', 36000).execute() + return new CDragonRequest(this.config, 'perks.json', 36000).execute() } perkstyles() { - return new CDragonRequest('perkstyles.json', 36000).execute() + return new CDragonRequest(this.config, 'perkstyles.json', 36000).execute() } summonerSpells() { - return new CDragonRequest('summoner-spells.json', 36000).execute() + return new CDragonRequest(this.config, 'summoner-spells.json', 36000).execute() } } diff --git a/server/app/Services/Jax/src/Jax.js b/server/app/Services/Jax/src/Jax.js index 0541643..c565b17 100644 --- a/server/app/Services/Jax/src/Jax.js +++ b/server/app/Services/Jax/src/Jax.js @@ -22,7 +22,7 @@ class Jax { this.Spectator = new SpectatorEndpoint(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) {