Files
cloudron-box/src/taskworker.js

111 lines
3.7 KiB
JavaScript
Raw Normal View History

#!/usr/bin/env node
'use strict';
const apptask = require('./apptask.js'),
async = require('async'),
backups = require('./backups.js'),
cloudron = require('./cloudron.js'),
database = require('./database.js'),
domains = require('./domains.js'),
2019-10-25 15:58:11 -07:00
externalLdap = require('./externalldap.js'),
fs = require('fs'),
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'),
settings = require('./settings.js'),
tasks = require('./tasks.js'),
2021-05-14 10:39:05 +02:00
updater = require('./updater.js');
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,
checkCerts: reverseProxy.checkCerts,
setupDnsAndCert: cloudron.setupDnsAndCert,
2019-01-10 16:00:49 -08:00
cleanBackups: backups.cleanup,
2019-10-25 15:58:11 -07:00
syncExternalLdap: externalLdap.sync,
changeMailLocation: mail.changeLocation,
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}`)),
_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)
};
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
2020-07-31 12:59:15 -07:00
const startTime = new Date();
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);
}
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
process.on('uncaughtException', function (e) { debug(e); process.exit(1); });
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
}
});