javascriptnode.jspm2

Why does my bare-minimum app keeps restarting which was start with pm2?


I have a bare-minium app with only app.js, ecosystem and package.json file and nothing else. The app.js file contains only this:

class App {
    async init() {
        console.log('App started started');
    }
}

const app = new App();

app
    .init()
    .then()
    .catch(err => console.log('Error initializing app: ', err));

And the ecosystem file contains this:

module.exports = {
    apps: [
        {
            name: 'main-app',
            script: './app.js',
            output: "./logs/app-out.log",
            error: "./logs/app-error.log",
            watch: false
        }
    ]
};

After I start the pm2 process with pm2 start, app keeps restarting. What could be the reason? I was facing this restart issue in my other app and so for testing I created this dummy, bare-minimum app and apparently it is happening here as well. Can someone shed some light?


Solution

  • Your code is evaluated and the app exits after

    app
        .init()
        .then()
        .catch(err => console.log('Error initializing app: ', err));
    

    The event loop is empty. pm2 restarts the application. The default restart strategy is

    When starting application with PM2, applications are automatically restarted on auto exit, event loop empty (node.js) or when application crash.

    https://pm2.keymetrics.io/docs/usage/restart-strategies/