mhl-dispatcher
Version:
A node dispatcher server for running and managing batch processes
80 lines (68 loc) • 2.01 kB
JavaScript
var exec = require('child_process').exec,
xml2js = require('xml2js'),
http = require('http'),
fs = require('fs'),
proc = {},
APPLICATIONSPATH = 'http://oracle.mhl.co.uk:8080/public/bm/Applications.xml';
'use strict';
module.exports = exports = proc;
proc.apps = null;
proc.run = function(command, callback){
var child;
//envProc = process.env;
//envProc['BatchID'] = '789983';
//command = 'set BatchID='+ envs + ' & ' + command;
//process.env['BatchID'] = envs;
//command = 'C:\Program Files\Autodesk\3ds Max Design 2012\3dsmax.exe" -showRFW:0 -script: "\\mhl.co.uk\dfs\job\1847\dispatcher test\Example_NJBatch02\SSR_130204160118.ms" "\\mhl.co.uk\dfs\job\1847\dispatcher test\Example_NJBatch02\city0411.max"'
//console.log(envProc);
child = exec(command, callback);
return child;
};
proc.kill = function(pid, callback){
var winKillCommand = 'Taskkill /PID '+ pid +' /F';
//process.kill(pid, 'SIGTERM');
process.nextTick(function(){
proc.run(winKillCommand, callback);
});
};
proc.getExecutablePath = function(appKey){
var apps, i, n;
if(proc.apps){
apps = proc.apps.applications.application;
for(i = 0, n = apps.length; i < n; i++){
if(apps[i]['$'].key == appKey){
return apps[i].executablepath[0];
}
}
}
return '';
};
proc.stringify = function(exe, args){
return '"' + exe + '" '+ args;
};
proc.loadAppsFile = function(url, callback){
http.get(url, function(res){
res.setEncoding('utf8');
res.on('data', function(chunk){
var parser = new xml2js.Parser();
parser.parseString(chunk, function(err, result){
if(err){
console.log(err);
}else{
proc.apps = result;
}
});
});
}).on('error', function(e){
//throw server sxception
console.log(e);
});
};
proc.init = function(){
proc.loadAppsFile(APPLICATIONSPATH);
};
//auto load the applications file on start up
proc.init();
proc.sample = function(data, callback){
callback(data);
};