diff --git a/src/cloudron.js b/src/cloudron.js index 9253f2b2d..9ffbf0a6f 100644 --- a/src/cloudron.js +++ b/src/cloudron.js @@ -51,6 +51,7 @@ var apps = require('./apps.js'), split = require('split'), sysinfo = require('./sysinfo.js'), tasks = require('./tasks.js'), + TaskError = require('./tasks.js').TaskError, users = require('./users.js'), util = require('util'); @@ -112,19 +113,6 @@ function onActivated(callback) { ], callback); } -function setUpdateSuccess(callback) { - tasks.listByTypePaged(tasks.TASK_UPDATE, 1, 1, function (error, results) { - if (error) return callback(new CloudronError(CloudronError.INTERNAL_ERROR, error)); - if (results.length !== 1) return callback(); // when hotfixing - - tasks.update(results[0].id, { percent: 100, error: null }, function (error) { - if (error) return callback(new CloudronError(CloudronError.INTERNAL_ERROR, error)); - - callback(); - }); - }); -} - function notifyUpdate(callback) { assert.strictEqual(typeof callback, 'function'); @@ -134,8 +122,8 @@ function notifyUpdate(callback) { eventlog.add(eventlog.ACTION_UPDATE_FINISH, auditSource.CRON, { oldVersion: version || 'dev', newVersion: constants.VERSION }, function (error) { if (error) return callback(new CloudronError(CloudronError.INTERNAL_ERROR, error)); - setUpdateSuccess(function (error) { - if (error) return callback(error); + tasks.setCompletedByType(tasks.TASK_UPDATE, { error: null }, function (error) { + if (error && error.reason !== TaskError.NOT_FOUND) return callback(error); // when hotfixing, task may not exist safe.fs.writeFileSync(paths.VERSION_FILE, constants.VERSION, 'utf8'); diff --git a/src/tasks.js b/src/tasks.js index 8c624ab21..55c8120ad 100644 --- a/src/tasks.js +++ b/src/tasks.js @@ -4,6 +4,8 @@ exports = module.exports = { get: get, add: add, update: update, + setCompleted: setCompleted, + setCompletedByType: setCompletedByType, listByTypePaged: listByTypePaged, getLogs: getLogs, @@ -118,6 +120,33 @@ function update(id, task, callback) { }); } +function setCompleted(id, task, callback) { + assert.strictEqual(typeof id, 'string'); + assert.strictEqual(typeof task, 'object'); + assert.strictEqual(typeof callback, 'function'); + + debug(`setCompleted - ${id}: ${JSON.stringify(task)}`); + + update(id, _.extend({ progress: 100 }, task), callback); +} + +function setCompletedByType(type, task, callback) { + assert.strictEqual(typeof type, 'string'); + assert.strictEqual(typeof task, 'object'); + assert.strictEqual(typeof callback, 'function'); + + listByTypePaged(type, 1, 1, function (error, results) { + if (error) return callback(new TaskError(TaskError.INTERNAL_ERROR, error)); + if (results.length !== 1) return callback(new TaskError(TaskError.NOT_FOUND)); + + setCompleted(results[0].id, task, function (error) { + if (error) return callback(new TaskError(TaskError.INTERNAL_ERROR, error)); + + callback(); + }); + }); +} + function add(type, args, callback) { assert.strictEqual(typeof type, 'string'); assert(Array.isArray(args)); @@ -156,7 +185,7 @@ function startTask(taskId, options, callback) { code: code === 0 ? exports.ESTOPPED : exports.ECRASHED }; // note that despite the update() here, we should handle the case where the box code was restarted and never got taskworker exit - update(taskId, { percent: 100, error: taskError }, NOOP_CALLBACK); + setCompleted(taskId, { error: taskError }, NOOP_CALLBACK); } else if (!error && task.error) { taskError = task.error; } else if (!task) { // db got cleared in tests diff --git a/src/taskworker.js b/src/taskworker.js index 9ad4085a8..0af5a400b 100755 --- a/src/taskworker.js +++ b/src/taskworker.js @@ -59,12 +59,11 @@ initialize(function (error) { const resultCallback = (error, result) => { // Error object has properties with enumerable: false (https://mattcbaker.com/posts/stringify-javascript-error/) const progress = { - percent: 100, result: result || null, error: error ? JSON.parse(JSON.stringify(error, Object.getOwnPropertyNames(error))) : null }; - tasks.update(taskId, progress, () => process.exit(error ? 50 : 0)); + tasks.setCompleted(taskId, progress, () => process.exit(error ? 50 : 0)); }; TASKS[task.type].apply(null, task.args.concat(progressCallback).concat(resultCallback));