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-08-13 17:22:28 -07:00
|
|
|
dns = require('./dns.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-08-22 17:08:16 -07:00
|
|
|
updater = require('./updater.js'),
|
|
|
|
|
util = require('util');
|
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-09-26 18:37:04 -07:00
|
|
|
backup: backuptask.fullBackup,
|
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-08-13 17:22:28 -07:00
|
|
|
syncDnsRecords: dns.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];
|
2021-08-30 22:01:34 -07:00
|
|
|
let logFd = null;
|
|
|
|
|
|
|
|
|
|
async function setupLogging() {
|
|
|
|
|
logFd = fs.openSync(logFile, 'a');
|
|
|
|
|
// we used to write using a stream before but it caches internally and there is no way to flush it when things crash
|
|
|
|
|
process.stdout.write = process.stderr.write = function (...args) {
|
|
|
|
|
const callback = typeof args[args.length-1] === 'function' ? args.pop() : function () {}; // callback is required for fs.write
|
|
|
|
|
fs.write.apply(fs, [logFd, ...args, callback]);
|
|
|
|
|
};
|
|
|
|
|
}
|
2020-08-04 22:16:38 -07:00
|
|
|
|
2021-08-30 22:01:34 -07:00
|
|
|
// this is also used as the 'uncaughtException' handler which can only have synchronous functions
|
|
|
|
|
function exitSync(status) {
|
|
|
|
|
if (status.error) fs.write(logFd, status.error.stack + '\n', function () {});
|
|
|
|
|
fs.fsyncSync(logFd);
|
|
|
|
|
fs.closeSync(logFd);
|
|
|
|
|
process.exit(status.code);
|
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
|
|
|
|
|
|
2021-08-30 22:01:34 -07:00
|
|
|
process.on('SIGTERM', () => exitSync({ code: 0 })); // sent as timeout notification
|
2020-08-04 22:16:38 -07:00
|
|
|
|
2021-07-26 22:11:25 -07:00
|
|
|
// ensure we log task crashes with the task logs. neither console.log nor debug are sync for some reason
|
2021-08-30 22:01:34 -07:00
|
|
|
process.on('uncaughtException', (error) => exitSync({ error, code: 1 }));
|
2020-10-27 09:15:36 +01:00
|
|
|
|
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-08-30 22:01:34 -07:00
|
|
|
if (getError) return exitSync({ error: getError, code: 50 });
|
|
|
|
|
if (!task) return exitSync({ error: new Error(`Task ${taskId} not found`), code: 50 });
|
2021-07-12 23:35:30 -07:00
|
|
|
|
|
|
|
|
const progressCallback = async function (progress, callback) {
|
2021-09-16 13:59:03 -07:00
|
|
|
await safe(tasks.update(taskId, progress), { debug });
|
2021-07-12 23:35:30 -07:00
|
|
|
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));
|
2021-08-30 22:01:34 -07:00
|
|
|
exitSync({ error, code: error ? 50 : 0 });
|
2021-07-12 23:35:30 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try {
|
2021-08-22 17:08:16 -07:00
|
|
|
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)));
|
2021-08-20 09:19:44 -07:00
|
|
|
await resultCallback(error, result);
|
2021-08-22 17:08:16 -07:00
|
|
|
} else {
|
|
|
|
|
TASKS[task.type].apply(null, task.args.concat(progressCallback).concat(resultCallback));
|
|
|
|
|
}
|
2021-07-12 23:35:30 -07:00
|
|
|
} catch (error) {
|
2021-08-30 22:01:34 -07:00
|
|
|
exitSync({ error, code: 1 }); // do not call setCompleted() intentionally. the task code must be resilient enough to handle it
|
2021-07-12 23:35:30 -07:00
|
|
|
}
|
2018-12-09 03:20:00 -08:00
|
|
|
});
|