tasks: distinguish runtime crash vs task error in worker
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
const apptask = require('./apptask.js'),
|
||||
backupCleaner = require('./backupcleaner.js'),
|
||||
backuptask = require('./backuptask.js'),
|
||||
BoxError = require('./boxerror.js'),
|
||||
dashboard = require('./dashboard.js'),
|
||||
database = require('./database.js'),
|
||||
dns = require('./dns.js'),
|
||||
@@ -73,6 +74,15 @@ function exitSync(status) {
|
||||
process.exit(status.code);
|
||||
}
|
||||
|
||||
function toTaskError(runError) {
|
||||
if (runError instanceof BoxError) return runError.toPlainObject();
|
||||
return {
|
||||
message: `Task crashed. ${runError.message}`,
|
||||
stack: runError.stack,
|
||||
code: tasks.ECRASHED
|
||||
};
|
||||
}
|
||||
|
||||
// Main process starts here
|
||||
const startTime = new Date();
|
||||
|
||||
@@ -107,22 +117,18 @@ async function main() {
|
||||
await safe(tasks.update(taskId, progress), { debug });
|
||||
}
|
||||
|
||||
try {
|
||||
debug(`Running task of type ${task.type}`);
|
||||
const [runError, result] = await safe(TASKS[task.type.replace(/_.*/,'')].apply(null, task.args.concat(progressCallback)));
|
||||
const progress = {
|
||||
result: result || null,
|
||||
error: runError ? JSON.parse(JSON.stringify(runError, Object.getOwnPropertyNames(runError))) : null,
|
||||
percent: 100
|
||||
};
|
||||
debug(`Running task of type ${task.type}`);
|
||||
const [runError, result] = await safe(TASKS[task.type.replace(/_.*/,'')].apply(null, task.args.concat(progressCallback)));
|
||||
const progress = {
|
||||
result: result || null,
|
||||
error: runError ? toTaskError(runError) : null,
|
||||
percent: 100
|
||||
};
|
||||
|
||||
debug(`Task took ${(new Date() - startTime)/1000} seconds`);
|
||||
debug(`Task took ${(new Date() - startTime)/1000} seconds`);
|
||||
|
||||
await safe(tasks.setCompleted(taskId, progress));
|
||||
exitSync({ error: runError, code: 0 }); // code itself ran fine, but resulted in some error. so exit with success
|
||||
} catch (error) {
|
||||
exitSync({ error, code: 50 }); // do not call setCompleted() intentionally. the task code must be resilient enough to handle it
|
||||
}
|
||||
await safe(tasks.setCompleted(taskId, progress), { debug });
|
||||
exitSync({ error: runError, code: runError instanceof BoxError ? 0 : 50 }); // handled error vs run time crash
|
||||
}
|
||||
|
||||
main();
|
||||
|
||||
Reference in New Issue
Block a user