taskworker: fix programming error

This commit is contained in:
Girish Ramakrishnan
2024-11-01 16:15:28 +01:00
parent 620ad13427
commit fc2786b07f

View File

@@ -79,42 +79,40 @@ async function main() {
await setupNetworking();
await database.initialize();
} catch (initError) {
if (initError) {
console.error(initError);
return process.exit(50);
}
console.error(initError);
return process.exit(50);
}
const debug = require('debug')('box:taskworker'); // require this here so that logging handler is already setup
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
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 }));
// 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}`);
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 });
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 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
};
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`);
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
}
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
}
}