96dc79cfe6
- Convert all require()/module.exports to import/export across 260+ files - Add "type": "module" to package.json to enable ESM by default - Add migrations/package.json with "type": "commonjs" to keep db-migrate compatible - Convert eslint.config.js to ESM with sourceType: "module" - Replace __dirname/__filename with import.meta.dirname/import.meta.filename - Replace require.main === module with process.argv[1] === import.meta.filename - Remove 'use strict' directives (implicit in ESM) - Convert dynamic require() in switch statements to static import lookup maps (dns.js, domains.js, backupformats.js, backupsites.js, network.js) - Extract self-referencing exports.CONSTANT patterns into standalone const declarations (apps.js, services.js, locks.js, users.js, mail.js, etc.) - Lazify SERVICES object in services.js to avoid circular dependency TDZ issues - Add clearMailQueue() to mailer.js for ESM-safe queue clearing in tests - Add _setMockApp() to ldapserver.js for ESM-safe test mocking - Add _setMockResolve() wrapper to dig.js for ESM-safe DNS mocking in tests - Convert backupupload.js to use dynamic imports so --check exits before loading the module graph (which requires BOX_ENV) - Update check-install to use ESM import for infra_version.js - Convert scripts/ (hotfix, release, remote_hotfix.js, find-unused-translations) - All 1315 tests passing Migration stats (AI-assisted using Cursor with Claude): - Wall clock time: ~3-4 hours - Assistant completions: ~80-100 - Estimated token usage: ~1-2M tokens Co-authored-by: Cursor <cursoragent@cursor.com>
140 lines
5.1 KiB
JavaScript
Executable File
140 lines
5.1 KiB
JavaScript
Executable File
#!/usr/bin/env -S node --unhandled-rejections=strict
|
|
|
|
import * as apptask from './apptask.js';
|
|
import * as backupCleaner from './backupcleaner.js';
|
|
import * as backupIntegrity from './backupintegrity.js';
|
|
import * as backuptask from './backuptask.js';
|
|
import BoxError from './boxerror.js';
|
|
import * as dashboard from './dashboard.js';
|
|
import * as database from './database.js';
|
|
import * as dns from './dns.js';
|
|
import * as dyndns from './dyndns.js';
|
|
import * as externalLdap from './externalldap.js';
|
|
import fs from 'node:fs';
|
|
import locks from './locks.js';
|
|
import * as mailServer from './mailserver.js';
|
|
import net from 'node:net';
|
|
import * as reverseProxy from './reverseproxy.js';
|
|
import safe from 'safetydance';
|
|
import tasks from './tasks.js';
|
|
import timers from 'timers/promises';
|
|
import * as updater from './updater.js';
|
|
import debugModule from 'debug';
|
|
|
|
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 = debugModule('box:taskworker'); // import 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();
|