Node server intermittently going down

I’m running a node server on fly.io and a mongodb on Atlas and twice in the last week it has crashed with the following error:

2024-01-05T17:19:33.060 app[4d89603c291487] sjc [info] Exception in getSubjects: 103 | * not subject to semantic versioning compatibility guarantees and may change at any time.

2024-01-05T17:19:33.060 app[4d89603c291487] sjc [info] 104 | *

2024-01-05T17:19:33.060 app[4d89603c291487] sjc [info] 105 | * @public

2024-01-05T17:19:33.060 app[4d89603c291487] sjc [info] 106 | **/

2024-01-05T17:19:33.060 app[4d89603c291487] sjc [info] 107 | constructor(message, options) {

2024-01-05T17:19:33.060 app[4d89603c291487] sjc [info] 108 | super(message, options);

2024-01-05T17:19:33.060 app[4d89603c291487] sjc [info] ^

2024-01-05T17:19:33.060 app[4d89603c291487] sjc [info] error: connection <monitor> to 54.81.181.174:27017 closed

2024-01-05T17:19:33.060 app[4d89603c291487] sjc [info] code: "undefined"

2024-01-05T17:19:33.060 app[4d89603c291487] sjc [info] at new MongoError (/app/node_modules/mongodb/lib/error.js:108:9)

2024-01-05T17:19:33.060 app[4d89603c291487] sjc [info] at new MongoSystemError (/app/node_modules/mongodb/lib/error.js:944:13)

2024-01-05T17:19:33.060 app[4d89603c291487] sjc [info] at new MongoServerSelectionError (/app/node_modules/mongodb/lib/error.js:979:9)

2024-01-05T17:19:33.060 app[4d89603c291487] sjc [info] at /app/node_modules/mongodb/lib/sdam/topology.js:276:34

2024-01-05T17:19:33.060 app[4d89603c291487] sjc [info] error: connection <monitor> to 54.81.181.174:27017 closed

Seems to suggest a connection error to the db but I don’t know what the problem is. I restarted the fly.io machine and site is back up and running. Didn’t need to do anything but restart the machine.

Two questions:

  1. What could be causing this intermittent issue?
  2. Is there anyway to tell the server to auto restart itself if it encounters it again?

@Rooster242 Do you have the code public in GitHub? That or a snippet where you’re doing the DB connection? It seems like something is off with the connection to the DB there.

I’m using mongoose so it’s just:
import mongoose from "mongoose";
await mongoose.connect(process.env.DB_URI);
where:
DB_URI=mongodb+srv://<user>:<password>@cluster0.vrzxfmm.mongodb.net/<app>?retryWrites=true&w=majority

@Rooster242 Hi,

You may have truncated your code/answer however if not, I’d recommend wrapping the code in try/catch. That lets you log the error to see what’s going on, but also it will catch the error and so the whole Node process/server won’t crash on an exception. That seems to be what’s happening.

There’s an example here

… which uses a slightly different approach but generally you do:

try {
   // your code here
} catch (err) {
   // log the error etc (probably with a logging library)
   console.error(err);
}

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.