0

I have a simple webapp using node.js that reads the database and displays this data to the user. There is this issue where after the app isn't actively used, when viewed initially the connection errors out with a [Error: connection closed]. After reloading the connection seems to be ok, though it does seem to bomb randomly after it first happens and you are clicking around (which I'm pretty sure is one of the workers dying from a bad connection and restarting). Here's pretty much all the important bits of the code...

//POSTPROVIDER.JS
var mongoose = require('mongoose');
var async = require('async');

var settings = require('./settings.js').mongodb;
if(!settings) {
  console.log('Could not connect to MongoDB server - missing MongoDB settings');
}

var connectString = "mongodb://" +
  settings.username + ":" + settings.password + "@" +
  settings.hostname + ":" + settings.hostport + "/" +
  settings.database;


mongoose.connect(connectString);

mongoose.connection.on('error', function(error) {
  console.log(error)
  mongoose.connect(connectString);
})

var Schema = mongoose.Schema;

var FeedbackSchema  = new Schema({
  //Schema
});

var feedbackDoc = mongoose.model('Feedback', FeedbackSchema);

//Example query
PostProvider.prototype.findByUuid = function(_id, callback) {

  feedbackDoc.findById( _id).exclude('screenshot').exec(function (err, feedback) {
    if (err) return callback(err);

    var feedbacks = JSON.stringify(convertFeedback(feedback), null, '    ');

    callback( null, feedbacks )
  });

};

exports.PostProvider = PostProvider;

And the server code...

//APP.JS    
var express = require('express');
var request = require('request');
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;

var app = module.exports = express.createServer();

// Express Config
var pub = __dirname + '/public';

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'ejs');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.compiler({ src: pub, enable: ['sass'] }));
  app.use(express.static(pub));
  app.use(app.router);
});

var error = require('./error');

app.configure('development', function(){
  app.use(error({ showMessage: true, dumpExceptions: true, showStack: true, logErrors: __dirname + '/log/error_log' }));
});

app.configure('production', function(){
  app.use(error({ showMessage: true, dumpExceptions: true, showStack: true, logErrors: __dirname + '/log/error_log' }));
});

var PostProvider = require('./postprovider').PostProvider;
var PostProvider= new PostProvider();


if (cluster.isMaster) {
  // Fork workers.

  console.log("Master is running.  [ %d ]", process.pid );
  for (var i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on('death', function(worker) {
    console.log('worker ' + worker.pid + ' died');
    console.log(worker)
    cluster.fork();
  });
} else {
  // Worker processes have a http server.();
    console.log("Worker is running.  [ %d ]", process.pid );

  //Main View
  app.get('/', function(req, res){

    var pageNum = 1;

      res.render('layout', {
            locals: {
              title: 'View Portal',
              pageNum: pageNum
            }
      });
  });

  app.listen(8900);
  console.log("Express server listening on port 8900");    
}

Is there a way to check the status of the connection, and if so a way to reconnect correctly before processing a query?

4

1 回答 1

0

使用新版本的 Node,必须重写相关应用程序。回答我自己的问题以结束这个问题。

于 2012-07-05T00:31:23.117 回答