Advertisement
Guest User

Untitled

a guest
May 3rd, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. app.configure(function configureMongoDbConnection() {
  2. var connectionString = 'mongodb://localhost:27017/test',
  3. sleep = 1000,
  4. timer;
  5.  
  6. db.on('connecting', function() {
  7. console.log('connecting to MongoDB...');
  8. });
  9.  
  10. db.on('error', function(error) {
  11. console.error('Error in MongoDb connection: ' + error);
  12. mongoose.disconnect();
  13. });
  14.  
  15. db.on('connected', function() {
  16. console.log('MongoDB connected!');
  17. });
  18.  
  19. db.once('open', function() {
  20. clearTimeout(timer);
  21. connect.time = 0;
  22. console.log('MongoDB connection opened!');
  23. });
  24.  
  25. db.on('reconnected', function () {
  26. console.log('MongoDB reconnected!');
  27. });
  28.  
  29. db.on('disconnected', function() {
  30. console.log('MongoDB disconnected!');
  31. connect.time += sleep;
  32. timer = setTimeout(connect, sleep);
  33. });
  34.  
  35. function connect() {
  36. if(connect.time > 22000) {
  37. console.error('giving up on db connection');
  38. return;
  39. }
  40. mongoose.connect(connectionString, {server:{auto_reconnect:true}});
  41. }
  42.  
  43. connect.time = 0;
  44. connect();
  45. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement