Files
cloudron-box/src/routes/tasks.js

113 lines
3.8 KiB
JavaScript
Raw Normal View History

'use strict';
exports = module.exports = {
2021-05-01 11:21:09 -07:00
get,
stopTask,
list,
2018-12-08 21:31:55 -08:00
2021-05-01 11:21:09 -07:00
getLogs,
getLogStream
};
2021-07-12 23:35:30 -07:00
const assert = require('assert'),
2019-10-22 20:12:44 -07:00
BoxError = require('../boxerror.js'),
HttpError = require('connect-lastmile').HttpError,
HttpSuccess = require('connect-lastmile').HttpSuccess,
2021-07-12 23:35:30 -07:00
safe = require('safetydance'),
tasks = require('../tasks.js');
2021-07-12 23:35:30 -07:00
async function stopTask(req, res, next) {
assert.strictEqual(typeof req.params.taskId, 'string');
2021-07-12 23:35:30 -07:00
const [error] = await safe(tasks.stopTask(req.params.taskId));
if (error) return next(BoxError.toHttpError(error));
2021-07-12 23:35:30 -07:00
next(new HttpSuccess(204, {}));
}
2021-07-12 23:35:30 -07:00
async function get(req, res, next) {
assert.strictEqual(typeof req.params.taskId, 'string');
2021-07-12 23:35:30 -07:00
const [error, task] = await safe(tasks.get(req.params.taskId));
if (error) return next(BoxError.toHttpError(error));
2021-07-14 10:59:49 -07:00
if (!task) return next(new HttpError(404, 'task not found'));
2021-07-12 23:35:30 -07:00
next(new HttpSuccess(200, tasks.removePrivateFields(task)));
2018-12-08 20:12:23 -08:00
}
2021-07-12 23:35:30 -07:00
async function list(req, res, next) {
const page = typeof req.query.page !== 'undefined' ? parseInt(req.query.page) : 1;
2018-12-08 20:12:23 -08:00
if (!page || page < 0) return next(new HttpError(400, 'page query param has to be a postive number'));
2021-07-12 23:35:30 -07:00
const perPage = typeof req.query.per_page !== 'undefined'? parseInt(req.query.per_page) : 25;
2018-12-08 20:12:23 -08:00
if (!perPage || perPage < 0) return next(new HttpError(400, 'per_page query param has to be a postive number'));
if (req.query.type && typeof req.query.type !== 'string') return next(new HttpError(400, 'type must be a string'));
2021-07-12 23:35:30 -07:00
let [error, result] = await safe(tasks.listByTypePaged(req.query.type || null, page, perPage));
if (error) return next(BoxError.toHttpError(error));
2018-12-08 20:12:23 -08:00
2021-07-12 23:35:30 -07:00
result = result.map(tasks.removePrivateFields);
2021-07-12 23:35:30 -07:00
next(new HttpSuccess(200, { tasks: result }));
}
2018-12-08 21:31:55 -08:00
2021-07-12 23:35:30 -07:00
async function getLogs(req, res, next) {
2018-12-08 21:31:55 -08:00
assert.strictEqual(typeof req.params.taskId, 'string');
2021-07-12 23:35:30 -07:00
const lines = 'lines' in req.query ? parseInt(req.query.lines, 10) : 10; // we ignore last-event-id
2018-12-08 21:31:55 -08:00
if (isNaN(lines)) return next(new HttpError(400, 'lines must be a number'));
2021-07-12 23:35:30 -07:00
const options = {
2018-12-08 21:31:55 -08:00
lines: lines,
follow: false,
format: req.query.format || 'json'
2018-12-08 21:31:55 -08:00
};
2021-07-12 23:35:30 -07:00
const logStream = tasks.getLogs(req.params.taskId, options);
2018-12-08 21:31:55 -08:00
2021-07-12 23:35:30 -07:00
res.writeHead(200, {
'Content-Type': 'application/x-logs',
'Content-Disposition': `attachment; filename="task-${req.params.taskId}.log"`,
'Cache-Control': 'no-cache',
'X-Accel-Buffering': 'no' // disable nginx buffering
2018-12-08 21:31:55 -08:00
});
2021-07-12 23:35:30 -07:00
logStream.pipe(res);
2018-12-08 21:31:55 -08:00
}
// this route is for streaming logs
2021-07-12 23:35:30 -07:00
async function getLogStream(req, res, next) {
2018-12-08 21:31:55 -08:00
assert.strictEqual(typeof req.params.taskId, 'string');
2021-07-12 23:35:30 -07:00
const lines = 'lines' in req.query ? parseInt(req.query.lines, 10) : 10; // we ignore last-event-id
2018-12-08 21:31:55 -08:00
if (isNaN(lines)) return next(new HttpError(400, 'lines must be a valid number'));
function sse(id, data) { return 'id: ' + id + '\ndata: ' + data + '\n\n'; }
if (req.headers.accept !== 'text/event-stream') return next(new HttpError(400, 'This API call requires EventStream'));
2021-07-12 23:35:30 -07:00
const options = {
2018-12-08 21:31:55 -08:00
lines: lines,
follow: true,
format: 'json'
2018-12-08 21:31:55 -08:00
};
2021-07-12 23:35:30 -07:00
const logStream = tasks.getLogs(req.params.taskId, options);
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'X-Accel-Buffering': 'no', // disable nginx buffering
'Access-Control-Allow-Origin': '*'
});
res.write('retry: 3000\n');
res.on('close', logStream.close);
logStream.on('data', function (data) {
2022-04-14 17:41:41 -05:00
const obj = JSON.parse(data);
2021-07-12 23:35:30 -07:00
res.write(sse(obj.monotonicTimestamp, JSON.stringify(obj))); // send timestamp as id
2018-12-08 21:31:55 -08:00
});
2021-07-12 23:35:30 -07:00
logStream.on('end', res.end.bind(res));
logStream.on('error', res.end.bind(res, null));
2018-12-08 21:31:55 -08:00
}