petruisfan / node-supervisor

Other
3.76k stars 260 forks source link

Cannot close app gracefully with supervisor #96

Open binarykitchen opened 11 years ago

binarykitchen commented 11 years ago

Hello Isaacs

Somehow the supervisor is giving me headaches with my own shutdown script. It never calls the timeout below:

    var graceTimeout;
    var gracefullyClosing = false;

    if (app.get('env') == 'development') {
        graceTimeout = 100;
    } else {
        graceTimeout = app.get('graceTimeout');
    }

    app.use(function (req, res, next) {
        if (gracefullyClosing) {
            console.info('gracefully refused a connection.');

            res.setHeader('Connection', 'close');
            res.send(502, 'server is in the process of restarting');
        } else
            next();
    });

    var closeGracefully = function(signal) {
        gracefullyClosing = true;

        console.info(
            'received signal (%s) on %s, shutting down gracefully in %s ms',
            signal,
            new Date().toString('T'),
            graceTimeout
        );

        setTimeout(function() {
            console.info(
                '(x) forcefully shutting down',
                graceTimeout
            );
            process.exit();
        }, graceTimeout);

        server.close(function() {
            console.info('all connections are ended');
        });
    };

    var signals = ['SIGINT', 'SIGTERM', 'SIGQUIT'];
    for (i in signals) {
        process.on(signals[i], function() {
            closeGracefully(signals[i]);
        });
    }

for production env the console shows: received signal (SIGQUIT) on Fri Mar 15 2013 17:39:14 GMT+1300 (NZDT), shutting down gracefully in 10000 ms

but that's all. settimeout '(x) forcefully shutting down' is never called. is that because the supervisor is exiting before? it so, how come it's not reloading?

thanks for your help, michael

binarykitchen commented 11 years ago

this issue is really bugging me why the timeout is never called ...

iangreenleaf commented 11 years ago

If you run this code without supervisor, does it work as intended?

When running it with supervisor, what happens? Does the process exit? I can't see any reason supervisor would be interfering with setTimeout. If supervisor is restarting the process, it will spit out some debugging data: crashing child... etc.

binarykitchen commented 11 years ago

You are right, silly me. It has nothing to do with the supervisor. The timeout is also never called without using the supervisor.

binarykitchen commented 10 years ago

This PR will add this feature :)

https://github.com/isaacs/node-supervisor/pull/132