Mongoose/MongoDB in Node.JS stops returning records after a while
Asked Answered
G

1

11

I have a script running which finds data in a database I have. For some reason after a short period of time, it stops finding any data but does not output any errors to the screen or through node.js. Here is the following setup I have for mongoose. I have a database at MLAB btw!

var connectionOptions = {
    server: {
        auto_reconnect: true,
        poolSize: 16,
        socketOptions: {
            keepAlive: 120,
            connectTimeoutMS: 5 * 60 * 60 * 1000,
            socketTimeoutMS: 5 * 60 * 1000
        }
    },
    replset: {
        poolSize: 16,
        socketOptions: {
            connectTimeoutMS: 300000, // 5 minutes
            keepAlive: 120
        },
        ha: true, // Make sure the high availability checks are on
        haInterval: 5000, // Run every 10 seconds
    }
};

//mongoose.set('debug', true);
mongoose.connect('mongodb://[email protected]:57328,XXXXXXXX.mlab.com:57328/XXXXXXXXX?replicaSet=rs-XXXXXX', connectionOptions, function (err) {
    if (err) console.log(err);
});

mongoose.connection.on('connecting', function () {
    console.log('Connecting to MongoDB...');
});

mongoose.connection.on('connected', function () {
    console.log('MongoDB connected!');
});

mongoose.connection.on('open', function () {
    console.log('MongoDB connection opened!');
});

mongoose.connection.on('error', function (err) {
    console.log(err);
    mongoose.disconnect();
});

mongoose.connection.on('disconnected', function () {
    console.log('MongoDB disconnected!');
    mongoose.connect('mongodb://[email protected]:57328,XXXXXXXX.mlab.com:57328/XXXXXXXXX?replicaSet=rs-XXXXXX', connectionOptions, function (err) {
        if (err) console.log(err);
    });
});

mongoose.connection.on('reconnected', function () {
    console.log('MongoDB reconnected!');
});

mongoose.connection.on('close', function () {
    console.log.error('MongoDB closed');
});

mongoose.connection.on('ha', function(type, data) {
    console.log('replset ha ' + type);
});

mongoose.connection.on('timeout', function () {
    console.log.error('MongoDB timeout');
});

I have seen a lot of configurations and I've tried a ton of different things but nothing seems to work.

I would really appreciate the help, as I've been researching around for quite a bit to no luck.

And to recap again, I have a script running querying records from a database, and after a certain time, the script will keep running but seem to not find any new records and stop responding to any new data.

Guillemot answered 23/5, 2017 at 20:18 Comment(16)
I'm facing the same issue and still can't find a way to fix it. I have a linux CRON calling an endpoint that searches for data in Mongo every 15 minutes. After a couple of hours it simply stops returning data even though I can manually execute that query on the shell and get results. I'm running crazy already.Wareing
So this happens just for cron or when you normally run the script as well?Historic
It only happens in my node API, which is currently using the MongoDB node JS driver. The cron keeps calling an endpoint from this API. After a few hours it stops working, but I can still copy the query to a mongoDB shell and it will return what I'm expecting. As soon as I restart my API the endpoint starts working again until a few hours go by and the issue happens again. More details here: groups.google.com/forum/#!topic/mongodb-user/LkJV0ARHbV8Wareing
are you deployed to heroku or something? Could this be related to a cold-start issue where it not-responding is actually the server spinning up dynamically?Foretoken
Deployed on AWS EC2. Like explained in the link above everything works fine besides getting the data out of my aggregation call. The same query will return nothing after a few hours of server uptime but when copied to the Mongo shell it returns everything that I expect. If I restart the server, the query works again but after a few hours the issue shows up again.Wareing
@RodolfoPerottoni do you see any error in the mongod logs, or is there any error printed by the application? I assume the endpoint keeps a database connection open for ~5 hours and it's possible that it's timing out on somethingImmediacy
@KevinAdistambha I can't check for logs because I'm using MongoDB Atlas. Yes, my endpoints share the same connection which is kept alive 24/7, but only my cron endpoints are failing after a certain period of time. I have ~100 other endpoints in the same webservice being used at the same time and they all work flawlessly.Wareing
@RodolfoPerottoni which Atlas instance are you using? Is it the free tier or the paid tier?Immediacy
@KevinAdistambha it's the free tierWareing
@RodolfoPerottoni could you take a look at Atlas Free Tier limitations and Atlas Free Tier unsupported commands? Maybe your constant monitoring is hitting some free tier limits. Actually, if the cron job works if you test it using a local deployment, there's a high chance that the issue is due to some free tier limits.Immediacy
@KevinAdistambha I've upgraded my server to an M10 to test it out and the issue remains. I swear I read the entire MongoDB documentation and there's nothing that explains this behavior. For the moment, I've split my aggregation into multiple find calls to solve my issue. I called MongoDB and the solution they gave me was to buy an Enterprise plan to get this issue analysed by an "expert". What a joke.Wareing
@RodolfoPerottoni I have posted a series of questions in groups.google.com/forum/#!topic/mongodb-user/LkJV0ARHbV8. Let's continue the conversation in that thread instead.Immediacy
Is it happen on a local Mongo Db also? or only when you deploy your app?Bonded
Possibility : Looks like your query is a bit expensive. Pipeline stages in aggreagte query have a limit of 100 megabytes of RAM. You need to use allowDiskUse and set it to true to verify the issue. docs.mongodb.com/manual/core/aggregation-pipeline-limits/…Euratom
If your app is deployed on heroku's free dinos, it's put to sleep after 20 minutes, I used to face a similar problem and that was the reason.Micra
did you ever find a solution on this problem? I think I might be facing a similar situationYonne
T
0

Not sure about your configurations there but you can try mine:

  mongoose.connect(
  process.env.DB_CONNECT,
  {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useCreateIndex: true,
  },
  () => {
    console.log("Connected To MongoDB");
  }
);
Tullis answered 8/11, 2021 at 22:28 Comment(1)
I can vouch for this, I have the same config (except I don't use useCreateIndex) and it works just fineGriffen

© 2022 - 2024 — McMap. All rights reserved.