apps: operators can now view backup logs and manage the backup task
we spun off the app backup as a separate task and this is not tracked by app.taskId . fixes #856
This commit is contained in:
@@ -13,6 +13,7 @@ import { HttpError } from '@cloudron/connect-lastmile';
|
||||
import { HttpSuccess } from '@cloudron/connect-lastmile';
|
||||
import metrics from '../metrics.js';
|
||||
import safe from 'safetydance';
|
||||
import tasks from '../tasks.js';
|
||||
import updater from '../updater.js';
|
||||
import users from '../users.js';
|
||||
import { getImageContentType } from '../image-content-type.js';
|
||||
@@ -1077,6 +1078,111 @@ async function getMetricStream(req, res, next) {
|
||||
metricStream.on('error', res.end.bind(res, null));
|
||||
}
|
||||
|
||||
function isAppTask(task, appId) {
|
||||
if (task.type === tasks.TASK_APP_BACKUP_PREFIX + appId) return true;
|
||||
if (task.type === tasks.TASK_APP && task.args[0] === appId) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
async function loadTask(req, res, next) {
|
||||
assert.strictEqual(typeof req.params.taskId, 'string');
|
||||
assert.strictEqual(typeof req.resources.app, 'object');
|
||||
|
||||
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'));
|
||||
if (!isAppTask(result, req.resources.app.id)) return next(new HttpError(404, 'Task not found'));
|
||||
|
||||
req.resources.task = result;
|
||||
|
||||
next();
|
||||
}
|
||||
|
||||
async function listAppTasks(req, res, next) {
|
||||
assert.strictEqual(typeof req.resources.app, 'object');
|
||||
|
||||
const [error, result] = await safe(tasks.list(1, 25, { prefix: tasks.TASK_APP_BACKUP_PREFIX + req.resources.app.id }));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(200, { tasks: result.map(tasks.removePrivateFields) }));
|
||||
}
|
||||
|
||||
async function getAppTask(req, res, next) {
|
||||
assert.strictEqual(typeof req.resources.task, 'object');
|
||||
|
||||
next(new HttpSuccess(200, tasks.removePrivateFields(req.resources.task)));
|
||||
}
|
||||
|
||||
async function getAppTaskLogs(req, res, next) {
|
||||
assert.strictEqual(typeof req.resources.task, 'object');
|
||||
|
||||
const lines = typeof req.query.lines === 'string' ? parseInt(req.query.lines, 10) : 10;
|
||||
if (isNaN(lines)) return next(new HttpError(400, 'lines must be a number'));
|
||||
|
||||
const options = {
|
||||
lines: lines,
|
||||
follow: false,
|
||||
format: typeof req.query.format === 'string' ? req.query.format : 'json'
|
||||
};
|
||||
|
||||
const [error, logStream] = await safe(tasks.getLogs(req.resources.task, options));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'application/x-logs',
|
||||
'Content-Disposition': `attachment; filename="task-${req.resources.task.id}.log"`,
|
||||
'Cache-Control': 'no-cache',
|
||||
'X-Accel-Buffering': 'no'
|
||||
});
|
||||
res.on('close', () => logStream.destroy());
|
||||
logStream.pipe(res);
|
||||
}
|
||||
|
||||
async function getAppTaskLogStream(req, res, next) {
|
||||
assert.strictEqual(typeof req.resources.task, 'object');
|
||||
|
||||
const lines = typeof req.query.lines === 'string' ? parseInt(req.query.lines, 10) : 10;
|
||||
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'));
|
||||
|
||||
const options = {
|
||||
lines: lines,
|
||||
follow: true,
|
||||
format: 'json'
|
||||
};
|
||||
|
||||
const [error, logStream] = await safe(tasks.getLogs(req.resources.task, options));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'text/event-stream',
|
||||
'Cache-Control': 'no-cache',
|
||||
'Connection': 'keep-alive',
|
||||
'X-Accel-Buffering': 'no',
|
||||
'Access-Control-Allow-Origin': '*'
|
||||
});
|
||||
res.write('retry: 3000\n');
|
||||
res.on('close', () => logStream.destroy());
|
||||
logStream.on('data', function (data) {
|
||||
const obj = JSON.parse(data);
|
||||
res.write(sse(obj.realtimeTimestamp, JSON.stringify(obj)));
|
||||
});
|
||||
logStream.on('end', res.end.bind(res));
|
||||
logStream.on('error', res.end.bind(res, null));
|
||||
}
|
||||
|
||||
async function stopAppTask(req, res, next) {
|
||||
assert.strictEqual(typeof req.resources.task, 'object');
|
||||
|
||||
const [error] = await safe(tasks.stopTask(req.resources.task.id));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(204, {}));
|
||||
}
|
||||
|
||||
export default {
|
||||
getApp,
|
||||
listByUser,
|
||||
@@ -1146,5 +1252,12 @@ export default {
|
||||
getMetrics,
|
||||
getMetricStream,
|
||||
|
||||
loadTask,
|
||||
listAppTasks,
|
||||
getAppTask,
|
||||
getAppTaskLogs,
|
||||
getAppTaskLogStream,
|
||||
stopAppTask,
|
||||
|
||||
load
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user