72 lines
2.4 KiB
JavaScript
Executable File
72 lines
2.4 KiB
JavaScript
Executable File
'use strict';
|
|
|
|
require('supererror')({ splatchError: true });
|
|
|
|
var apptask = require('./apptask.js'),
|
|
assert = require('assert'),
|
|
async = require('async'),
|
|
backups = require('./backups.js'),
|
|
database = require('./database.js'),
|
|
debug = require('debug')('box:taskworker'),
|
|
domains = require('./domains.js'),
|
|
externalLdap = require('./externalldap.js'),
|
|
reverseProxy = require('./reverseproxy.js'),
|
|
settings = require('./settings.js'),
|
|
tasks = require('./tasks.js'),
|
|
updater = require('./updater.js');
|
|
|
|
const NOOP_CALLBACK = function (error) { if (error) debug(error); };
|
|
|
|
const TASKS = { // indexed by task type
|
|
app: apptask.run,
|
|
backup: backups.backupBoxAndApps,
|
|
update: updater.update,
|
|
renewcerts: reverseProxy.renewCerts,
|
|
prepareDashboardDomain: domains.prepareDashboardDomain,
|
|
cleanBackups: backups.cleanup,
|
|
syncExternalLdap: externalLdap.sync,
|
|
|
|
_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}`); },
|
|
_sleep: (arg) => setTimeout(process.exit, arg)
|
|
};
|
|
|
|
process.on('SIGTERM', function () {
|
|
process.exit(0);
|
|
});
|
|
|
|
assert.strictEqual(process.argv.length, 3, 'Pass the taskid as argument');
|
|
const taskId = process.argv[2];
|
|
|
|
function initialize(callback) {
|
|
async.series([
|
|
database.initialize,
|
|
settings.initCache
|
|
], callback);
|
|
}
|
|
|
|
// Main process starts here
|
|
debug(`Starting task ${taskId}`);
|
|
|
|
initialize(function (error) {
|
|
if (error) return process.exit(50);
|
|
|
|
tasks.get(taskId, function (error, task) {
|
|
if (error) return process.exit(50);
|
|
|
|
const progressCallback = (progress, cb) => tasks.update(taskId, progress, cb || NOOP_CALLBACK);
|
|
const resultCallback = (error, result) => {
|
|
// Error object has properties with enumerable: false (https://mattcbaker.com/posts/stringify-javascript-error/)
|
|
const progress = {
|
|
result: result || null,
|
|
error: error ? JSON.parse(JSON.stringify(error, Object.getOwnPropertyNames(error))) : null
|
|
};
|
|
|
|
tasks.setCompleted(taskId, progress, () => process.exit(error ? 50 : 0));
|
|
};
|
|
|
|
TASKS[task.type].apply(null, task.args.concat(progressCallback).concat(resultCallback));
|
|
});
|
|
});
|