5

Jake 任务执行一个长时间运行的系统命令。另一个任务取决于第一个任务在开始之前完全完成。'child_process' 的 'exec' 函数异步执行系统命令,使得第二个任务可以在第一个任务完成之前开始。

编写 Jakefile 以确保第一个任务中长时间运行的系统命令在第二个任务开始之前完成的最干净的方法是什么?

我曾考虑在第一个任务结束时在虚拟循环中使用轮询,但这闻起来很糟糕。似乎必须有更好的方法。我看过这个 SO question,但它并没有完全解决我的问题。

var exec = require('child_process').exec;

desc('first task');
task('first', [], function(params) {
  exec('long running system command');
});

desc('second task');
task('second', ['first'], function(params) {
  // do something dependent on the completion of 'first' task
});
4

2 回答 2

2

通过重新阅读Matthew Eernisse 的帖子,我找到了自己问题的答案。对于那些想知道如何做到这一点的人:

var exec = require('child_process').exec;

desc('first task');
task('first', [], function(params) {
  exec('long running system command', function() {
    complete();
  });
}, true); // this prevents task from exiting until complete() is called

desc('second task');
task('second', ['first'], function(params) {
  // do something dependent on the completion of 'first' task
});
于 2011-07-20T01:01:33.423 回答
1

仅供将来参考,我有一个没有其他依赖项的同步 exec 模块。

例子:

var allsync = require("allsync");
allsync.exec( "find /", function(data){
    process.stdout.write(data);
});
console.log("Done!");

在上面的例子中,只有在进程存在后才Done打印。该功能基本上阻塞,直到完成。findexec

于 2013-02-05T23:23:47.260 回答