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
+25 -14
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',
+4 -4
View File
@@ -133,10 +133,10 @@ async function initializeExpressSync() {
// task routes
router.get ('/api/v1/tasks', token, authorizeAdmin, routes.tasks.list);
router.get ('/api/v1/tasks/:taskId', token, authorizeAdmin, routes.tasks.get);
router.get ('/api/v1/tasks/:taskId/logs', token, authorizeAdmin, routes.tasks.getLogs);
router.get ('/api/v1/tasks/:taskId/logstream', token, authorizeAdmin, routes.tasks.getLogStream);
router.post('/api/v1/tasks/:taskId/stop', json, token, authorizeAdmin, routes.tasks.stopTask);
router.get ('/api/v1/tasks/:taskId', token, authorizeAdmin, routes.tasks.load, routes.tasks.get);
router.get ('/api/v1/tasks/:taskId/logs', token, authorizeAdmin, routes.tasks.load, routes.tasks.getLogs);
router.get ('/api/v1/tasks/:taskId/logstream', token, authorizeAdmin, routes.tasks.load, routes.tasks.getLogStream);
router.post('/api/v1/tasks/:taskId/stop', json, token, authorizeAdmin, routes.tasks.load, routes.tasks.stopTask);
// notification routes (these are server level)
router.get ('/api/v1/notifications', token, authorizeAdmin, routes.notifications.list);
+7 -5
View File
@@ -258,18 +258,20 @@ async function listByTypePaged(type, page, perPage) {
return results;
}
function getLogs(taskId, options) {
assert.strictEqual(typeof taskId, 'string');
async function getLogs(task, options) {
assert.strictEqual(typeof task, 'object');
assert(options && typeof options === 'object');
assert.strictEqual(typeof options.lines, 'number');
assert.strictEqual(typeof options.format, 'string');
assert.strictEqual(typeof options.follow, 'boolean');
debug(`Getting logs for ${taskId}`);
const logFile = `${paths.TASKS_LOG_DIR}/${task.id}.log`;
const cp = logs.tail([`${paths.TASKS_LOG_DIR}/${taskId}.log`], { lines: options.lines, follow: options.follow });
const logStream = new logs.LogStream({ format: options.format || 'json', source: taskId });
if (!task.active && !safe.fs.existsSync(logFile)) throw new BoxError(BoxError.FS_ERROR, 'Log file removed/missing'); // logrotated
const cp = logs.tail([`${paths.TASKS_LOG_DIR}/${task.id}.log`], { lines: options.lines, follow: options.follow });
const logStream = new logs.LogStream({ format: options.format || 'json', source: task.id });
logStream.close = cp.kill.bind(cp, 'SIGKILL'); // hook for caller. closing stream kills the child process
cp.stdout.pipe(logStream);