-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·48 lines (40 loc) · 1.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
let restify = require('restify')
let server = restify.createServer()
var jobs = {foo: 'bar'}
server.get('/deferredjob', defer)
server.get('/monitor/:job_id', monitor)
server.get('/result/:job_id', result)
server.listen(8080, () => {
console.log('%s listening at %s', server.name, server.url);
})
function defer(req, res, next) {
const job_id = Math.random()
jobs[job_id] = null
res.header('Location', `/monitor/${job_id}`)
res.send(202, {job_id: job_id})
setTimeout(()=>{
jobs[job_id] = `Result of long-running process ${job_id}.`
// console.log(`Job ${job_id} completed.`);
}, 2000)
next()
}
function monitor(req, res, next) {
if(jobs[req.params.job_id]) {
res.header('Location', `/result/${req.params.job_id}`)
res.send(201, {job_id: req.params.job_id})
} else {
res.send(200, {job_id: 'still working...'})
}
next()
}
function result(req, res, next) {
if(jobs[req.params.job_id]) {
res.send({result: jobs[req.params.job_id]})
} else {
res.send(422, {
message: `Job ${req.params.job_id} failed to complete or was not found.`}
)
}
next()
}
module.exports = server