使用grunt,我如何同时运行三个阻塞任务



我使用grunt-concurrent成功地将grunt-contrib-watchgrunt-nodemon组合在一起,使我可以在编辑和传输coffeescript文件时自动启动node.js实例。

以下是我用来实现这一点的gruntfile的grunt-concurrent部分:

咕哝文件.咖啡

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

watchnodemon任务配置在同一文件中,但为了简洁起见已删除。这很好。

现在,我想将grunt-node-inspector添加到并发任务列表中。像这样:

concurrent:
  dev:
    tasks: [
      'watch'
      'nodemon'
      'node-inspector'
    ]
    options: 
      logConcurrentOutput: true

至少根据grunt-nodemon的帮助文件,这应该是可能的,因为它是作为一个示例使用的:并发运行Nodemon

然而,这对我来说不起作用。相反,只开始前两项任务。

实验表明,grunt-concurrent似乎仅限于同时运行两个任务。将忽略任何后续任务。我尝试了各种选择,例如:

concurrent:
  dev1:[
      'watch'
      'nodemon'
    ]
  dev2:[
      'node-inspector'
    ]        
    options: 
      logConcurrentOutput: true
grunt.registerTask 'default', ['concurrent:dev1', 'concurrent:dev2']

我还尝试将limit选项设置为3。我对此寄予厚望,所以也许我误解了如何正确应用价值:

concurrent:
  dev:
    limit: 3
    tasks: [
      'watch'
      'nodemon'
      'node-inspector'
    ]
    options: 
      logConcurrentOutput: true

但是我无法运行我的第三个阻塞任务。

问题如何使所有三个阻塞任务同时运行?

谢谢。

将极限值放入选项中,如下所示:

concurrent: {
    tasks: ['nodemon', 'watch', 'node-inspector'],
    options: {
        limit: 5,
        logConcurrentOutput: true
    }
}

我一直在使用grunt.util.spawn来运行我的任务,并在最后包含1个阻塞调用。http://gruntjs.com/api/grunt.util#grunt.util.spawnhttp://nodejs.org/api/process.html#process_signal_events

这个街区杀死了孩子们。

var children = [];
process.on('SIGINT', function(){
    children.forEach(function(child) {
        console.log('killing child!');
        child.kill('SIGINT');
    });
});
module.exports = function (grunt) {
    'use strict';

然后我注册一个任务

grunt.registerTask('blocked', 'blocking calls', function() {
    var path = require('path')
    var bootstrapDir = path.resolve(process.cwd()) + '/bootstrap';
    var curDir = path.resolve(process.cwd());
    children.push(
        grunt.util.spawn( {
            cmd: 'grunt',
            args: ['watch'],
            opts: {
                cwd: bootstrapDir,
                stdio: 'inherit',
            }
        })
    );
    children.push(
        grunt.util.spawn( {
            cmd: 'grunt',
            args: ['nodemon'],
            opts: {
                cwd: curDir,
                stdio: 'inherit',
            }
        })
    );
    children.push(
        grunt.util.spawn( {
            cmd: 'grunt',
            args: ['node-inspector'],
            opts: {
                cwd: curDir,
                stdio: 'inherit',
            }
        })
    );
    grunt.task.run('watch');
});

在您的情况下,您可以将当前工作目录更改为gruntfile.js并运行多个实例。