diff --git a/routes/calc-queue.js b/routes/calc-queue.js index b9d0845..922f51a 100644 --- a/routes/calc-queue.js +++ b/routes/calc-queue.js @@ -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) { if (calcQueueSvc.isInProgress(id, calcFn)) { 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 router.get('/status', function(req, res) { res.status(statusCodes.OK).send({ @@ -71,19 +108,22 @@ router.get('/status', function(req, res) { // Get the status of all tasks with by id router.get('/status/:id', function(req, res) { - res.status(statusCodes.OK).send({ - 'id': req.params.id, - 'status': processStatus(req.params.id, null) - }) + const status = calcQueueSvc.getStatus(req.params.id, null) + if (status.length == 0) { + res.status(statusCodes.BAD_REQUEST).send() + } else { + res.status(statusCodes.OK).send(status) + } }) // Get the status of an individual task router.get('/status/:id/:calcFn', function(req, res) { - res.status(statusCodes.OK).send({ - 'id': req.params.id, - 'calcFn': req.params.calcFn, - 'status': processStatus(req.params.id, req.params.calcFn) - }) + const status = calcQueueSvc.getStatus(req.params.id, req.params.calcFn) + if (status.length == 0) { + res.status(statusCodes.BAD_REQUEST).send() + } else { + res.status(statusCodes.OK).send(status) + } }) module.exports = router diff --git a/src/services/calc-queue/CalcQueue.js b/src/services/calc-queue/CalcQueue.js index 66dfe66..fd103dc 100644 --- a/src/services/calc-queue/CalcQueue.js +++ b/src/services/calc-queue/CalcQueue.js @@ -92,23 +92,76 @@ CalcQueue.prototype = { // Report if a task or collection of tasks by id are finished isFinished: function(id, calcFn = null) { - if (calcFn == null) { - const fin = this.finished.filter((el) => { - return el.id() == id - }).reduce((agg, el) => { - 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 - } + const idx = this.finished.findIndex((el) => { + return el.id() == id && (calcFn == null || el.calcFn() == calcFn) + }) + return idx != -1 }, // Get the list of finished tasks getFinished: function() { 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 { 'id': q.id(), '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 isQueued: function(id, calcFn = null) { return (this.queuePosition(id, calcFn) != -1)