Skip to content

Commit

Permalink
Merge pull request #447 from Optum/master
Browse files Browse the repository at this point in the history
R0.9.0
  • Loading branch information
jdweeks authored Mar 28, 2019
2 parents 832e8fa + cec0661 commit 7f45a32
Show file tree
Hide file tree
Showing 48 changed files with 3,567 additions and 3,298 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ specs
.nyc_output
*.DS_store
public/js/bower_components
public/fusepartials
.vscode/settings.json
6 changes: 2 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ RUN mkdir -p /app
COPY . /app
WORKDIR /app

# install app dependencies (backend)
# install app dependencies
RUN npm install

# install app depedencies (frontend)
RUN npm install -g bower
RUN bower install --allow-root

# fix for openshift permission problems
# fix for k8s permission problems
RUN mkdir /.pm2 && chmod 777 /.pm2 && chmod 777 /app

# start app
Expand Down
84 changes: 73 additions & 11 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,60 @@ const helmet = require('helmet');
const actuator = require('express-actuator');
const schedule = require('node-schedule');
const Archive = require('./models/common/Archive');
const System = require('./models/common/System');
const MQService = require('./models/mq/MQService');
const constants = require('./lib/util/constants');
const fuseHelper = require('./lib/util/fuse');
global.__basedir = __dirname;

// connect to database
const db = require('./models/db');
db.on('error', function(err) {throw err; });
db.on('error', function(err) { throw err; });
db.once('open', function() {
debug(`Successfully connected to Mongo (${process.env.MONGODB_HOST})`);

// retroactively assign queue manager / request queue to groups
const defaultManager = process.env.DEFAULT_QUEUE_MANAGER;
const defaultQueue = process.env.DEFAULT_REQUEST_QUEUE;

if (!defaultManager || !defaultQueue) {
debug('No default queue manager / request queue is configured');
}
else {
mqInfo = {
manager: defaultManager,
reqQueue: defaultQueue
};

System.find({}, function(err, systems) {
if (err) return;

systems.forEach(function(system) {
if (!system.mqInfo) system.mqInfo = mqInfo;
system.save(function(err, newSystem) {
if (err) debug(err);
});
});
});
}

// retroactively assign payload type to MQ services
MQService.find({}, function(err, services) {
if (err) {
debug(err);
return;
}

services.forEach(function(service) {
service.rrpairs.forEach(function(rrpair) {
if (!rrpair.payloadType) rrpair.payloadType = 'XML';
});
service.save(function(err, newService) {
if (err) debug(err);
});
});
});

// ready to start
app.emit('ready');
});
Expand All @@ -38,7 +84,7 @@ function init() {
app.use(morgan('dev'));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

// parse request body as plaintext if no content-type is set
app.use(function(req, res, next) {
if (!req.get('content-type')) {
Expand Down Expand Up @@ -137,16 +183,20 @@ function init() {

// register SOAP / REST virts from DB
virtual.registerAllRRPairsForAllServices();

app.use('/api/services', api);
app.use('/virtual', virtual.router);
app.use('/virtual',invoke.router);
app.use('/virtual', invoke.router);

// initialize recording routers
const recorder = require('./routes/recording');
const recorderController = require('./controllers/recorderController');
app.use('/recording',recorder.recordingRouter);
app.use('/api/recording',recorder.apiRouter);

const mqController = require('./controllers/mqController');
mqController.registerAllMQServices();

// register new virts on all threads
if (process.env.MOCKIATO_MODE !== 'single') {
process.on('message', function(message) {
Expand All @@ -157,19 +207,27 @@ function init() {
const action = msg.action;
debug(action);

virtual.deregisterService(service);
invoke.deregisterServiceInvoke(service);
if (action === 'register') {
virtual.registerService(service);

if(service.liveInvocation && service.liveInvocation.enabled){
invoke.registerServiceInvoke(service);
if (service.type !== 'MQ') {
virtual.deregisterService(service);
invoke.deregisterServiceInvoke(service);
if (action === 'register') {
virtual.registerService(service);

if(service.liveInvocation && service.liveInvocation.enabled){
invoke.registerServiceInvoke(service);
}
}
}
else {
mqController.deregisterMQService(service);
if (action === 'register') {
mqController.registerMQService(service);
}
}
}else if(msg.recorder){
const rec = msg.recorder;
const action = msg.action;
console.log("msg: " + JSON.stringify(msg));

if(action === 'register'){
recorderController.registerRecorder(rec);
}else if(action === 'deregister'){
Expand Down Expand Up @@ -203,6 +261,9 @@ function init() {
const users = require('./routes/users');
app.use('/api/users', users);

const mqinfo = require('./routes/mqInfo');
app.use('/api/mqinfo', mqinfo);

// handle no match responses
app.use(/\/((?!recording).)*/,function(req, res, next) {
if (!req.msgContainer) {
Expand All @@ -221,6 +282,7 @@ function init() {
});

// ready for testing (see test/test.js)
fuseHelper.fuseAllFiles();
app.emit('started');
}

Expand Down
120 changes: 120 additions & 0 deletions controllers/mqController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
const System = require('../models/common/System');
const MQService = require('../models/mq/MQService');
const virtual = require('../routes/virtual');
const debug = require('debug')('default');

function registerAllMQServices() {
System.find({}, function(err, systems) {
if (err) {
debug('Error registering MQ services: ' + err);
return;
}

systems.forEach(function(system) {
registerMQServicesInSystem(system);
});
});
}

function registerMQServicesInSystem(sut, exclude) {
MQService.find({ 'sut.name' : sut.name }, function(err, mqservices) {
if (err) {
debug('Error registering MQ services: ' + err);
return;
}

mqservices.forEach(function(mqservice) {
if (!deepEquals(mqservice, exclude))
registerMQService(mqservice, sut);
});
});
}

function registerMQService(mqserv, sut) {
if (!sut) {
System.findOne({ 'name' : mqserv.sut.name }, function(err, system) {
if (err) {
debug('Error registering MQ service: ' + err);
return;
}

register(mqserv, system);
});
}
else {
register(mqserv, sut);
}

function register(mqservice, system) {
if (!mqservice.running || !system || !system.mqInfo) {
return;
}

mqservice.basePath = `/mq/${system.mqInfo.manager}/${system.mqInfo.reqQueue}`;

mqservice.rrpairs.forEach(function(rrpair){
rrpair.verb = 'POST';
if (!rrpair.payloadType) rrpair.payloadType = 'XML';

virtual.registerRRPair(mqservice, rrpair);
});
}
}

function deregisterMQService(mqserv) {
System.findOne({ name: mqserv.sut.name }, function(err, sut) {
if (err) {
debug('Error deregistering MQ services: ' + err);
return;
}

if (!sut) {
return;
}

deregisterMQServicesByInfo(sut.mqInfo, reregister);

function reregister(mqinfo) {
let q = {
'$and': [{
'mqInfo.manager': mqinfo.manager
}, {
'mqInfo.reqQueue': mqinfo.reqQueue
}]
}

System.find(q, function(err, systems) {
if (err) {
debug('Error reregistering MQ services: ' + err);
return;
}

systems.forEach(function(system) {
registerMQServicesInSystem(system, mqserv);
});
});
}
});
}

function deregisterMQServicesByInfo(mqinfo, cb) {
if (!mqinfo) {
return;
}

let mqserv = {
basePath: `/mq/${mqinfo.manager}/${mqinfo.reqQueue}`,
rrpairs: [{}]
};
virtual.deregisterService(mqserv);

setTimeout(function() {
cb(mqinfo);
}, 300);
}

module.exports = {
registerMQService: registerMQService,
deregisterMQService: deregisterMQService,
registerAllMQServices: registerAllMQServices
};
45 changes: 45 additions & 0 deletions controllers/mqInfoController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const request = require('request');
const debug = require('debug')('default');
const mockiatoJmsUri = process.env.MOCKIATO_JMS_URI;

function getMQInfo(req, res) {
if (!mockiatoJmsUri) {
debug('MOCKIATO_JMS_URI not set');
handleError('Could not retrieve MQ info', res, 500);
return;
}

request(mockiatoJmsUri + '/env', function(err, resp, body) {
if (err) {
debug(err);
handleError('Could not retrieve MQ info', res, 500);
return;
}

let data = parseInfo(body);

res.json(data);
});
}

function parseInfo(body) {
let obj = JSON.parse(body);
let query = process.env.MOCKIATO_JMS_QUERY;
let info = unflattenObject(obj[query]);

if (!info || !info.mockiato) {
return { msg: 'Could not retrieve MQ info' };
}
let final = info.mockiato.mq;

final.defaults = {
manager: process.env.DEFAULT_QUEUE_MANAGER,
reqQueue: process.env.DEFAULT_REQUEST_QUEUE
};

return final;
}

module.exports = {
getMQInfo: getMQInfo
};
Loading

0 comments on commit 7f45a32

Please sign in to comment.