replace debug() with our custom logger
mostly we want trace() and log(). trace() can be enabled whenever we want by flipping a flag and restarting box
This commit is contained in:
+8
-8
@@ -19,7 +19,7 @@ import safe from 'safetydance';
|
||||
import tasks from './tasks.js';
|
||||
import timers from 'timers/promises';
|
||||
import updater from './updater.js';
|
||||
import debugModule from 'debug';
|
||||
import logger from './logger.js';
|
||||
|
||||
const TASKS = { // indexed by task type
|
||||
app: apptask.run,
|
||||
@@ -100,28 +100,28 @@ async function main() {
|
||||
return process.exit(50);
|
||||
}
|
||||
|
||||
const debug = debugModule('box:taskworker'); // import this here so that logging handler is already setup
|
||||
const { log, trace } = logger('taskworker'); // import this here so that logging handler is already setup
|
||||
|
||||
process.on('SIGTERM', () => {
|
||||
debug('Terminated');
|
||||
log('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}`);
|
||||
log(`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 });
|
||||
await safe(tasks.update(taskId, progress), { debug: log });
|
||||
}
|
||||
|
||||
const taskName = task.type.replace(/_.*/,'');
|
||||
debug(`Running task of type ${taskName}`);
|
||||
log(`Running task of type ${taskName}`);
|
||||
const [runError, result] = await safe(TASKS[taskName].apply(null, task.args.concat(progressCallback)));
|
||||
const progress = {
|
||||
result: result || null,
|
||||
@@ -129,9 +129,9 @@ async function main() {
|
||||
percent: 100
|
||||
};
|
||||
|
||||
await safe(tasks.setCompleted(taskId, progress), { debug });
|
||||
await safe(tasks.setCompleted(taskId, progress), { debug: log });
|
||||
|
||||
debug(`Task took ${(new Date() - startTime)/1000} seconds`);
|
||||
log(`Task took ${(new Date() - startTime)/1000} seconds`);
|
||||
|
||||
exitSync({ error: runError, code: (!runError || runError instanceof BoxError) ? 0 : 50 }); // handled error vs run time crash
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user