(jobId, options = {})
| 22 | } |
| 23 | |
| 24 | async function pollTillResult (jobId, options = {}) { |
| 25 | let url = `/db/background-jobs/${jobId}` |
| 26 | if (options.other) { |
| 27 | url = '/' + options.other + url |
| 28 | } |
| 29 | const fetchOptions = options.fetchOptions || {} |
| 30 | let job = await fetchJson(url, fetchOptions) |
| 31 | const MAX_ATTEMPTS = 30 |
| 32 | const DELAY_MS = 5000 |
| 33 | let attempts = 0 |
| 34 | while (job.status !== 'completed' && job.status !== 'failed' && attempts < MAX_ATTEMPTS) { |
| 35 | await sleep(DELAY_MS) |
| 36 | job = await fetchJson(url, fetchOptions) |
| 37 | attempts++ |
| 38 | |
| 39 | if (options.showNotification && job.message) { |
| 40 | noty({ |
| 41 | text: job.message, |
| 42 | type: 'warning', |
| 43 | layout: 'topCenter', |
| 44 | timeout: 3000, |
| 45 | }) |
| 46 | } |
| 47 | } |
| 48 | if (job.status === 'completed') { |
| 49 | const output = job.output || '{}' |
| 50 | return JSON.parse(output) |
| 51 | } |
| 52 | if (job.status === 'failed') { |
| 53 | throw new Error(job.message) |
| 54 | } |
| 55 | if (attempts === MAX_ATTEMPTS) { |
| 56 | throw new Error('Failed to load the results') |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | module.exports = { |
| 61 | create, |
no test coverage detected