'use strict'; require('supererror')({ splatchError: true }); var assert = require('assert'), backups = require('./backups.js'), database = require('./database.js'), debug = require('debug')('box:taskworker'), tasks = require('./tasks.js'), updater = require('./updater.js'); const NOOP_CALLBACK = function (error) { if (error) debug(error); }; const TASKS = { // indexed by task type backup: backups.backupBoxAndApps, updater: updater.update, }; process.on('SIGTERM', function () { process.exit(0); }); assert.strictEqual(process.argv.length, 3, 'Pass the taskid as argument'); const taskId = process.argv[2]; // Main process starts here debug(`Staring task ${taskId}`); database.initialize(function (error) { if (error) return process.exit(50); tasks.get(taskId, function (error, task) { if (error) return process.exit(50); const progressCallback = (progress) => tasks.update(taskId, progress, NOOP_CALLBACK); const resultCallback = (error, result) => { const progress = { percent: 100, result: result || '', errorMessage: error ? error.message : '' }; tasks.update(taskId, progress, () => process.exit(error ? 50 : 0)); }; TASKS[task.type].apply(null, task.args.concat(progressCallback).concat(resultCallback)); }); });