taskworker: refactor

This commit is contained in:
Girish Ramakrishnan
2024-10-31 09:46:36 +01:00
parent aefa481c43
commit 03e17aea22
+42 -39
View File
@@ -3,7 +3,6 @@
'use strict';
const apptask = require('./apptask.js'),
async = require('async'),
backupCleaner = require('./backupcleaner.js'),
backuptask = require('./backuptask.js'),
dashboard = require('./dashboard.js'),
@@ -74,45 +73,49 @@ function exitSync(status) {
// Main process starts here
const startTime = new Date();
async.series([
setupLogging,
setupNetworking,
database.initialize,
], async function (initError) {
if (initError) {
console.error(initError);
return process.exit(50);
}
const debug = require('debug')('box:taskworker'); // require this here so that logging handler is already setup
process.on('SIGTERM', () => exitSync({ code: 0 })); // sent as timeout notification
// ensure we log task crashes with the task logs. neither console.log nor debug are sync for some reason
process.on('uncaughtException', (error) => exitSync({ error, code: 1 }));
debug(`Starting task ${taskId}. Logs are at ${logFile}`);
const [getError, task] = await safe(tasks.get(taskId));
if (getError) return exitSync({ error: getError, code: 50 });
if (!task) return exitSync({ error: new Error(`Task ${taskId} not found`), code: 50 });
async function progressCallback(progress) {
await safe(tasks.update(taskId, progress), { debug });
}
async function main() {
try {
const [runError, result] = await safe(TASKS[task.type].apply(null, task.args.concat(progressCallback)));
const progress = {
result: result || null,
error: runError ? JSON.parse(JSON.stringify(runError, Object.getOwnPropertyNames(runError))) : null
};
await setupLogging();
await setupNetworking();
await database.initialize();
} catch (initError) {
if (initError) {
console.error(initError);
return process.exit(50);
}
debug(`Task took ${(new Date() - startTime)/1000} seconds`);
const debug = require('debug')('box:taskworker'); // require this here so that logging handler is already setup
await safe(tasks.setCompleted(taskId, progress));
exitSync({ error: runError, code: runError ? 50 : 0 });
} catch (error) {
exitSync({ error, code: 1 }); // do not call setCompleted() intentionally. the task code must be resilient enough to handle it
process.on('SIGTERM', () => exitSync({ code: 0 })); // sent as timeout notification
// ensure we log task crashes with the task logs. neither console.log nor debug are sync for some reason
process.on('uncaughtException', (error) => exitSync({ error, code: 1 }));
debug(`Starting task ${taskId}. Logs are at ${logFile}`);
const [getError, task] = await safe(tasks.get(taskId));
if (getError) return exitSync({ error: getError, code: 50 });
if (!task) return exitSync({ error: new Error(`Task ${taskId} not found`), code: 50 });
async function progressCallback(progress) {
await safe(tasks.update(taskId, progress), { debug });
}
try {
const [runError, result] = await safe(TASKS[task.type].apply(null, task.args.concat(progressCallback)));
const progress = {
result: result || null,
error: runError ? JSON.parse(JSON.stringify(runError, Object.getOwnPropertyNames(runError))) : null
};
debug(`Task took ${(new Date() - startTime)/1000} seconds`);
await safe(tasks.setCompleted(taskId, progress));
exitSync({ error: runError, code: runError ? 50 : 0 });
} catch (error) {
exitSync({ error, code: 1 }); // do not call setCompleted() intentionally. the task code must be resilient enough to handle it
}
}
});
}
main();