Add tasks table and API

progress will be tracked with this table instead of being in-process
like progress.js
This commit is contained in:
Girish Ramakrishnan
2018-11-16 11:13:03 -08:00
parent 390e69c01c
commit 218739a6b5
11 changed files with 259 additions and 52 deletions
+1 -11
View File
@@ -2,8 +2,7 @@
exports = module.exports = {
list: list,
startBackup: startBackup,
stopBackup: stopBackup
startBackup: startBackup
};
var backupdb = require('../backupdb.js'),
@@ -42,12 +41,3 @@ function startBackup(req, res, next) {
next(new HttpSuccess(202, {}));
});
}
function stopBackup(req, res, next) {
backups.stopBackupTask(auditSource(req), function (error) {
if (error && error.reason === BackupsError.BAD_STATE) return next(new HttpError(409, error.message));
if (error) return next(new HttpError(500, error));
next(new HttpSuccess(202, {}));
});
}
+1
View File
@@ -19,5 +19,6 @@ exports = module.exports = {
sysadmin: require('./sysadmin.js'),
settings: require('./settings.js'),
ssh: require('./ssh.js'),
tasks: require('./tasks.js'),
users: require('./users.js')
};
+39
View File
@@ -0,0 +1,39 @@
'use strict';
exports = module.exports = {
getProgress: getProgress,
stopTask: stopTask
};
let assert = require('assert'),
HttpError = require('connect-lastmile').HttpError,
HttpSuccess = require('connect-lastmile').HttpSuccess,
TaskError = require('../tasks.js').TaskError,
tasks = require('../tasks.js');
function auditSource(req) {
var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress || null;
return { ip: ip, username: req.user ? req.user.username : null, userId: req.user ? req.user.id : null };
}
function stopTask(req, res, next) {
assert.strictEqual(typeof req.params.taskId, 'string');
tasks.stopTask(req.params.taskId, auditSource(req), function (error) {
if (error && error.reason === TaskError.NOT_FOUND) return next(new HttpError(404, 'No such task'));
if (error) return next(new HttpError(500, error));
next(new HttpSuccess(204, {}));
});
}
function getProgress(req, res, next) {
assert.strictEqual(typeof req.params.taskId, 'string');
tasks.getProgress(req.params.taskId, function (error, progress) {
if (error && error.reason === TaskError.NOT_FOUND) return next(new HttpError(404, 'No such task'));
if (error) return next(new HttpError(500, error));
next(new HttpSuccess(200, progress));
});
}
+3 -3
View File
@@ -84,7 +84,7 @@ describe('Backups API', function () {
describe('create', function () {
it('fails due to mising token', function (done) {
superagent.post(SERVER_URL + '/api/v1/cloudron/start_backup')
superagent.post(SERVER_URL + '/api/v1/backups')
.end(function (error, result) {
expect(result.statusCode).to.equal(401);
done();
@@ -92,7 +92,7 @@ describe('Backups API', function () {
});
it('fails due to wrong token', function (done) {
superagent.post(SERVER_URL + '/api/v1/cloudron/start_backup')
superagent.post(SERVER_URL + '/api/v1/backups')
.query({ access_token: token.toUpperCase() })
.end(function (error, result) {
expect(result.statusCode).to.equal(401);
@@ -101,7 +101,7 @@ describe('Backups API', function () {
});
it('succeeds', function (done) {
superagent.post(SERVER_URL + '/api/v1/cloudron/start_backup')
superagent.post(SERVER_URL + '/api/v1/backups')
.query({ access_token: token })
.end(function (error, result) {
expect(result.statusCode).to.equal(202);