tasks: return 404 if task not found

part of #826
This commit is contained in:
Girish Ramakrishnan
2023-05-15 09:50:39 +02:00
parent 9036b272a8
commit 94eb7849fe
3 changed files with 36 additions and 23 deletions

View File

@@ -1,10 +1,12 @@
'use strict';
exports = module.exports = {
load,
get,
stopTask,
list,
stopTask,
getLogs,
getLogStream
};
@@ -16,23 +18,21 @@ const assert = require('assert'),
safe = require('safetydance'),
tasks = require('../tasks.js');
async function stopTask(req, res, next) {
async function load(req, res, next) {
assert.strictEqual(typeof req.params.taskId, 'string');
const [error] = await safe(tasks.stopTask(req.params.taskId));
const [error, result] = await safe(tasks.get(req.params.taskId));
if (error) return next(BoxError.toHttpError(error));
if (!result) return next(new HttpError(404, 'Task not found'));
next(new HttpSuccess(204, {}));
req.task = result;
next();
}
async function get(req, res, next) {
assert.strictEqual(typeof req.params.taskId, 'string');
const [error, task] = await safe(tasks.get(req.params.taskId));
if (error) return next(BoxError.toHttpError(error));
if (!task) return next(new HttpError(404, 'task not found'));
next(new HttpSuccess(200, tasks.removePrivateFields(task)));
next(new HttpSuccess(200, tasks.removePrivateFields(req.task)));
}
async function list(req, res, next) {
@@ -52,8 +52,17 @@ async function list(req, res, next) {
next(new HttpSuccess(200, { tasks: result }));
}
async function stopTask(req, res, next) {
assert.strictEqual(typeof req.task, 'object');
const [error] = await safe(tasks.stopTask(req.task.id));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(204, {}));
}
async function getLogs(req, res, next) {
assert.strictEqual(typeof req.params.taskId, 'string');
assert.strictEqual(typeof req.task, 'object');
const lines = 'lines' in req.query ? parseInt(req.query.lines, 10) : 10; // we ignore last-event-id
if (isNaN(lines)) return next(new HttpError(400, 'lines must be a number'));
@@ -64,11 +73,12 @@ async function getLogs(req, res, next) {
format: req.query.format || 'json'
};
const logStream = tasks.getLogs(req.params.taskId, options);
const [error, logStream] = await safe(tasks.getLogs(req.task, options));
if (error) return next(BoxError.toHttpError(error));
res.writeHead(200, {
'Content-Type': 'application/x-logs',
'Content-Disposition': `attachment; filename="task-${req.params.taskId}.log"`,
'Content-Disposition': `attachment; filename="task-${req.task.id}.log"`,
'Cache-Control': 'no-cache',
'X-Accel-Buffering': 'no' // disable nginx buffering
});
@@ -77,7 +87,7 @@ async function getLogs(req, res, next) {
// this route is for streaming logs
async function getLogStream(req, res, next) {
assert.strictEqual(typeof req.params.taskId, 'string');
assert.strictEqual(typeof req.task, 'object');
const lines = 'lines' in req.query ? parseInt(req.query.lines, 10) : 10; // we ignore last-event-id
if (isNaN(lines)) return next(new HttpError(400, 'lines must be a valid number'));
@@ -92,7 +102,8 @@ async function getLogStream(req, res, next) {
format: 'json'
};
const logStream = tasks.getLogs(req.params.taskId, options);
const [error, logStream] = await safe(tasks.getLogs(req.task, options));
if (error) return next(BoxError.toHttpError(error));
res.writeHead(200, {
'Content-Type': 'text/event-stream',