Files
cloudron-box/src/taskworker.js

72 lines
2.4 KiB
JavaScript
Raw Normal View History

'use strict';
require('supererror')({ splatchError: true });
2019-08-26 15:55:57 -07:00
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'),
2019-10-25 15:58:11 -07:00
externalLdap = require('./externalldap.js'),
2018-12-10 20:20:53 -08:00
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
2019-08-26 15:55:57 -07:00
app: apptask.run,
backup: backups.backupBoxAndApps,
2018-12-09 12:04:51 -08:00
update: updater.update,
2018-12-10 21:05:46 -08:00
renewcerts: reverseProxy.renewCerts,
prepareDashboardDomain: domains.prepareDashboardDomain,
2019-01-10 16:00:49 -08:00
cleanBackups: backups.cleanup,
2019-10-25 15:58:11 -07:00
syncExternalLdap: externalLdap.sync,
2018-12-10 21:05:46 -08:00
_identity: (arg, progressCallback, callback) => callback(null, arg),
_error: (arg, progressCallback, callback) => callback(new Error(`Failed for arg: ${arg}`)),
2018-12-10 21:42:03 -08:00
_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
2019-10-03 15:25:52 -07:00
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));
});
});