'Why Google Cloud Run gettings massive container restart / new instance creation?

I've been using Google Cloud Run for a year now and the issue with cloud run containers restarts / new container start is from the beginning.

I've hosted Node + MongoDB app in Cloud Run, but cloud run container is restarting frequently. It's getting around 10 - 12 requests / second, couldn't find any performance bottleneck, requests are serving smoothly, sometimes requests are served more than normal time, might be new container instance cold start delay.

Google Cloud Run Container Restart

The issue I am facing is the HIGH Number of connections to the MONGODB Server. After some research I could find that I've to close mongodb connection on node process exit so I've added a graceful shutdown function.

// Function to terminate the app gracefully:
const gracefulShutdown = async () => {
    console.log(`MONGODB CONNECTION CLOSED!`);
    await mongoose.connection.close();
};

// This will handle process.exit():
process.on('exit', gracefulShutdown);

// This will handle kill commands, such as CTRL+C:
process.on('SIGINT', gracefulShutdown);
process.on('SIGTERM', gracefulShutdown);

// This will prevent dirty exit on code-fault crashes:
process.on('uncaughtException', gracefulShutdown);

But even after adding this, I couldn't find this graceful shutdown function is invoked while checking logs.

  1. Does google cloud run really signals when the nodejs process in the container crashed?
  2. Is there any way to identity a container restart or new instance creation in cloud run?

Here is the MongoDB connection code

exports.connect = () => {
    try {
        mongoose
            .connect(MONGO.URI, {
                useCreateIndex: true,
                keepAlive: 1,
                useNewUrlParser: true,
                useUnifiedTopology: true,
                useFindAndModify: false,
            })
            .then((docs) => {
                console.log(`DB Connected`);
            })
            .catch((err) => {
                console.log(`err`, err);
            });
        return mongoose.connection;
    } catch (err) {
        console.log(`#### Error Connecting DB`, err);
        console.log(`Mongo URI: `, MONGO.URI);
    }
};

Sometimes cloud run issues a high number of connections to MONGODB, and hits the connection limit of 1500 connections.

Google Cloud Run MongoDB High Connection issue

Any suggestions are appreciated! I've been facing this issue for a year now.



Solution 1:[1]

You should not start the node process using npm or yarn but directly as CMD ["node", "index.js"] (when you are inside a Docker container using Dockerfile)

Explanation here https://maximorlov.com/process-signals-inside-docker-containers/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alberto Valero