Skip to content

How to properly handle watcher errors/disconnection #14312

Closed

Description

Prerequisites

  • I have written a descriptive issue title

Mongoose version

8.0.3

Node.js version

20.10.0

MongoDB version

6.0.13 (atlas server) & 6.2.0 (node)

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

Debian Bullseye 11

Issue

Hello, for some time now, some of my watchers have been disconnecting and not reconnecting, resulting in events not being received. I'm unable to reproduce the problem effectively locally, but once in production it often happens:

  1. I start my NodeJS service
  2. I start my watchers
  3. I log each insertion event
  4. After a while (sometimes 1 month or 10 days or less) I don't receive any more insertion events even though I'm creating documents.

Here is my current code :

const MyMongooseModel = mongoose.model('ModelName', mySchema);

MyMongooseModel.watch([{ $match: { operationType: "insert" } }])
     .on("change", async data => {
          console.log(data);
      })
      .on("error", error => {
            console.error("Error on log watch stream", error);
      });

--> How can I efficiently detect when watchers are completely closed in order to restart it ?

N.B. I've tried listening for the "close" event but sometimes it's sent but events are always received and sometimes afterwards there are no more events received.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    helpThis issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessaryhelp wanted

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions