tasks: add pending field
this indicates if a task is scheduled. previously, we relied on task.progress being 1
This commit is contained in:
+7
-10
@@ -61,7 +61,7 @@ let gTasks = {}; // indexed by task id
|
||||
const START_TASK_CMD = path.join(__dirname, 'scripts/starttask.sh');
|
||||
const STOP_TASK_CMD = path.join(__dirname, 'scripts/stoptask.sh');
|
||||
|
||||
const TASKS_FIELDS = [ 'id', 'type', 'argsJson', 'percent', 'message', 'errorJson', 'creationTime', 'resultJson', 'ts' ];
|
||||
const TASKS_FIELDS = [ 'id', 'type', 'argsJson', 'percent', 'pending', 'message', 'errorJson', 'creationTime', 'resultJson', 'ts' ];
|
||||
|
||||
function postProcess(task) {
|
||||
assert.strictEqual(typeof task, 'object');
|
||||
@@ -84,13 +84,8 @@ function postProcess(task) {
|
||||
function updateStatus(result) {
|
||||
assert.strictEqual(typeof result, 'object');
|
||||
|
||||
// running means actively running
|
||||
// pending means not actively running
|
||||
// active mean task is 'done' or not. at this point, clients can stop polling this task.
|
||||
// the apptaskmanager sets percent=1 when queued. just a hack to figure non-started but scheduled tasks
|
||||
result.running = !!gTasks[result.id];
|
||||
result.active = result.running || result.percent === 1;
|
||||
result.pending = !gTasks[result.id] && result.active;
|
||||
result.running = !!gTasks[result.id]; // running means actively running
|
||||
result.active = result.running || !!result.pending; // active mean task is 'done' or not. at this point, clients can stop polling this task.
|
||||
|
||||
// we rely on 'percent' to determine success. maybe this can become a db field
|
||||
result.success = result.percent === 100 && !result.error;
|
||||
@@ -157,7 +152,7 @@ async function add(type, args) {
|
||||
assert.strictEqual(typeof type, 'string');
|
||||
assert(Array.isArray(args));
|
||||
|
||||
const result = await database.query('INSERT INTO tasks (type, argsJson, percent, message) VALUES (?, ?, ?, ?)', [ type, JSON.stringify(args), 0, 'Queued' ]);
|
||||
const result = await database.query('INSERT INTO tasks (type, argsJson, percent, message, pending) VALUES (?, ?, ?, ?, ?)', [ type, JSON.stringify(args), 0, 'Queued', true ]);
|
||||
return String(result.insertId);
|
||||
}
|
||||
|
||||
@@ -221,6 +216,8 @@ function startTask(id, options, onTaskFinished) {
|
||||
if (error) debug(`startTask: error stopping task: ${error.message}`);
|
||||
}, options.timeout);
|
||||
}
|
||||
|
||||
update(id, { pending: false }); // fixme: make async
|
||||
}
|
||||
|
||||
async function stopTask(id) {
|
||||
@@ -288,7 +285,7 @@ async function getLogs(task, options) {
|
||||
|
||||
// removes all fields that are strictly private and should never be returned by API calls
|
||||
function removePrivateFields(task) {
|
||||
return _.pick(task, ['id', 'type', 'percent', 'message', 'error', 'active', 'pending', 'creationTime', 'result', 'ts', 'success']);
|
||||
return _.pick(task, ['id', 'type', 'percent', 'message', 'error', 'running', 'active', 'creationTime', 'result', 'ts', 'success']);
|
||||
}
|
||||
|
||||
async function del(id) {
|
||||
|
||||
Reference in New Issue
Block a user