Better status reporting and handling of return codes

This commit is contained in:
2021-03-03 14:00:54 +00:00
parent 05b1260483
commit e6d47a7b62
2 changed files with 116 additions and 37 deletions

View File

@@ -47,7 +47,7 @@ router.delete('/queue/:id/:calcFn', function(req, res) {
}) })
// Get the status of a task as a text string // Poll the status of a task as a text string
const processStatus = function(id, calcFn) { const processStatus = function(id, calcFn) {
if (calcQueueSvc.isInProgress(id, calcFn)) { if (calcQueueSvc.isInProgress(id, calcFn)) {
return 'in_progress' return 'in_progress'
@@ -60,6 +60,43 @@ const processStatus = function(id, calcFn) {
} }
} }
// Poll the status of an individual task
router.get('/poll/:id', function(req, res) {
const status = processStatus(req.params.id, null)
if (status == 'finished') {
res.status(statusCodes.OK).send({
id: req.params.id,
status: status,
retCode: calcQueueSvc.retCode(req.params.id, null)
})
} else {
res.status(statusCodes.OK).send({
id: req.params.id,
status: status
})
}
})
// Poll the status of an individual task
router.get('/poll/:id/:calcFn', function(req, res) {
const status = processStatus(req.params.id, req.params.calcFn)
if (status == 'finished') {
res.status(statusCodes.OK).send({
id: req.params.id,
calcFn: req.params.calcFn,
status: status,
retCode: calcQueueSvc.retCode(req.params.id, req.params.calcFn)
})
} else {
res.status(statusCodes.OK).send({
id: req.params.id,
calcFn: req.params.calcFn,
status: status
})
}
})
// Get the status of the queue and all tasks // Get the status of the queue and all tasks
router.get('/status', function(req, res) { router.get('/status', function(req, res) {
res.status(statusCodes.OK).send({ res.status(statusCodes.OK).send({
@@ -71,19 +108,22 @@ router.get('/status', function(req, res) {
// Get the status of all tasks with by id // Get the status of all tasks with by id
router.get('/status/:id', function(req, res) { router.get('/status/:id', function(req, res) {
res.status(statusCodes.OK).send({ const status = calcQueueSvc.getStatus(req.params.id, null)
'id': req.params.id, if (status.length == 0) {
'status': processStatus(req.params.id, null) res.status(statusCodes.BAD_REQUEST).send()
}) } else {
res.status(statusCodes.OK).send(status)
}
}) })
// Get the status of an individual task // Get the status of an individual task
router.get('/status/:id/:calcFn', function(req, res) { router.get('/status/:id/:calcFn', function(req, res) {
res.status(statusCodes.OK).send({ const status = calcQueueSvc.getStatus(req.params.id, req.params.calcFn)
'id': req.params.id, if (status.length == 0) {
'calcFn': req.params.calcFn, res.status(statusCodes.BAD_REQUEST).send()
'status': processStatus(req.params.id, req.params.calcFn) } else {
}) res.status(statusCodes.OK).send(status)
}
}) })
module.exports = router module.exports = router

View File

@@ -92,23 +92,76 @@ CalcQueue.prototype = {
// Report if a task or collection of tasks by id are finished // Report if a task or collection of tasks by id are finished
isFinished: function(id, calcFn = null) { isFinished: function(id, calcFn = null) {
if (calcFn == null) { const idx = this.finished.findIndex((el) => {
const fin = this.finished.filter((el) => { return el.id() == id && (calcFn == null || el.calcFn() == calcFn)
return el.id() == id })
}).reduce((agg, el) => { return idx != -1
return agg && el.result() != null
}, true)
return fin
} else {
const idx = this.finished.findIndex((el) => {
return el.id() == id && (calcFn == null || el.calcFn() == calcFn)
})
return idx != -1
}
}, },
// Get the list of finished tasks // Get the list of finished tasks
getFinished: function() { getFinished: function() {
return this.finished.map((q) => { return this.finished.map((q) => {
return {
'id': q.id(),
'calcFn': q.calcFn(),
'result': q.result(),
'error': (
q.error() == null
? null
: q.error().name + ': ' + q.error().message
)
}
})
},
// Report if a task or collection of tasks by id are in progress
isInProgress: function(id, calcFn = null) {
const idx = this.inProgress.findIndex((el) => {
return el.id() == id && (calcFn == null || el.calcFn() == calcFn)
})
return idx != -1
},
// Get the list of in-progress tasks
getInProgress: function() {
return this.inProgress.map((q) => {
return { 'id': q.id(), 'calcFn': q.calcFn() }
})
},
// Get the process result of a single task or collection of tasks by id
retCode: function(id, calcFn = null) {
const finishedTasks = this.finished.filter((el) => {
return el.id() == id && (calcFn == null || el.calcFn() == calcFn)
})
if (finishedTasks.length == 0) {
return null;
}
const res = finishedTasks.map((t) => {
return {'id': t.id(), 'result': t.result(), 'calcFn': t.calcFn()}
})
if (res.length == 1 || calcFn != null) {
return res[res.length - 1].result
}
return res
},
// Get the status of a task
getStatus: function(id, calcFn = null) {
const queued = this.queue.filter((el) => {
return el.id() == id && (calcFn == null || el.calcFn() == calcFn)
})
const inProg = this.inProgress.filter((el) => {
return el.id() == id && (calcFn == null || el.calcFn() == calcFn)
})
const fin = this.finished.filter((el) => {
return el.id() == id && (calcFn == null || el.calcFn() == calcFn)
})
const matches = [].concat(queued, inProg, fin)
return matches.map((q) => {
return { return {
'id': q.id(), 'id': q.id(),
'calcFn': q.calcFn(), 'calcFn': q.calcFn(),
@@ -130,20 +183,6 @@ CalcQueue.prototype = {
}) })
}, },
// Report if a task or collection of tasks by id are in progress
isInProgress: function(id, calcFn = null) {
const idx = this.inProgress.findIndex((el) => {
return el.id() == id && (calcFn == null || el.calcFn() == calcFn)
})
return idx != -1
},
// Get the list of in-progress tasks
getInProgress: function() {
return this.inProgress.map((q) => {
return { 'id': q.id(), 'calcFn': q.calcFn() }
})
},
// Report if a task or collection of tasks by id are queued // Report if a task or collection of tasks by id are queued
isQueued: function(id, calcFn = null) { isQueued: function(id, calcFn = null) {
return (this.queuePosition(id, calcFn) != -1) return (this.queuePosition(id, calcFn) != -1)