Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.96k stars 3.84k forks source link

Change stream - error MongoError: Timed out waiting for connection #9562

Closed Karman40 closed 3 years ago

Karman40 commented 3 years ago
I'm submitting a...
[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[x] Support request

Environment

"@nestjs/cli": "7.5.1",
"@nestjs/common": "^7.0.0",
"@nestjs/config": "^0.5.0",
"@nestjs/core": "^7.0.0",
"@nestjs/mongoose": "^7.1.0",

Source code:

app.module.ts:

MongooseModule.forRoot(process.env.APP_MONGO_URL,
      {
        useFindAndModify: false,
        useUnifiedTopology: true,
      }
),

stream.ts:

    const stream = this.usersDB.watch([], {fullDocument: 'updateLookup', maxAwaitTimeMS: 500});
    stream.on('change', async (change) => {

      try {

        // deleted user data
        if (change.operationType === 'delete') {
          await this.algolia.userDelete(change.documentKey._id.toHexString());
          this.logger.log(`user ${change.documentKey._id.toHexString()} delete successfull`);
        }

        // create or update
        if (change.operationType === 'update' || change.operationType === 'insert' || change.operationType === 'replace') {
          await this.algolia.userSync(change.documentKey._id.toHexString(), change.fullDocument);
          this.logger.log(`user ${change.documentKey._id.toHexString()} sync successfull`);
        }

      } catch (e) {
        this.logger.error(e);
      }

    });
    stream.on('error', (error) => {
      console.log('error', error);
    })
    stream.on('end', () => {
      console.log('end');
    })
    stream.on('close', () => {
      console.log('close');
    })

Error log:

error MongoError: Timed out waiting for connection
    at Timeout._onTimeout (D:\nestJS\organza-backend\node_modules\mongodb\lib\change_stream.js:475:23)
    at listOnTimeout (internal/timers.js:554:17)
    at processTimers (internal/timers.js:497:7)

some times:

events.js:291
      throw er; // Unhandled 'error' event
      ^

MongoError: Timed out waiting for connection

image

message:

I can't catch the error message. I don’t know if it’s a bug or just I don’t understand. Regardless, it works flawlessly. What should I do to prevent the error message from appearing? @nestjs/mongoose issues #650

Thank you in advance for your help!

vkarpov15 commented 3 years ago

I think you would be better served opening an issue with @nestjs/mongoose, we aren't familiar enough with how that module works to debug.

My best guess is that Mongoose is failing to connect. Can you try running await mongoose.connect(process.env.APP_MONGO_URL, { useFindAndModify: false, useUnifiedTopology: true }) and see what result you get?

Also, are you connecting to MongoDB Atlas, a local database, or a remote database?

Karman40 commented 3 years ago

@vkarpov15 Thanks your answer!

First, i open issues in @nestjs/mongoose issues #650, but thay said: "So please, report this in mongoose repository https://github.com/Automattic/mongoose"

I connenct to local database.

WIN 10, MongoDB server 4.2 Yesterday i upgraded to MongoDB to MongoDB Enterprise 4.4 but since then I haven't had time to test it.

Also use this, in Ubuntu 20.04 LTS VPS server, but i haven't looked at the bug here yet. Despite the error, all processes run flawlessly and work, only confusing the error message in the console.

I started the dev server and will report with experience in a few hours.

Karman40 commented 3 years ago

After then i update to 4.4 the bug is solved.

Tanks for your help!