@@ -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',
|
||||
|
||||
Reference in New Issue
Block a user