From 035877c368cddbc8186d6e0970729ef5924f80d1 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Mon, 3 Sep 2018 20:38:56 +0200 Subject: [PATCH] reachableFrom: retry :bug: --- index.js | 80 ++++++++++++++++++++++++++++++---------------------- package.json | 1 + 2 files changed, 48 insertions(+), 33 deletions(-) diff --git a/index.js b/index.js index bd63f9ec..15e6f612 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,7 @@ const minBy = require('lodash/minBy') const maxBy = require('lodash/maxBy') const isObj = require('lodash/isObject') const sortBy = require('lodash/sortBy') +const pRetry = require('p-retry') const defaultProfile = require('./lib/default-profile') const createParseBitmask = require('./parse/products-bitmask') @@ -474,40 +475,53 @@ const createClient = (profile, userAgent, request = _request) => { }, opt) if (Number.isNaN(+opt.when)) throw new Error('opt.when is invalid') - return request(profile, userAgent, opt, { - meth: 'LocGeoReach', - req: { - loc: profile.formatLocation(profile, address, 'address'), - maxDur: opt.maxDuration, - maxChg: opt.maxTransfers, - date: profile.formatDate(profile, opt.when), - time: profile.formatTime(profile, opt.when), - period: 120, // todo: what is this? - jnyFltrL: [ - profile.formatProductsFilter(opt.products || {}) - ] - } - }) - .then((d) => { - if (!Array.isArray(d.posL)) throw new Error('invalid response') - - const byDuration = [] - let i = 0, lastDuration = NaN - for (const pos of sortBy(d.posL, 'dur')) { - const loc = d.locations[pos.locX] - if (!loc) continue - if (pos.dur !== lastDuration) { - lastDuration = pos.dur - i = byDuration.length - byDuration.push({ - duration: pos.dur, - stations: [loc] - }) - } else { - byDuration[i].stations.push(loc) + const refetch = () => { + return request(profile, userAgent, opt, { + meth: 'LocGeoReach', + req: { + loc: profile.formatLocation(profile, address, 'address'), + maxDur: opt.maxDuration, + maxChg: opt.maxTransfers, + date: profile.formatDate(profile, opt.when), + time: profile.formatTime(profile, opt.when), + period: 120, // todo: what is this? + jnyFltrL: [ + profile.formatProductsFilter(opt.products || {}) + ] } - } - return byDuration + }) + .then((d) => { + if (!Array.isArray(d.posL)) { + console.error('d', d) + const err = new Error('invalid response') + err.shouldRetry = true + throw err + } + + const byDuration = [] + let i = 0, lastDuration = NaN + for (const pos of sortBy(d.posL, 'dur')) { + const loc = d.locations[pos.locX] + if (!loc) continue + if (pos.dur !== lastDuration) { + lastDuration = pos.dur + i = byDuration.length + byDuration.push({ + duration: pos.dur, + stations: [loc] + }) + } else { + byDuration[i].stations.push(loc) + } + } + return byDuration + }) + } + + return pRetry(refetch, { + retries: 3, + factor: 2, + minTimeout: 2 * 1000 }) } diff --git a/package.json b/package.json index dbd8ca40..150964ba 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "gps-distance": "0.0.4", "lodash": "^4.17.5", "luxon": "^1.3.0", + "p-retry": "^2.0.0", "p-throttle": "^1.1.0", "pinkie-promise": "^2.0.1", "query-string": "^6.0.0",