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

122 lines
4.2 KiB
JavaScript
Raw Normal View History

import assert from 'node:assert';
import BoxError from '../boxerror.js';
import { HttpError } from '@cloudron/connect-lastmile';
import { HttpSuccess } from '@cloudron/connect-lastmile';
import safe from 'safetydance';
import tasks from '../tasks.js';
export {
2023-05-15 09:50:39 +02:00
load,
2021-05-01 11:21:09 -07:00
get,
list,
2018-12-08 21:31:55 -08:00
2023-05-15 09:50:39 +02:00
stopTask,
2021-05-01 11:21:09 -07:00
getLogs,
getLogStream
2018-11-16 11:13:03 -08:00
};
2023-05-15 09:50:39 +02:00
async function load(req, res, next) {
2018-11-16 11:13:03 -08:00
assert.strictEqual(typeof req.params.taskId, 'string');
2023-05-15 09:50:39 +02:00
const [error, result] = await safe(tasks.get(req.params.taskId));
2021-07-12 23:35:30 -07:00
if (error) return next(BoxError.toHttpError(error));
2023-05-15 09:50:39 +02:00
if (!result) return next(new HttpError(404, 'Task not found'));
2018-11-16 11:13:03 -08:00
req.resources.task = result;
2023-05-15 09:50:39 +02:00
next();
2018-11-16 11:13:03 -08:00
}
2021-07-12 23:35:30 -07:00
async function get(req, res, next) {
2018-11-16 11:13:03 -08:00
assert.strictEqual(typeof req.params.taskId, 'string');
next(new HttpSuccess(200, tasks.removePrivateFields(req.resources.task)));
2018-12-08 20:12:23 -08:00
}
2021-07-12 23:35:30 -07:00
async function list(req, res, next) {
2025-07-13 13:14:32 +02:00
const page = typeof req.query.page === 'string' ? 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'));
2025-07-13 13:14:32 +02:00
const perPage = typeof req.query.per_page === 'string'? 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 ('type' in req.query && typeof req.query.type !== 'string') return next(new HttpError(400, 'type must be a string'));
2018-12-08 20:12:23 -08:00
const [error, result] = await safe(tasks.list(page, perPage, { type: req.query.type || null }));
2021-07-12 23:35:30 -07:00
if (error) return next(BoxError.toHttpError(error));
2018-12-08 20:12:23 -08:00
2025-06-08 15:54:26 +02:00
next(new HttpSuccess(200, { tasks: result.map(tasks.removePrivateFields) }));
2018-11-16 11:13:03 -08:00
}
2018-12-08 21:31:55 -08:00
2023-05-15 09:50:39 +02:00
async function stopTask(req, res, next) {
assert.strictEqual(typeof req.resources.task, 'object');
2023-05-15 09:50:39 +02:00
const [error] = await safe(tasks.stopTask(req.resources.task.id));
2023-05-15 09:50:39 +02:00
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(204, {}));
}
2021-07-12 23:35:30 -07:00
async function getLogs(req, res, next) {
assert.strictEqual(typeof req.resources.task, 'object');
2018-12-08 21:31:55 -08:00
2025-07-13 13:14:32 +02:00
const lines = typeof req.query.lines === 'string' ? 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,
2025-07-13 13:14:32 +02:00
format: typeof req.query.format === 'string' ? req.query.format : 'json'
2018-12-08 21:31:55 -08:00
};
const [error, logStream] = await safe(tasks.getLogs(req.resources.task, options));
2023-05-15 09:50:39 +02:00
if (error) return next(BoxError.toHttpError(error));
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.resources.task.id}.log"`,
2021-07-12 23:35:30 -07:00
'Cache-Control': 'no-cache',
'X-Accel-Buffering': 'no' // disable nginx buffering
2018-12-08 21:31:55 -08:00
});
res.on('close', () => logStream.destroy());
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) {
assert.strictEqual(typeof req.resources.task, 'object');
2018-12-08 21:31:55 -08:00
2025-07-13 13:14:32 +02:00
const lines = typeof req.query.lines === 'string' ? 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,
2019-01-08 12:10:53 -08:00
follow: true,
format: 'json'
2018-12-08 21:31:55 -08:00
};
const [error, logStream] = await safe(tasks.getLogs(req.resources.task, options));
2023-05-15 09:50:39 +02:00
if (error) return next(BoxError.toHttpError(error));
2021-07-12 23:35:30 -07:00
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.destroy());
2021-07-12 23:35:30 -07:00
logStream.on('data', function (data) {
2022-04-14 17:41:41 -05:00
const obj = JSON.parse(data);
2022-11-06 16:02:46 +01:00
res.write(sse(obj.realtimeTimestamp, 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
}