mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 07:09:35 +02:00
reachableFrom: retry 🐛
This commit is contained in:
parent
dfad3d9d84
commit
035877c368
2 changed files with 48 additions and 33 deletions
80
index.js
80
index.js
|
@ -4,6 +4,7 @@ const minBy = require('lodash/minBy')
|
||||||
const maxBy = require('lodash/maxBy')
|
const maxBy = require('lodash/maxBy')
|
||||||
const isObj = require('lodash/isObject')
|
const isObj = require('lodash/isObject')
|
||||||
const sortBy = require('lodash/sortBy')
|
const sortBy = require('lodash/sortBy')
|
||||||
|
const pRetry = require('p-retry')
|
||||||
|
|
||||||
const defaultProfile = require('./lib/default-profile')
|
const defaultProfile = require('./lib/default-profile')
|
||||||
const createParseBitmask = require('./parse/products-bitmask')
|
const createParseBitmask = require('./parse/products-bitmask')
|
||||||
|
@ -474,40 +475,53 @@ const createClient = (profile, userAgent, request = _request) => {
|
||||||
}, opt)
|
}, opt)
|
||||||
if (Number.isNaN(+opt.when)) throw new Error('opt.when is invalid')
|
if (Number.isNaN(+opt.when)) throw new Error('opt.when is invalid')
|
||||||
|
|
||||||
return request(profile, userAgent, opt, {
|
const refetch = () => {
|
||||||
meth: 'LocGeoReach',
|
return request(profile, userAgent, opt, {
|
||||||
req: {
|
meth: 'LocGeoReach',
|
||||||
loc: profile.formatLocation(profile, address, 'address'),
|
req: {
|
||||||
maxDur: opt.maxDuration,
|
loc: profile.formatLocation(profile, address, 'address'),
|
||||||
maxChg: opt.maxTransfers,
|
maxDur: opt.maxDuration,
|
||||||
date: profile.formatDate(profile, opt.when),
|
maxChg: opt.maxTransfers,
|
||||||
time: profile.formatTime(profile, opt.when),
|
date: profile.formatDate(profile, opt.when),
|
||||||
period: 120, // todo: what is this?
|
time: profile.formatTime(profile, opt.when),
|
||||||
jnyFltrL: [
|
period: 120, // todo: what is this?
|
||||||
profile.formatProductsFilter(opt.products || {})
|
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)
|
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
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
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,6 +40,7 @@
|
||||||
"gps-distance": "0.0.4",
|
"gps-distance": "0.0.4",
|
||||||
"lodash": "^4.17.5",
|
"lodash": "^4.17.5",
|
||||||
"luxon": "^1.3.0",
|
"luxon": "^1.3.0",
|
||||||
|
"p-retry": "^2.0.0",
|
||||||
"p-throttle": "^1.1.0",
|
"p-throttle": "^1.1.0",
|
||||||
"pinkie-promise": "^2.0.1",
|
"pinkie-promise": "^2.0.1",
|
||||||
"query-string": "^6.0.0",
|
"query-string": "^6.0.0",
|
||||||
|
|
Loading…
Add table
Reference in a new issue