Skip to content

Commit

Permalink
Merge pull request #1 from wolterskluwer-redwoodhq/rest-api
Browse files Browse the repository at this point in the history
Rest api
  • Loading branch information
Wolters Kluwer - UpToDate authored Sep 9, 2016
2 parents 400559a + 24568e7 commit 9f6d7ce
Show file tree
Hide file tree
Showing 6 changed files with 586 additions and 17 deletions.
8 changes: 6 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/**
* Module dependencies.
*/
Expand Down Expand Up @@ -49,7 +48,8 @@ var express = require('express')
, actionHistory = require('./routes/actionHistory')
, versionControl = require('./routes/versionControl')
, syncIDE = require('./routes/syncIDE')
, testcaseHistory = require('./routes/testcaseHistory');
, testcaseHistory = require('./routes/testcaseHistory')
, remoteexecution = require('./routes/remoteexecution');

var realFs = require("fs");
var gracefulFs = require("graceful-fs");
Expand Down Expand Up @@ -164,6 +164,9 @@ app.post('/variableTags',auth.auth, variableTags.variableTagsPost);
//start execution
app.post('/executionengine/startexecution', executionengine.startexecutionPost);

// remote execution
app.get('/api/remoteexecution/startexecution', remoteexecution.startexecutionPost);

//stop
app.post('/executionengine/stopexecution',auth.auth, executionengine.stopexecutionPost);
app.post('/executionengine/actionresult',executionengine.actionresultPost);
Expand Down Expand Up @@ -312,6 +315,7 @@ common.parseConfig(function(){
common.cleanUpExecutions();
common.cleanUpUserStatus(function(){
var server = require('http').createServer(app);
server.setTimeout(parseInt(common.Config.DefaultTimeout));
server.listen(common.Config.AppServerPort, function(){
realtime.initSocket(server);
common.logger.log("Express server listening on port %d in %s mode", common.Config.AppServerPort, app.settings.env);
Expand Down
17 changes: 10 additions & 7 deletions properties.conf
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
[Agent]
AgentVersion=1.00.01
AgentVersion=2.40.20
AgentPort=5009
AgentVNCPort=3006
Update=PASS
Update=
OS=Windows
CloudAgent=true

[Server]
ServerVersion=
ServerVersion=2.40.20
DBPort=27017
AppServerPort=3000
AppServerIPHost=localhost
AppServerIPHost22=10.130.10.35
AppServerIPHost22=192.168.10.107
AppServerIPHost=127.0.0.1
# Default timeout for REST API in Milliseconds
DefaultTimeout=1200000

[Windows]
VNC=Yes
AppShortcutFolderName=RedwoodHQ
9 changes: 9 additions & 0 deletions public/controller/Executions.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,15 @@ Ext.define("Redwood.controller.Executions", {
executionView.up("executionsEditor").down("#runExecution").setDisabled(false);
executionView.up("executionsEditor").down("#stopExecution").setDisabled(true);
}
},
failure: function(response) {
if (Ext.MessageBox.isVisible()) Ext.MessageBox.hide();
var obj = Ext.decode(response.responseText);
if(obj.error != null){
Ext.Msg.alert('Error', Ext.util.Format.htmlEncode(obj.error));
executionView.up("executionsEditor").down("#runExecution").setDisabled(false);
executionView.up("executionsEditor").down("#stopExecution").setDisabled(true);
}
}
});
})
Expand Down
74 changes: 67 additions & 7 deletions routes/executionengine.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var nodemailer = require("nodemailer");
var spawn = require('child_process').spawn;
var os = require('os');
var archiver = require('archiver');
var elk = require('./elk');
var db;
var compilations = {};
var fileSync = {};
Expand Down Expand Up @@ -61,13 +62,47 @@ exports.stopexecutionPost = function(req, res){
});
};


exports.startexecutionPost = function(req, res){
_startexecutionPost(req, res, function(exitcode){
console.log("+++++++++++++++++++++++++EXECUTIONS STARTED ++++++++++++++++++++++++++++++++++++++++++++++++")
var executionId = req.body.executionID;
MonitorExecution(executionId, function(){
// End the startexecution request
console.log('execution completed');
elk.publishDataToElk(executionId, function(){
console.log("Data published success: publishDataToElk");
});
});
});
}

function MonitorExecution(execId,callback){
db = common.getDB();
var getStatus = function(callback){
db.collection('executions', function(err, collection) {
collection.findOne({_id:execId}, {status:1}, function(err, dbexecution) {
callback(dbexecution.status);
});
});
};
var verifyStatus = function(status){
if(status == "Ready To Run"){
setTimeout(function(){callback()},10000);
}else{
setTimeout(function(){getStatus(verifyStatus)},10000)
}
};
setTimeout(function(){getStatus(verifyStatus)},20000)
}

var _startexecutionPost = function(req, res, callback){
db = common.getDB();

res.contentType('json');
if (req.body.testcases.length == 0){
res.contentType('json');
res.status(403);
res.json({error:"No Test Cases are selected for execution."});
callback(0); // forbidden
return;
}
var executionID = req.body.executionID;
Expand Down Expand Up @@ -96,7 +131,10 @@ exports.startexecutionPost = function(req, res){

var machineConflict = false;
var updatingConflict = false;

machines.forEach(function(machine){
machine.threads = (machine.threads) ? machine.threads : 1;
console.log("validating thread count " + machine.threads);
if (machine.state == "Running Test"){
machineConflict = true;
}
Expand All @@ -107,20 +145,27 @@ exports.startexecutionPost = function(req, res){
});

if(machineConflict == true){
res.contentType('json');
console.log("selected machines are running tests +++++++++");
res.status(409);
res.json({error:"Selected machines are currently running other tests."});
callback(0); // conflict
return;
}

if(updatingConflict == true){
res.contentType('json');
console.log("selected machines are running tests 2 +++++++++");
res.status(409);
res.json({error:"Selected machines are being updated."});
callback(0); // conflict
return;
}

if(executions[executionID]){
console.log("selected machines are running tests 3 +++++++++");
res.contentType('json');
res.status(409);
res.json({error:"Execution is already running."});
callback(0); // conflict
return;
}

Expand All @@ -134,9 +179,11 @@ exports.startexecutionPost = function(req, res){
compileBuild(req.cookies.project,req.cookies.username,function(err){
if (err != null){
res.contentType('json');
res.status(403);
res.json({error:"Unable to compile scripts."});
updateExecution({_id:executionID},{$set:{status:"Ready To Run"}},true);
delete executions[executionID];
callback(0); // forbidden
}
else{
//copy files for each execution to prevent conflicts
Expand All @@ -149,16 +196,22 @@ exports.startexecutionPost = function(req, res){
executions[executionID].sourceCache = sourceCache;
}
else{
console.log("internal error +++++++++");
/*res.status(500);
res.json({error:"Internal Error"});*/
callback(0);
return;
}
verifyMachineState(machines,function(err){
if(err){
console.error("verifymachinestate failed +++++++++");
updateExecution({_id:executionID},{$set:{status:"Ready To Run"}},true);
res.contentType('json');
res.status(403);
res.json({error:err});
//git.deleteFiles(path.join(__dirname, '../public/automationscripts/'+req.cookies.project+"/"+req.cookies.username+"/build"),os.tmpDir()+"/jar_"+req.body.executionID);
deleteDir(os.tmpDir()+"/jar_"+req.body.executionID);
delete executions[executionID];
callback(0); // forbidden
return;
}
VerifyCloudCapacity(executions[executionID].template,function(response){
Expand All @@ -170,12 +223,15 @@ exports.startexecutionPost = function(req, res){
else{
message = "Cloud does not have the capacity to run this execution."
}
console.error("cloud error: failed +++++++++");
updateExecution({_id:executionID},{$set:{status:"Ready To Run",cloudStatus:"Error: "+message}},true);
res.contentType('json');
res.status(500);
res.json({error:"Cloud Error: "+message});
//git.deleteFiles(path.join(__dirname, '../public/automationscripts/'+req.cookies.project+"/"+req.cookies.username+"/build"),os.tmpDir()+"/jar_"+req.body.executionID);
deleteDir(os.tmpDir()+"/jar_"+req.body.executionID);
delete executions[executionID];
callback(0); //internal error
return;
}
res.contentType('json');
Expand All @@ -194,6 +250,8 @@ exports.startexecutionPost = function(req, res){
//git.deleteFiles(path.join(__dirname, '../public/automationscripts/'+req.cookies.project+"/"+req.cookies.username+"/build"),os.tmpDir()+"/jar_"+req.body.executionID);
deleteDir(os.tmpDir()+"/jar_"+req.body.executionID);
delete executions[executionID];
console.error("startcloudmachine failed +++++++++");
callback(0); //elk
return;
}
if(executions[executionID].template){
Expand Down Expand Up @@ -223,6 +281,7 @@ exports.startexecutionPost = function(req, res){
});
});
});
callback(0); // elk
});
});
});
Expand Down Expand Up @@ -1937,6 +1996,8 @@ function updateExecution(query,update,finished,callback){
}
realtime.emitMessage("UpdateExecutions",data);
if(finished === true){
console.log("FinishExecution +++++++++++++++ ");
//console.log(data);
realtime.emitMessage("FinishExecution",data);
}
if (callback){
Expand Down Expand Up @@ -1995,7 +2056,7 @@ function verifyMachineState(machines,callback){
machines.forEach(function(machine){
db.collection('machines', function(err, collection) {
collection.findOne({_id:new ObjectID(machine._id)}, {}, function(err, dbMachine) {

//console.log("verifyMachineState : " + machine.threads );
if(dbMachine.maxThreads < dbMachine.takenThreads + machine.threads)
{
callback("Machine: "+ machine.host+" has reached thread limit.");
Expand Down Expand Up @@ -2768,4 +2829,3 @@ function deleteDir(path,callback){
}
})
}

10 changes: 9 additions & 1 deletion routes/executions.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ exports.executionsDelete = function(req, res){
Tags.CleanUpExecutionTags(req);
};

exports.deleteExecution = function(db, execution, callback) {
DeleteExecutions(db, execution, function() {
console.log("executions.deleteExecution")
realtime.emitMessage("DeleteExecutions",{id: execution._id})
callback()
})
}

exports.executionsPost = function(req, res){
var app = require('../common');
var data = req.body;
Expand Down Expand Up @@ -162,4 +170,4 @@ function GetExecutions(db,query,callback){
});
})
})
}
}
Loading

0 comments on commit 9f6d7ce

Please sign in to comment.