Retry logic for getting tip data
This commit is contained in:
parent
77417da1b4
commit
5b6ff06a7e
1 changed files with 24 additions and 7 deletions
|
|
@ -2,11 +2,13 @@
|
||||||
* @prettier
|
* @prettier
|
||||||
*/
|
*/
|
||||||
const Logger = require('winston')
|
const Logger = require('winston')
|
||||||
|
const { wait } = require('./helpers')
|
||||||
const Key = require('../services/gunDB/contact-api/key')
|
const Key = require('../services/gunDB/contact-api/key')
|
||||||
const { getUser } = require('../services/gunDB/Mediator')
|
const { getUser } = require('../services/gunDB/Mediator')
|
||||||
const LightningServices = require('./lightningServices')
|
const LightningServices = require('./lightningServices')
|
||||||
|
|
||||||
const ERROR_TRIES_THRESHOLD = 3
|
const ERROR_TRIES_THRESHOLD = 3
|
||||||
|
const ERROR_TRIES_DELAY = 500
|
||||||
const INVOICE_STATE = {
|
const INVOICE_STATE = {
|
||||||
OPEN: 'OPEN',
|
OPEN: 'OPEN',
|
||||||
SETTLED: 'SETTLED',
|
SETTLED: 'SETTLED',
|
||||||
|
|
@ -113,18 +115,33 @@ const _updateTipData = (invoiceHash, data) =>
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const _getTipData = invoiceHash =>
|
const _getTipData = (invoiceHash, tries = 0) =>
|
||||||
new Promise((resolve, reject) => {
|
new Promise((resolve, reject) => {
|
||||||
getUser()
|
if (tries >= ERROR_TRIES_THRESHOLD) {
|
||||||
.get(Key.TIPS_PAYMENT_STATUS)
|
|
||||||
.get(invoiceHash)
|
|
||||||
.once(tip => {
|
|
||||||
if (tip === undefined) {
|
|
||||||
reject(new Error('Malformed data'))
|
reject(new Error('Malformed data'))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getUser()
|
||||||
|
.get(Key.TIPS_PAYMENT_STATUS)
|
||||||
|
.get(invoiceHash)
|
||||||
|
.once(async tip => {
|
||||||
|
try {
|
||||||
|
if (tip === undefined) {
|
||||||
|
await wait(ERROR_TRIES_DELAY)
|
||||||
|
const tip = await _getTipData(invoiceHash, tries + 1)
|
||||||
|
|
||||||
|
if (tip) {
|
||||||
resolve(tip)
|
resolve(tip)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve(tip)
|
||||||
|
} catch (err) {
|
||||||
|
reject(err)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue