Files
cloudron-box/src/taskworker.js
2021-08-23 15:20:14 -07:00

118 lines
4.1 KiB
JavaScript
Executable File

#!/usr/bin/env node
'use strict';
const apptask = require('./apptask.js'),
async = require('async'),
backupCleaner = require('./backupcleaner.js'),
backuptask = require('./backuptask.js'),
cloudron = require('./cloudron.js'),
database = require('./database.js'),
dns = require('./dns.js'),
externalLdap = require('./externalldap.js'),
fs = require('fs'),
mail = require('./mail.js'),
reverseProxy = require('./reverseproxy.js'),
safe = require('safetydance'),
settings = require('./settings.js'),
tasks = require('./tasks.js'),
updater = require('./updater.js'),
util = require('util');
const TASKS = { // indexed by task type
app: apptask.run,
backup: backuptask.backupBoxAndApps,
update: updater.update,
checkCerts: reverseProxy.checkCerts,
setupDnsAndCert: cloudron.setupDnsAndCert,
cleanBackups: backupCleaner.run,
syncExternalLdap: externalLdap.sync,
changeMailLocation: mail.changeLocation,
syncDnsRecords: dns.syncDnsRecords,
_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}`); }, // the test looks for this debug string in the log file
_sleep: (arg) => setTimeout(process.exit, arg)
};
if (process.argv.length !== 4) {
console.error('Pass the taskid and logfile as argument');
process.exit(1);
}
const taskId = process.argv[2];
const logFile = process.argv[3];
function setupLogging(callback) {
const logfileStream = fs.createWriteStream(logFile, { flags:'a' });
process.stdout.write = process.stderr.write = logfileStream.write.bind(logfileStream);
callback();
}
// Main process starts here
const startTime = new Date();
async.series([
setupLogging,
database.initialize,
settings.initCache
], async function (error) {
if (error) {
console.error(error);
return process.exit(50);
}
const debug = require('debug')('box:taskworker'); // require this here so that logging handler is already setup
process.on('SIGTERM', () => process.exit(0)); // sent as timeout notification
// ensure we log task crashes with the task logs. neither console.log nor debug are sync for some reason
process.on('uncaughtException', function (e) { fs.appendFileSync(logFile, e.stack); process.exit(1); });
debug(`Starting task ${taskId}. Logs are at ${logFile}`);
const [getError, task] = await safe(tasks.get(taskId));
if (getError || !task) {
debug(getError ? `Error getting task: ${getError.message}` : 'Task not found');
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 {
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)));
resultCallback(error, result);
} else {
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
}
});