簡體   English   中英

Grunt任務等待退出

[英]Grunt task to wait for exit

我有一個啟動IIS Express異步的任務,並且要停止IIS,我必須觸發一個grunt事件。

我想做一個等待直到按ctrl-c然后觸發該事件的任務。

我試過這樣做:

grunt.registerTask("killiis", function(){
    process.stdin.resume();
    var done = this.async();
    grunt.log.writeln('Waiting...');    

    process.on('SIGINT', function() {
        grunt.event.emit('iis.kill');
        grunt.log.writeln('Got SIGINT.  Press Control-D to exit.');
        done();
    });
});

該任務成功停止了grunt,但沒有正確發送事件。

SIGINT處理程序在Node.js中工作,但在Grunt中不起作用(我不知道為什么)。 我使用readline模塊手動處理ctrl+c並監聽exit事件:

var readline = require('readline');

var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.on('SIGINT', function() {
  process.emit('SIGINT');
});

process.on('exit', killIis);

function killIis() {
  // kill it
}

另外我建議收聽SIGINTSIGHUPSIGBREAK信號以處理控制台窗口關閉或ctrl+break (如果有人使用它,呵呵)。 當您還要停止應用程序時,在這些處理程序中調用process.exit()

process.on('exit', killIis);
process.on('SIGINT', killIisAndExit);
process.on('SIGHUP', killIisAndExit);
process.on('SIGBREAK', killIisAndExit);

我有一個grunt-iisexpress ,在退出時殺死IIS: https//github.com/whyleee/grunt-iisexpress

我嘗試了whyleee的建議,但我發現grunt沒有等待清理過程退出,然后關閉自己。

我的解決方案基於這篇文章

module.exports = function(grunt) {
  var exec, readline, shuttingDown, stdInterface;
  shuttingDown = false;
  readline = require("readline");
  exec = require("child_process").exec;
  // other grunt requires / task loads, including a "cleanup" task ...
  stdInterface = readline.createInterface({
    input: process.stdin,
    output: process.stdout
  });
  stdInterface.on("SIGINT", function() {
    var child;
    if (shuttingDown) {
      return;
    }
    shuttingDown = true;
    console.info("Cleaning up ...");
    child = exec("grunt cleanup", function(err, stdout, stderr) {
      console.info(stdout);
      process.exit(err ? 1 : 0);
    });
  });
  // other grunt config ...
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM