4

I'm struggling to understand behavior I'm seeing when using grunt-concurrent and trying to handle SIGINT, and hope someone can shed some light on it. Basically: when I use a SIGINT handler within my function(grunt) { } declaration, why does only one of the concurrent tasks see SIGINT?

The brief setup: I'm using grunt-concurrent to fire off watch and nodemon tasks; the exact concurrent stanza of my Gruntfile.js file is:

concurrent: {
    dev: {
        options: {
            logConcurrentOutput: true
        },
        tasks: [ 'watch', 'nodemon:dev' ]
    }
}

I wanted to watch for SIGINT, so that when I hit Ctrl+C, I can clean up a few files that get copied as part of the watch task. I began by adding the following to the end of my Gruntfile.js, outside of the module.exports = function(grunt) { ... } function declaration:

process.on('SIGINT', function() {
    console.log('Cleaning up after the dev server...');
    console.log(JSON.stringify(process.argv));
});

When it's included there and I hit Ctrl+C, it runs three times — once for the main grunt task I ran (dev), and one for each of the two tasks that grunt-concurrent ran (watch and nodemon:dev). That's clearly not what I intended.

When I move the SIGINT handler within the function(grunt) { } method, then when I hit Ctrl+C, it only runs once — for the nodemon:dev task. What I can't understand is why I'm not seeing it run for the watch task too (and for the master concurrent task dev that fires both of them off!), and it's sorta killing me... mostly because I don't want to depend on this behavior when I can't understand it and can't be sure it won't be the same on all the different machines this app will end up on.

So... thoughts? What am I missing here? Thanks!

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
delfuego
  • 14,085
  • 4
  • 39
  • 39

0 Answers0