Commit 8c197676 authored by priit's avatar priit
Browse files

Utiliidi töö katkestamine

parent 0f360a37
......@@ -33,7 +33,7 @@ router.get('/:sessionId', function(req, res) {
});
});
router.get('/:sessionId/:fileId', function(req, res) {
router.get('/:sessionId/download/:fileId', function(req, res) {
logger.debug({request:'Get file', instance: req.params.sessionId, file: req.params.fileId});
......@@ -59,4 +59,13 @@ router.get('/:sessionId/:fileId', function(req, res) {
});
});
router.get('/:sessionId/kill', function(req, res) {
wrapperService.kill(req.params.sessionId, function (err, data) {
if(err){
return res.status(404).send({errors: err});
}
res.send(data);
});
});
module.exports = router;
......@@ -13,13 +13,14 @@ var Session = function( id ){
this.data = null;
this.outputFiles = {};
this.errors = null;
this.pid = null; //system process id
};
Session.prototype.addOutputFile = function(key, path){
this.outputFiles[key] = path;
};
Session.prototype.setErrors = function(errors, path){
Session.prototype.setErrors = function(errors){
this.errors = errors;
this.isSuccess = false;
this.message = Session.messages.ERROR
......
......@@ -3,23 +3,26 @@ var sessionService = require('../../service/sessionService');
var LocalCommand = require('../../mapper/localCommand');
var spawn = require('child_process').spawn;
var fs = require('fs');
var async = require('async');
function LocalExecutor() {
var self = this;
this.execute = function (commandModel, callback) {
this.execute = function (commandModel, session, callback) {
var response = {
isSuccess:true,
stdOutPath: sessionService.getNewSessionFilePath(commandModel.session, {extension: commandModel.stdOutExtension}),
errors: []
errors: [],
pid: null
};
var localCommand = new LocalCommand(commandModel).generate();
self._executeLocalCommand(localCommand, response, callback);
self._executeLocalCommand(localCommand, session, response, callback);
};
this._executeLocalCommand = function (localCommand, response, callback) {
this._executeLocalCommand = function (localCommand, session, response, cb) {
var command = localCommand.command;
var commandParams = localCommand.commandParams;
......@@ -27,13 +30,29 @@ function LocalExecutor() {
logger.debug(localCommand.command);
logger.debug(localCommand.commandParams);
try {
var process = spawn(command, commandParams);
self._run(response, process, callback);
} catch (e) {
logger.error(e);
callback(e.message);
}
var process = spawn(command, commandParams);
async.waterfall([
function (callback) {
session.pid = process.pid;
sessionService.saveSession( session, function (err, session) {
callback();
});
},
function (callback) {
self._run(response, process, function (err, response) {
callback(err, response);
});
},
function (response, callback) {
session.pid = null;
sessionService.saveSession( session, function (err, session) {
callback(err, response);
});
}
], function (err, response) {
cb(err, response);
});
};
this._run = function (response, process, callback) {
......@@ -59,14 +78,34 @@ function LocalExecutor() {
if(code && code > 0){
response.isSuccess = false;
process.kill();
}
});
process.on('close', function (code, signal) {
logger.debug('child process terminated due to receipt of signal: ' + signal + ' code: ' + code);
if(signal == 'SIGKILL'){
response.isSuccess = false;
response.errors.push({util:' Utiliidi töö katkestati!'});
}
callback(null, response);
});
};
this.kill = function (session, callback) {
logger.debug('Kill system process');
if(session.pid){
try{
process.kill( session.pid, 'SIGKILL' );
callback(null, 'Katkestamise signaal saadetud');
} catch ( e ){
callback('Katkestamisel tekkis viga: ' + e.message);
}
} else {
callback('Protsessi ei leitud');
}
}
}
module.exports = new LocalExecutor();
\ No newline at end of file
......@@ -57,6 +57,41 @@ function WrapperService() {
callback(null, outputFile);
});
};
this.kill = function ( sessionId , callback) {
var mapResponse = function (err, message) {
var data = {};
data.sessionId = sessionId;
if(err){
data.success = false;
data.message = err;
} else {
data.success = true;
data.message = message;
}
var response = {
response: data
};
return response;
};
sessionService.getSession(sessionId, function (err, session) {
if(err){
return callback(err);
}
var processor = new Processor();
if(processor.kill != undefined){
processor.kill( session, function (err, response) {
callback(null, mapResponse(err, response));
});
} else {
callback(null, mapResponse('Teenusele ei ole katkestamise meetodit implementeeritud'));
}
});
};
}
module.exports = new WrapperService();
\ No newline at end of file
......@@ -22,11 +22,11 @@ function InputOutputLocalCommand(){
return callback( err, session );
}
self.getCommandModel(session, function (err, model) {
self._getCommandModel(session, function (err, model) {
logger.debug('getCommandModel callback');
if(err) return callback(err);
localExecutor.execute( model, function ( err, response ) {
localExecutor.execute( model, session, function ( err, response ) {
if(err) return callback(err);
logger.debug('Program is finished');
......@@ -50,7 +50,7 @@ function InputOutputLocalCommand(){
});
};
this.getCommandModel = function (session, callback) {
this._getCommandModel = function (session, callback) {
var model = new CommandModel();
model.serviceProperties.commandTemplate = config.wrapper.command.commandTemplate;
......@@ -61,7 +61,9 @@ function InputOutputLocalCommand(){
logger.debug('Render callback');
callback(err, model);
});
}
};
this.kill = localExecutor.kill;
}
module.exports = InputOutputLocalCommand;
\ No newline at end of file
......@@ -14,14 +14,14 @@ function SimpleLocalCommand(){
this.process = function ( session, callback) {
self.getCommandModel(session, function (err, model) {
self._getCommandModel(session, function (err, model) {
logger.debug('GetCommandModel callback');
if(err){
logger.error(err);
return callback(err);
}
localExecutor.execute( model, function ( err, response ) {
localExecutor.execute( model, session, function ( err, response ) {
if(err) return callback(err);
logger.debug('Program is finished');
......@@ -43,7 +43,7 @@ function SimpleLocalCommand(){
});
};
this.getCommandModel = function (session, callback) {
this._getCommandModel = function (session, callback) {
var model = new CommandModel();
model.serviceProperties.commandTemplate = config.wrapper.command.commandTemplate;
model.init( session );
......@@ -52,7 +52,9 @@ function SimpleLocalCommand(){
logger.debug('Render callback');
callback(err, model);
});
}
};
this.kill = localExecutor.kill;
}
module.exports = SimpleLocalCommand;
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment