2021-05-14 10:51:37 +02:00
|
|
|
#!/usr/bin/env node
|
2020-08-06 14:36:25 -07:00
|
|
|
|
2018-12-09 03:20:00 -08:00
|
|
|
'use strict';
|
|
|
|
|
|
2021-05-18 13:28:48 -07:00
|
|
|
const apptask = require('./apptask.js'),
|
2019-07-26 17:11:33 -07:00
|
|
|
async = require('async'),
|
2021-07-14 11:07:19 -07:00
|
|
|
backupCleaner = require('./backupcleaner.js'),
|
|
|
|
|
backuptask = require('./backuptask.js'),
|
2020-08-15 23:17:29 -07:00
|
|
|
cloudron = require('./cloudron.js'),
|
2018-12-09 03:20:00 -08:00
|
|
|
database = require('./database.js'),
|
2021-02-24 18:42:39 -08:00
|
|
|
domains = require('./domains.js'),
|
2019-10-25 15:58:11 -07:00
|
|
|
externalLdap = require('./externalldap.js'),
|
2020-08-04 22:16:38 -07:00
|
|
|
fs = require('fs'),
|
2020-08-15 23:17:47 -07:00
|
|
|
mail = require('./mail.js'),
|
2018-12-10 20:20:53 -08:00
|
|
|
reverseProxy = require('./reverseproxy.js'),
|
2021-07-12 23:35:30 -07:00
|
|
|
safe = require('safetydance'),
|
2019-07-26 17:11:33 -07:00
|
|
|
settings = require('./settings.js'),
|
2018-12-09 03:20:00 -08:00
|
|
|
tasks = require('./tasks.js'),
|
2021-05-14 10:39:05 +02:00
|
|
|
updater = require('./updater.js');
|
2018-12-09 03:20:00 -08:00
|
|
|
|
|
|
|
|
const TASKS = { // indexed by task type
|
2019-08-26 15:55:57 -07:00
|
|
|
app: apptask.run,
|
2021-07-14 11:07:19 -07:00
|
|
|
backup: backuptask.backupBoxAndApps,
|
2018-12-09 12:04:51 -08:00
|
|
|
update: updater.update,
|
2021-05-18 13:28:48 -07:00
|
|
|
checkCerts: reverseProxy.checkCerts,
|
2020-08-15 23:17:29 -07:00
|
|
|
setupDnsAndCert: cloudron.setupDnsAndCert,
|
2021-07-14 11:07:19 -07:00
|
|
|
cleanBackups: backupCleaner.run,
|
2019-10-25 15:58:11 -07:00
|
|
|
syncExternalLdap: externalLdap.sync,
|
2020-08-15 23:17:47 -07:00
|
|
|
changeMailLocation: mail.changeLocation,
|
2021-02-24 18:42:39 -08:00
|
|
|
syncDnsRecords: domains.syncDnsRecords,
|
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}`)),
|
2020-08-04 22:16:38 -07:00
|
|
|
_crash: (arg) => { throw new Error(`Crashing for arg: ${arg}`); }, // the test looks for this debug string in the log file
|
2018-12-10 21:42:03 -08:00
|
|
|
_sleep: (arg) => setTimeout(process.exit, arg)
|
2018-12-09 03:20:00 -08:00
|
|
|
};
|
|
|
|
|
|
2020-08-04 22:16:38 -07:00
|
|
|
if (process.argv.length !== 4) {
|
|
|
|
|
console.error('Pass the taskid and logfile as argument');
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
2018-12-09 03:20:00 -08:00
|
|
|
|
|
|
|
|
const taskId = process.argv[2];
|
2020-08-04 22:16:38 -07:00
|
|
|
const logFile = process.argv[3];
|
|
|
|
|
|
|
|
|
|
function setupLogging(callback) {
|
2021-05-13 22:11:23 -07:00
|
|
|
const logfileStream = fs.createWriteStream(logFile, { flags:'a' });
|
|
|
|
|
process.stdout.write = process.stderr.write = logfileStream.write.bind(logfileStream);
|
2020-08-04 22:16:38 -07:00
|
|
|
|
2021-05-13 22:11:23 -07:00
|
|
|
callback();
|
2019-07-26 17:11:33 -07:00
|
|
|
}
|
|
|
|
|
|
2018-12-09 03:20:00 -08:00
|
|
|
// Main process starts here
|
2020-07-31 12:59:15 -07:00
|
|
|
const startTime = new Date();
|
2018-12-09 03:20:00 -08:00
|
|
|
|
2020-08-04 22:16:38 -07:00
|
|
|
async.series([
|
|
|
|
|
setupLogging,
|
|
|
|
|
database.initialize,
|
|
|
|
|
settings.initCache
|
2021-07-12 23:35:30 -07:00
|
|
|
], async function (error) {
|
2021-04-09 10:55:31 -07:00
|
|
|
if (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
return process.exit(50);
|
|
|
|
|
}
|
2018-12-09 03:20:00 -08:00
|
|
|
|
2020-08-04 22:16:38 -07:00
|
|
|
const debug = require('debug')('box:taskworker'); // require this here so that logging handler is already setup
|
|
|
|
|
|
2020-08-06 14:36:25 -07:00
|
|
|
process.on('SIGTERM', () => process.exit(0)); // sent as timeout notification
|
2020-08-04 22:16:38 -07:00
|
|
|
|
2020-10-27 09:15:36 +01:00
|
|
|
// ensure we log task crashes with the task logs
|
|
|
|
|
process.on('uncaughtException', function (e) { debug(e); process.exit(1); });
|
|
|
|
|
|
2020-08-04 22:16:38 -07:00
|
|
|
debug(`Starting task ${taskId}. Logs are at ${logFile}`);
|
|
|
|
|
|
2021-07-12 23:35:30 -07:00
|
|
|
const [getError, task] = await safe(tasks.get(taskId));
|
2021-07-14 10:59:49 -07:00
|
|
|
if (getError || !task) {
|
|
|
|
|
debug(getError ? `Error getting task: ${getError.message}` : 'Task not found');
|
2021-07-12 23:35:30 -07:00
|
|
|
return process.exit(50);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const [updateError] = await safe(tasks.update(taskId, { percent: 2, error: null }));
|
|
|
|
|
if (updateError) {
|
|
|
|
|
debug(updateError);
|
|
|
|
|
return process.exit(50);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const progressCallback = async function (progress, callback) {
|
|
|
|
|
await safe(tasks.update(taskId, progress));
|
|
|
|
|
if (callback) callback();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const resultCallback = async (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
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
debug(`Task took ${(new Date() - startTime)/1000} seconds`);
|
|
|
|
|
|
|
|
|
|
await safe(tasks.setCompleted(taskId, progress));
|
|
|
|
|
process.exit(error ? 50 : 0);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
TASKS[task.type].apply(null, task.args.concat(progressCallback).concat(resultCallback));
|
|
|
|
|
} catch (error) {
|
|
|
|
|
debug('Uncaught exception in task', error);
|
|
|
|
|
process.exit(1); // do not call setCompleted() intentionally. the task code must be resilient enough to handle it
|
|
|
|
|
}
|
2018-12-09 03:20:00 -08:00
|
|
|
});
|