node-supervisor icon indicating copy to clipboard operation
node-supervisor copied to clipboard

Cannot close app gracefully with supervisor

Open binarykitchen opened this issue 12 years ago • 4 comments

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 avatar Mar 15 '13 04:03 binarykitchen

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

binarykitchen avatar Mar 16 '13 04:03 binarykitchen

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.

iangreenleaf avatar Mar 17 '13 22:03 iangreenleaf

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

binarykitchen avatar Mar 18 '13 01:03 binarykitchen

This PR will add this feature :)

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

binarykitchen avatar May 30 '14 03:05 binarykitchen