Files
cloudron-box/src/taskworker.js

114 lines
4.2 KiB
JavaScript
Raw Normal View History

#!/usr/bin/env node
'use strict';
const apptask = require('./apptask.js'),
async = require('async'),
2021-07-14 11:07:19 -07:00
backupCleaner = require('./backupcleaner.js'),
backuptask = require('./backuptask.js'),
cloudron = require('./cloudron.js'),
database = require('./database.js'),
2021-08-13 17:22:28 -07:00
dns = require('./dns.js'),
dyndns = require('./dyndns.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'),
2022-10-12 10:26:21 +02:00
system = require('./system.js'),
tasks = require('./tasks.js'),
2022-04-15 17:40:46 -05:00
updater = require('./updater.js');
const TASKS = { // indexed by task type
2019-08-26 15:55:57 -07:00
app: apptask.run,
backup: backuptask.fullBackup,
2018-12-09 12:04:51 -08:00
update: updater.update,
checkCerts: reverseProxy.checkCerts,
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,
changeMailLocation: mail.changeLocation,
2021-08-13 17:22:28 -07:00
syncDnsRecords: dns.syncDnsRecords,
syncDyndns: dyndns.sync,
2022-10-12 10:26:21 +02:00
updateDiskUsage: system.updateDiskUsage,
2018-12-10 21:05:46 -08:00
2022-04-15 17:40:46 -05:00
_identity: async (arg, progressCallback) => { progressCallback(); return arg; },
_error: async (arg, progressCallback) => { progressCallback(); throw new Error(`Failed for arg: ${arg}`); },
2022-04-28 18:03:36 -07:00
_crash: (arg) => { throw new Error(`Crashing for arg: ${arg}`); }, // the test looks for this debug string in the log file
2022-04-15 17:40:46 -05:00
_sleep: async (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];
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]);
};
process.stdout.logFile = logFile; // used by update task
2021-08-30 22:01:34 -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);
}
// Main process starts here
2020-07-31 12:59:15 -07:00
const startTime = new Date();
async.series([
setupLogging,
database.initialize,
settings.initCache
2022-04-15 17:40:46 -05:00
], async function (initError) {
if (initError) {
console.error(initError);
2021-04-09 10:55:31 -07:00
return process.exit(50);
}
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
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 }));
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
2022-04-15 17:40:46 -05:00
async function progressCallback(progress) {
2021-09-16 13:59:03 -07:00
await safe(tasks.update(taskId, progress), { debug });
2022-04-15 17:40:46 -05:00
}
2021-07-12 23:35:30 -07:00
2022-04-15 17:40:46 -05:00
try {
const [runError, result] = await safe(TASKS[task.type].apply(null, task.args.concat(progressCallback)));
2021-07-12 23:35:30 -07:00
const progress = {
result: result || null,
2022-04-15 17:40:46 -05:00
error: runError ? JSON.parse(JSON.stringify(runError, Object.getOwnPropertyNames(runError))) : null
2021-07-12 23:35:30 -07:00
};
debug(`Task took ${(new Date() - startTime)/1000} seconds`);
await safe(tasks.setCompleted(taskId, progress));
2022-04-15 17:40:46 -05:00
exitSync({ error: runError, code: runError ? 50 : 0 });
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
}
});