咕task任务等待退出 [英] Grunt task to wait for exit
问题描述
我有一个启动IIS Express异步的任务,并且要停止IIS,我必须发出一个咕噜的事件。
I've got a task that starts IIS Express async, and to stop IIS I have to fire a grunt event.
我想做一个任务只是等待,直到我按 ctrl-c
,然后触发该事件。
I would like to make a task that just waits until i press ctrl-c
and then fires that event.
我试过这样做:
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,但不会正确发送事件。
The task stops grunt successfully, but doesn't send the event properly.
推荐答案
SIGINT
处理程序在Node.js中工作,但不在Grunt中(我不知道为什么)。我使用 readline
模块手动处理 ctrl + c
并监听 exit
event:
SIGINT
handlers work in Node.js, but not in Grunt (I don't know why). I handle ctrl+c
manually using readline
module and listen to exit
event:
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
}
此外,我建议收听 SIGINT
, SIGHUP
和 SIGBREAK
信号来处理控制台窗口关闭或 ctrl + break
(如果有人使用它,heh)。在这些处理程序中调用 process.exit()
时,您还想停止应用程序:
Additionally I suggest to listen to SIGINT
, SIGHUP
and SIGBREAK
signals to handle console window close or ctrl+break
(if anybody use it, heh). Call process.exit()
in these handlers when you also want to stop the app:
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 。
I have a fork of grunt-iisexpress
that kills IIS on exit: https://github.com/whyleee/grunt-iisexpress.
这篇关于咕task任务等待退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!