Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoClient Updates #54

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var ProcessManager = exports.ProcessManager = function(options){

self._persistency = null;
if (options.persistencyOptions) {
self._persistency = new Persistency(options.persistencyOptions);
self._persistency = new Persistency(options.persistencyOptions.url, options.persistencyOptions.options);
self._doneLoadingHandler = options.persistencyOptions.doneLoading;
self._doneSavingHandler = options.persistencyOptions.doneSaving;
}
Expand Down Expand Up @@ -640,4 +640,4 @@ ProcessManager.prototype.getDefinitionNames = function(callback){
*/
ProcessManager.prototype.createServer = function(options, restifyOptions){
return rest.createServer(this, options, restifyOptions);
};
};
52 changes: 24 additions & 28 deletions lib/persistency/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@ var EventEmitter = require('events').EventEmitter;

// details see http://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html
var defaultOptions = {
"server": {
// if we set this to true, we will try to reconnect even if the url is wrong.
// So we can never close the connection if something went wrong
// If this were true, the connectionNOK test will fail.
"auto_reconnect": false,
"poolSize": 10,
"socketOptions": {
"connectTimeoutMS": 5000
}
}
// if we set this to true, we will try to reconnect even if the url is wrong.
// So we can never close the connection if something went wrong
// If this were true, the connectionNOK test will fail.
"autoReconnect": false,
"poolSize": 10,
"socketTimeoutMS": 5000
};

/*
Expand All @@ -31,24 +27,24 @@ var waitingForConnectionEventEmitters = {};
var connectionEventName = 'connectionEventName';

/**
* @param {String} uri
* @param {String} url
* @param {*} options
* @constructor
*/
var Persistency = exports.Persistency = function(uri, options) {
var Persistency = exports.Persistency = function(url, options) {
this.options = options || defaultOptions;
this.options.server = this.options.server || defaultOptions.server;
Object.assign(this.options, this.options.server || defaultOptions.server)

if (this.options.logger) {
this._trace = this.options.logger.trace || function() {};
} else {
this._trace = function() {};
}

if (uri) {
this.uri = uri;
if (url) {
this.url = url;
} else {
throw new Error("MongoDB: Persistency: requires uri to db");
throw new Error("MongoDB: Persistency: requires url to db");
}
};

Expand Down Expand Up @@ -110,11 +106,11 @@ Persistency.prototype._execute = function(dbCall, argument, done) {
} else {
waitingForConnection = self._waitingForConnection();
if (waitingForConnection) {
self._trace("Waiting for connection. URI: " + self.uri);
self._trace("Waiting for connection. URL: " + self.url);
waitingForConnection.on(connectionEventName, function(error, db) {
if (error) {
self._resetConnection();
self._trace("ERROR: Stopped waiting for connection. URI: " + self.uri + " Error: " + error);
self._trace("ERROR: Stopped waiting for connection. URL: " + self.url + " Error: " + error);
done(error);
} else {
self._trace("Stopped waiting. Got connection '" + db.databaseName + "'.");
Expand All @@ -123,12 +119,12 @@ Persistency.prototype._execute = function(dbCall, argument, done) {
});
} else {
self._setWaitingForConnection();
self._trace("Trying to get connection for URI: " + self.uri + " ...");
self._trace("Trying to get connection for URL: " + self.url + " ...");
self._connect(function(error, db) {
var waitingForConnection = self._waitingForConnection();

if(error) {
self._trace("ERROR: Could not get connection. URI: " + self.uri + " Error: " + error);
self._trace("ERROR: Could not get connection. URL: " + self.url + " Error: " + error);
waitingForConnection.emit(connectionEventName, error); // we do this to stop waiting
self._resetConnection();
if (db){
Expand All @@ -139,7 +135,7 @@ Persistency.prototype._execute = function(dbCall, argument, done) {
done(error);
}
} else {
self._trace("Got connection '" + db.databaseName + "' URI: " + self.uri);
self._trace("Got connection '" + db.databaseName + "' URL: " + self.url);
self._setConnection(db);
waitingForConnection.emit(connectionEventName, null, db);
dbCall.call(self, db, argument, done);
Expand All @@ -150,46 +146,46 @@ Persistency.prototype._execute = function(dbCall, argument, done) {
};

Persistency.prototype._connect = function(done) {
MongoClient.connect(this.uri, this.options, done);
MongoClient.connect(this.url, this.options, done);
};

/**
* @returns {*}
* @private
*/
Persistency.prototype._getConnection = function() {
return uniqueDbConnections[this.uri];
return uniqueDbConnections[this.url];
};

/**
* @param {*} connection
* @private
*/
Persistency.prototype._setConnection = function(connection) {
uniqueDbConnections[this.uri] = connection;
uniqueDbConnections[this.url] = connection;
};

/**
* @returns {EventEmitter}
* @private
*/
Persistency.prototype._waitingForConnection = function() {
return waitingForConnectionEventEmitters[this.uri];
return waitingForConnectionEventEmitters[this.url];
};

/**
* @private
*/
Persistency.prototype._setWaitingForConnection = function() {
waitingForConnectionEventEmitters[this.uri] = new EventEmitter();
waitingForConnectionEventEmitters[this.url] = new EventEmitter();
};

/**
* @private
*/
Persistency.prototype._resetConnection = function() {
delete uniqueDbConnections[this.uri];
delete waitingForConnectionEventEmitters[this.uri];
delete uniqueDbConnections[this.url];
delete waitingForConnectionEventEmitters[this.url];
};

/**
Expand Down
13 changes: 6 additions & 7 deletions lib/persistency/persistency.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@ var MongoDBPersistency = require('./mongodb.js').Persistency;
* @param {{uri: String, uri: String}} options
* @constructor
*/
var Persistency = exports.Persistency = function(options) {
var Persistency = exports.Persistency = function(url, options) {
var isMongoDbUri;
var uri = options ? options.uri : null;

if (uri) {
isMongoDbUri = uri.indexOf('mongodb://') === 0;
if (url) {
isMongoDbUri = url.indexOf('mongodb://') === 0;
if (isMongoDbUri) {
this.implementation = new MongoDBPersistency(uri, options);
this.implementation = new MongoDBPersistency(url, options);
} else {
this.implementation = new FilePersistency(uri);
this.implementation = new FilePersistency(url);
}
} else {
throw new Error("Persistency options must contain an uri property.");
throw new Error("Persistency options must contain a uri property.");
}
};

Expand Down
18 changes: 6 additions & 12 deletions test/public/manager/persistency.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ require("../../../lib/history.js").setDummyTimestampFunction();
exports.testCreatePersistentFlatProcess = function(test) {
var bpmnProcess;

var persistencyUri = path.join(__dirname, '../../resources/persistency/testPersistentProcess');
fileUtils.cleanDirectorySync(persistencyUri);
var persistencyURL = path.join(__dirname, '../../resources/persistency/testPersistentProcess');
fileUtils.cleanDirectorySync(persistencyURL);

var savedState = function(error, savedData) {
test.ok(error === null, "testCreatePersistentFlatProcess: no error saving.");
Expand Down Expand Up @@ -95,11 +95,10 @@ exports.testCreatePersistentFlatProcess = function(test) {
var fileName = path.join(__dirname, "../../resources/projects/simple/taskExampleProcess.bpmn");

var persistencyOptions = {
uri: persistencyUri,
doneSaving: savedState
};

var manager = new Manager({
var manager = new Manager(persistencyURL, {
bpmnFilePath: fileName,
persistencyOptions: persistencyOptions
});
Expand All @@ -116,17 +115,12 @@ exports.testCreatePersistentFlatProcess = function(test) {

exports.testLoadPersistentFlatProcess = function(test){

var persistencyUri = path.join(__dirname, '../../resources/persistency/testPersistentProcess');
var persistencyURL = path.join(__dirname, '../../resources/persistency/testPersistentProcess');

var fileName = path.join(__dirname, "../../resources/projects/simple/taskExampleProcess.bpmn");

var persistencyOptions = {
uri: persistencyUri
};

var manager = new Manager({
bpmnFilePath: fileName,
persistencyOptions: persistencyOptions
var manager = new Manager(persistencyURL, {
bpmnFilePath: fileName
});


Expand Down