diff --git a/src/taskworker.js b/src/taskworker.js index 01d85b145..e69fbd2b0 100755 --- a/src/taskworker.js +++ b/src/taskworker.js @@ -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 } }