Skip to content

Commit

Permalink
Merge pull request #399 from ZIMkaRU/bugfix/improve-interruption-for-…
Browse files Browse the repository at this point in the history
…tax-report

Improve interruption for tax report
  • Loading branch information
ezewer authored Sep 5, 2024
2 parents 76d569a + 63d9ca8 commit 8bfdac1
Showing 1 changed file with 28 additions and 15 deletions.
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

0 comments on commit 8bfdac1

Please sign in to comment.