tryandwait routine

This commit is contained in:
Daniel Lugo 2020-02-24 14:30:49 -04:00
parent 7600029037
commit 9cf80a655a

View file

@ -1,6 +1,8 @@
/** /**
* @format * @format
*/ */
const logger = require('winston')
const ErrorCode = require('../errorCode') const ErrorCode = require('../errorCode')
const Key = require('../key') const Key = require('../key')
@ -37,19 +39,68 @@ const timeout10 = promise => {
]) ])
} }
/**
* @template T
* @param {Promise<T>} promise
* @returns {Promise<T>}
*/
const timeout5 = promise => {
return Promise.race([
promise,
new Promise((_, rej) => {
setTimeout(() => {
rej(new Error(ErrorCode.TIMEOUT_ERR))
}, 5000)
})
])
}
/** /**
* @template T * @template T
* @param {(gun: GUNNode, user: UserGUNNode) => Promise<T>} promGen The function * @param {(gun: GUNNode, user: UserGUNNode) => Promise<T>} promGen The function
* receives the most recent gun and user instances. * receives the most recent gun and user instances.
* @returns {Promise<T>} * @returns {Promise<T>}
*/ */
const tryAndWait = promGen => const tryAndWait = async promGen => {
timeout10( /* eslint-disable no-empty */
// If hang stop at 10, wait 3, retry, if hang stop at 5, reinstate, warm for
// 5, retry, stop at 10, err
try {
return await timeout10(
promGen(
require('../../Mediator/index').getGun(),
require('../../Mediator/index').getUser()
)
)
} catch (_) {}
logger.info(`\n retrying \n`)
await delay(3000)
try {
return await timeout5(
promGen(
require('../../Mediator/index').getGun(),
require('../../Mediator/index').getUser()
)
)
} catch (_) {}
logger.info(`\n recreating gun and retrying one last time \n`)
await require('../../Mediator/index').instantiateGun()
return timeout10(
promGen( promGen(
require('../../Mediator/index').getGun(), require('../../Mediator/index').getGun(),
require('../../Mediator/index').getUser() require('../../Mediator/index').getUser()
) )
) )
/* eslint-enable no-empty */
}
/** /**
* @param {string} pub * @param {string} pub
@ -74,7 +125,7 @@ const pubToEpub = async pub => {
return epub return epub
} catch (err) { } catch (err) {
console.log(err) logger.error(err)
throw new Error(`pubToEpub() -> ${err.message}`) throw new Error(`pubToEpub() -> ${err.message}`)
} }
} }