MCPcopy Create free account
hub / github.com/codecombat/codecombat / pollTillResult

Function pollTillResult

app/core/api/background-job.js:24–58  ·  view source on GitHub ↗
(jobId, options = {})

Source from the content-addressed store, hash-verified

22}
23
24async 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
60module.exports = {
61 create,

Callers 1

apiClient.jsFile · 0.90

Calls 2

parseMethod · 0.80
sleepFunction · 0.50

Tested by

no test coverage detected