diff --git a/src/taskworker.js b/src/taskworker.js index b7aec1b8b..ebf674d66 100755 --- a/src/taskworker.js +++ b/src/taskworker.js @@ -16,8 +16,7 @@ const apptask = require('./apptask.js'), safe = require('safetydance'), settings = require('./settings.js'), tasks = require('./tasks.js'), - updater = require('./updater.js'), - util = require('util'); + updater = require('./updater.js'); const TASKS = { // indexed by task type app: apptask.run, @@ -30,10 +29,10 @@ const TASKS = { // indexed by task type changeMailLocation: mail.changeLocation, syncDnsRecords: dns.syncDnsRecords, - _identity: (arg, progressCallback, callback) => callback(null, arg), - _error: (arg, progressCallback, callback) => callback(new Error(`Failed for arg: ${arg}`)), - _crash: (arg) => { throw new Error(`Crashing for arg: ${arg}`); }, // the test looks for this debug string in the log file - _sleep: (arg) => setTimeout(process.exit, arg) + _identity: async (arg, progressCallback) => { progressCallback(); return arg; }, + _error: async (arg, progressCallback) => { progressCallback(); throw new Error(`Failed for arg: ${arg}`); }, + _crash: async (arg) => { throw new Error(`Crashing for arg: ${arg}`); }, // the test looks for this debug string in the log file + _sleep: async (arg) => setTimeout(process.exit, arg) }; if (process.argv.length !== 4) { @@ -69,9 +68,9 @@ async.series([ setupLogging, database.initialize, settings.initCache -], async function (error) { - if (error) { - console.error(error); +], async function (initError) { + if (initError) { + console.error(initError); return process.exit(50); } @@ -88,31 +87,21 @@ async.series([ if (getError) return exitSync({ error: getError, code: 50 }); if (!task) return exitSync({ error: new Error(`Task ${taskId} not found`), code: 50 }); - const progressCallback = async function (progress, callback) { + async function progressCallback(progress) { await safe(tasks.update(taskId, progress), { debug }); - if (callback) callback(); - }; + } - const resultCallback = async (error, result) => { - // Error object has properties with enumerable: false (https://mattcbaker.com/posts/stringify-javascript-error/) + try { + const [runError, result] = await safe(TASKS[task.type].apply(null, task.args.concat(progressCallback))); const progress = { result: result || null, - error: error ? JSON.parse(JSON.stringify(error, Object.getOwnPropertyNames(error))) : 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, code: error ? 50 : 0 }); - }; - - try { - if (util.types.isAsyncFunction(TASKS[task.type])) { // can also use fn[Symbol.toStringTag] - const [error, result] = await safe(TASKS[task.type].apply(null, task.args.concat(progressCallback))); - await resultCallback(error, result); - } else { - TASKS[task.type].apply(null, task.args.concat(progressCallback).concat(resultCallback)); - } + 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 }