Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve interruption for tax report #399

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 28 additions & 15 deletions workers/loc.api/helpers/get-data-from-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,26 @@ const {
} = require('./api-errors-testers')

const _delay = (mc = 80000, interrupter) => {
if (_isInterrupted(interrupter)) {
return Promise.resolve({ isInterrupted: true })
}

return new Promise((resolve) => {
const hasInterrupter = interrupter instanceof Interrupter
const timeout = setTimeout(() => {
if (hasInterrupter) {
interrupter.offInterrupt(onceInterruptHandler)
}

resolve()
resolve({ isInterrupted: false })
}, mc)
const onceInterruptHandler = () => {
if (!timeout.hasRef()) {
return
}

clearTimeout(timeout)
resolve()
resolve({ isInterrupted: true })
}

if (hasInterrupter) {
Expand Down Expand Up @@ -116,11 +120,12 @@ module.exports = (
if (countRateLimitError > 100) {
throw err
}
if (_isInterrupted(_interrupter)) {
return { isInterrupted: true }
}

await _delay(ms, _interrupter)
const { isInterrupted } = await _delay(ms, _interrupter)

if (isInterrupted) {
return { isInterrupted }
}

continue
}
Expand All @@ -130,11 +135,12 @@ module.exports = (
if (countNonceSmallError > 20) {
throw err
}
if (_isInterrupted(_interrupter)) {
return { isInterrupted: true }
}

await _delay(1000, _interrupter)
const { isInterrupted } = await _delay(1000, _interrupter)

if (isInterrupted) {
return { isInterrupted }
}

continue
}
Expand All @@ -156,7 +162,13 @@ module.exports = (
await wsEventEmitter.emitENetError(callerName)
}

await _delay(eNetErrorAttemptsTimeoutMs, _interrupter)
const {
isInterrupted
} = await _delay(eNetErrorAttemptsTimeoutMs, _interrupter)

if (isInterrupted) {
return { isInterrupted }
}

continue
}
Expand All @@ -170,11 +182,12 @@ module.exports = (
) {
throw err
}
if (_isInterrupted(_interrupter)) {
return { isInterrupted: true }
}

await _delay(10000, _interrupter)
const { isInterrupted } = await _delay(10000, _interrupter)

if (isInterrupted) {
return { isInterrupted }
}
}
}

Expand Down