#!/usr/bin/env -S node --unhandled-rejections=strict 'use strict'; const apptask = require('./apptask.js'), backupCleaner = require('./backupcleaner.js'), backupIntegrity = require('./backupintegrity.js'), backuptask = require('./backuptask.js'), BoxError = require('./boxerror.js'), dashboard = require('./dashboard.js'), database = require('./database.js'), dns = require('./dns.js'), dyndns = require('./dyndns.js'), externalLdap = require('./externalldap.js'), fs = require('node:fs'), locks = require('./locks.js'), mailServer = require('./mailserver.js'), net = require('node:net'), reverseProxy = require('./reverseproxy.js'), safe = require('safetydance'), tasks = require('./tasks.js'), timers = require('timers/promises'), updater = require('./updater.js'); const TASKS = { // indexed by task type app: apptask.run, appBackup: backuptask.appBackup, backup: backuptask.fullBackup, boxUpdate: updater.updateBox, checkCerts: reverseProxy.checkCerts, prepareDashboardLocation: dashboard.prepareLocation, cleanBackups: backupCleaner.run, syncExternalLdap: externalLdap.sync, changeMailLocation: mailServer.changeLocation, syncDnsRecords: dns.syncDnsRecords, syncDyndns: dyndns.sync, checkBackupIntegrity: backupIntegrity.check, // testing identity: async (arg, progressCallback) => { progressCallback({ percent: 20 }); return arg; }, error: async (arg, progressCallback) => { progressCallback({ percent: 20 }); throw 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: async (arg) => await timers.setTimeout(parseInt(arg, 10)) }; 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]; 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 } // happy eyeballs workaround. see box.js for detailed note async function setupNetworking() { net.setDefaultAutoSelectFamilyAttemptTimeout(2500); } // this is also used as the 'uncaughtException' handler which can only have synchronous functions // taskworker.sh forwards the exit code of the actual worker. It's either a raw signal number OR the exit code. So, choose exit codes > 31 // 50 - internal error , 70 - SIGTERM exit function exitSync(status) { if (status.error) fs.write(logFd, status.error.stack + '\n', function () {}); fs.write(logFd, `${(new Date()).toISOString()} Exiting with code ${status.code}\n`, function () {}); fs.fsyncSync(logFd); fs.closeSync(logFd); process.exit(status.code); } function toTaskError(runError) { if (runError instanceof BoxError) return runError.toPlainObject(); return { message: `Task crashed. ${runError.message}`, stack: runError.stack, code: tasks.ECRASHED }; } // Main process starts here const startTime = new Date(); async function main() { try { await setupLogging(); await setupNetworking(); await database.initialize(); locks.setTaskId(taskId); } catch (initError) { console.error(initError); return process.exit(50); } const debug = require('debug')('box:taskworker'); // require this here so that logging handler is already setup process.on('SIGTERM', () => { debug('Terminated'); exitSync({ code: 70 }); }); // ensure we log task crashes with the task logs. neither console.log nor debug are sync for some reason process.on('uncaughtException', (error) => exitSync({ error, code: 1 })); debug(`Starting task ${taskId}. Logs are at ${logFile}`); const [getError, task] = await safe(tasks.get(taskId)); if (getError) return exitSync({ error: getError, code: 50 }); if (!task) return exitSync({ error: new Error(`Task ${taskId} not found`), code: 50 }); async function progressCallback(progress) { await safe(tasks.update(taskId, progress), { debug }); } const taskName = task.type.replace(/_.*/,''); debug(`Running task of type ${taskName}`); const [runError, result] = await safe(TASKS[taskName].apply(null, task.args.concat(progressCallback))); const progress = { result: result || null, error: runError ? toTaskError(runError) : null, percent: 100 }; await safe(tasks.setCompleted(taskId, progress), { debug }); debug(`Task took ${(new Date() - startTime)/1000} seconds`); exitSync({ error: runError, code: (!runError || runError instanceof BoxError) ? 0 : 50 }); // handled error vs run time crash } main();