Add tasks.setCompleted

this lets us easily grep the code on where the task is completed
This commit is contained in:
Girish Ramakrishnan
2019-09-05 11:29:46 -07:00
parent 9769fbfcf2
commit 0843f78ec8
3 changed files with 34 additions and 18 deletions
+30 -1
View File
@@ -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