pass log file as argument to task worker
initially, i thought i can hardcode the log file into taskworker.js depending on the task type but for apptask, it's not easy to get the appId from the taskId unless we introspect task arguments as well. it's easier for now to pass it as an argument.
This commit is contained in:
+37
-18
@@ -1,19 +1,17 @@
|
||||
'use strict';
|
||||
|
||||
var apptask = require('./apptask.js'),
|
||||
assert = require('assert'),
|
||||
async = require('async'),
|
||||
backups = require('./backups.js'),
|
||||
database = require('./database.js'),
|
||||
debug = require('debug')('box:taskworker'),
|
||||
domains = require('./domains.js'),
|
||||
externalLdap = require('./externalldap.js'),
|
||||
fs = require('fs'),
|
||||
reverseProxy = require('./reverseproxy.js'),
|
||||
settings = require('./settings.js'),
|
||||
tasks = require('./tasks.js'),
|
||||
updater = require('./updater.js');
|
||||
|
||||
const NOOP_CALLBACK = function (error) { if (error) debug(error); };
|
||||
updater = require('./updater.js'),
|
||||
util = require('util');
|
||||
|
||||
const TASKS = { // indexed by task type
|
||||
app: apptask.run,
|
||||
@@ -26,31 +24,47 @@ const TASKS = { // indexed by task type
|
||||
|
||||
_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}`); },
|
||||
_crash: (arg) => { throw new Error(`Crashing for arg: ${arg}`); }, // the test looks for this debug string in the log file
|
||||
_sleep: (arg) => setTimeout(process.exit, arg)
|
||||
};
|
||||
|
||||
process.on('SIGTERM', function () {
|
||||
process.exit(0);
|
||||
});
|
||||
if (process.argv.length !== 4) {
|
||||
console.error('Pass the taskid and logfile as argument');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
assert.strictEqual(process.argv.length, 3, 'Pass the taskid as argument');
|
||||
const taskId = process.argv[2];
|
||||
const logFile = process.argv[3];
|
||||
|
||||
function initialize(callback) {
|
||||
async.series([
|
||||
database.initialize,
|
||||
settings.initCache
|
||||
], callback);
|
||||
function setupLogging(callback) {
|
||||
fs.open(logFile, 'a', function (error, fd) {
|
||||
if (error) return callback(error);
|
||||
|
||||
require('debug').log = function (...args) {
|
||||
fs.appendFileSync(fd, util.format(...args) + '\n');
|
||||
};
|
||||
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
// Main process starts here
|
||||
const startTime = new Date();
|
||||
debug(`Starting task ${taskId}`);
|
||||
|
||||
initialize(function (error) {
|
||||
async.series([
|
||||
setupLogging,
|
||||
database.initialize,
|
||||
settings.initCache
|
||||
], function (error) {
|
||||
if (error) return process.exit(50);
|
||||
|
||||
const debug = require('debug')('box:taskworker'); // require this here so that logging handler is already setup
|
||||
const NOOP_CALLBACK = function (error) { if (error) debug(error); };
|
||||
|
||||
process.on('SIGTERM', () => process.exit(0));
|
||||
|
||||
debug(`Starting task ${taskId}. Logs are at ${logFile}`);
|
||||
|
||||
tasks.get(taskId, function (error, task) {
|
||||
if (error) return process.exit(50);
|
||||
|
||||
@@ -67,6 +81,11 @@ initialize(function (error) {
|
||||
tasks.setCompleted(taskId, progress, () => process.exit(error ? 50 : 0));
|
||||
};
|
||||
|
||||
TASKS[task.type].apply(null, task.args.concat(progressCallback).concat(resultCallback));
|
||||
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
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user