Created
August 1, 2012 19:39
-
-
Save mpobrien/3230010 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongo = require('mongodb'); | |
var logger = require('logger'); | |
logger.debug = console.log | |
function connect_to_mongo( config_name, master_config, raw, on_connect ) { | |
if (raw){ | |
return create_connection( master_config[config_name]); | |
} | |
var mongo_wrapper = { | |
refresh_connection : function(on_refresh) { | |
logger.debug('Master config: ' + JSON.stringify(master_config)); | |
if( this.client ) this.client.close(); | |
var self = this; | |
this.client = create_connection( master_config[config_name], on_refresh); | |
}, | |
fetch_collection: function( collection_name, callback, action ) { | |
this.client.collection( collection_name, function( error, collection ) { | |
if( error ) { | |
this.check_for_dead_connection( error ); | |
callback( null, error ); | |
} else { | |
action( collection ); | |
} | |
}); | |
}, | |
check_for_dead_connection: function( error ) { | |
if( error_indicates_dead_connection( error ) ) | |
notify_of_dead_connection( error ); | |
function error_indicates_dead_connection( error ) { | |
if( !error ) return false; | |
return error.message.indexOf( 'no open connections') || error.message.indexOf( 'connection closed' ); | |
} | |
function notify_of_dead_connection( error ) { | |
logger.error( error.stack ); | |
var mailer = require('mailer'); | |
var smtp_config = require('config-loader')(__dirname, '/../../nedules/config/smtp'); | |
mailer.send({ host: smtp_config.host, | |
port: smtp_config.port, | |
domain: '[127.0.0.1]', | |
to: smtp_config.lander.to, | |
from: smtp_config.lander.to, | |
subject: active_environment() + ' Dead Mongo Connection', | |
body: error.stack}, | |
function(err){ | |
if(err) { | |
logger.error( 'Failed to send dead mongo email: ' + err.stack ); | |
} | |
}); | |
} | |
}, | |
ObjectID : mongo.ObjectID | |
}; | |
mongo_wrapper.refresh_connection(on_connect); | |
return mongo_wrapper; | |
}; | |
module.exports.connect_to_mongo = connect_to_mongo | |
function create_connection( config, on_connect ){ | |
logger.debug('Connecting to mongo with server options: ' + JSON.stringify( config ) ); | |
var connection_handlers = { 'standalone': open_standalone_connection, 'replicaset': open_replicaset_connection }; | |
var db = connection_handlers[config['configuration_type']]( config[config['configuration_type']] ); | |
db.open(function(error, cb){ | |
console.log("here",error, cb); | |
if(error) { | |
console.log( "Failed to connect to " + config['configuration_type'] + " mongo: " + error.message ); | |
throw error; | |
} | |
if( on_connect ) on_connect(); | |
}); | |
return db; | |
function open_standalone_connection( config ) { | |
logger.debug('Connecting to standalone mongo with server options: ' + JSON.stringify( config ) ); | |
return new mongo.Db( config.db_name, new mongo.Server( config.host, config.port, config.options )); | |
} | |
function open_replicaset_connection( config ) { | |
logger.debug('Connecting to replicaset mongo with server options: ' + JSON.stringify( config ) ); | |
var servers= []; | |
for( var index in config['servers'] ) | |
servers.push( new mongo.Server( | |
config['servers'][index]['host'], | |
config['servers'][index]['port'], | |
config['servers'][index]['options'] | |
)); | |
var replSet = new mongo.ReplSetServers( servers, {rs_name:config['options']['rs_name']} ); | |
return new mongo.Db( config.db_name, replSet) ; | |
} | |
} | |
function active_environment() { | |
if( !process.argv || process.argv.length < 3 ) | |
return process.env.NODE_ENV; | |
for( var index = 2; index < process.argv.length; index++ ) { if( process.argv[index].indexOf('env=') === 0 ) return process.argv[index].substr(4); } | |
return process.env.NODE_ENV; | |
} | |
bc = { | |
auth:{ | |
configuration_type: 'replicaset', | |
replicaset: { | |
db_name: 'auth', | |
servers: [ | |
{host:'localhost',port:4000,options:{auto_reconnect:true}}, | |
{host:'localhost',port:4001,options:{auto_reconnect:true}} | |
,{host:'localhost',port:4002,options:{auto_reconnect:true}} | |
], | |
options: { | |
rs_name: 'replset0', | |
read_secondary: true, | |
socketOptions: { timeout: 0, keepAlive: 1 } | |
} | |
}, | |
standalone: { | |
db_name:'auth', | |
host:'UAT-NJ-MONGO-01', | |
port:36108, | |
options:{ | |
auto_reconnect: true, | |
poolSize: 3, | |
socketOptions: { keepAlive: 1 } | |
} | |
} | |
}, | |
session:{ | |
configuration_type: 'standalone', | |
replicaset: { | |
db_name: 'sessions', | |
servers: [ | |
{host:'UAT-NJ-MONGO-01',port:36108,options:{auto_reconnect:true}}, | |
{host:'UAT-NJ-MONGO-02',port:36108,options:{auto_reconnect:true}} | |
,{host:'UAT-NJ-MONGO-03',port:36108,options:{auto_reconnect:true}} | |
], | |
options: { | |
rs_name: 'LANDER', | |
read_secondary: true, | |
socketOptions: { timeout: 0, keepAlive: 1 } | |
} | |
}, | |
standalone: { | |
db_name:'sessions', | |
host:'UAT-NJ-MONGO-01', | |
port:36108, | |
options:{ | |
auto_reconnect: true, | |
poolSize: 3, | |
socketOptions: { keepAlive: 1 } | |
} | |
} | |
}, | |
sso:{ | |
configuration_type: 'replicaset', | |
replicaset: { | |
db_name: 'sso', | |
servers: [ | |
{host:'UAT-NJ-MONGO-01',port:36108,options:{auto_reconnect:true}}, | |
{host:'UAT-NJ-MONGO-02',port:36108,options:{auto_reconnect:true}} | |
,{host:'UAT-NJ-MONGO-03',port:36108,options:{auto_reconnect:true}} | |
], | |
options: { | |
rs_name: 'LANDER', | |
read_secondary: true, | |
socketOptions: { timeout: 0, keepAlive: 1 } | |
} | |
}, | |
standalone: { | |
db_name:'sso', | |
host:'UAT-NJ-MONGO-01', | |
port:36108, | |
options:{ | |
auto_reconnect: true, | |
poolSize: 3, | |
socketOptions: { keepAlive: 1 } | |
} | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment