gruntjs/grunt-contrib-watch — pull request #155 by blai (2013-07-13 07:37:16+00:00)
Title: `interrupt` should work more than once

Found that the current implementation of `interrupt` only work once, second time onward the `running` of Runner would be `false` (https://github.com/gruntjs/grunt-contrib-watch/blob/master/tasks/lib/taskrunner.js#L152). I believe the problem lies in `TaskRun.run()` calling `done()` upon all completion signal (https://github.com/gruntjs/grunt-contrib-watch/blob/master/tasks/lib/taskrun.js#L63), as in the case of `interrupt`, `TaskRun.complete()` would be triggered prior to actual completion of the "interrupted" task (https://github.com/gruntjs/grunt-contrib-watch/blob/master/tasks/lib/taskrun.js#L73). I am not sure if this is the right fix, but tested to work (see updated unit test).

The actual use case is something similar to the following:

The example use `grunt-contrib-watch` to monitor an `express.js` server file, restarts the server whenever it changes, it also uses `atBegin` to start the server initially. Noticed first time updates the `./server.js`, the server restarted, but second time (or onward) changing `./server.js` will result in error `EADDRINUSE` since `interrupt()` was never triggered.

```javascript
var express = require('express');

module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-contrib-watch');

grunt.initConfig({
watch: {
server: {
files: './server.js',
tasks: ['server'],
options: {
interrupt: true,
atBegin: true
}
}
});

grunt.registerTask('server', function() {
require('./server').listen(3000, function(err) {
if (err) {
grunt.log.writeln(err);
} else {
grunt.log.writeln('started');
}
});
this.async();
});

// Default task(s).
grunt.registerTask('default', ['watch']);
};
```