2011-07-20 44 views
5

Jake任務執行長時間運行的系統命令。另一項任務取決於第一項任務在開始前完全完成。 'child_process'的'exec'功能異步執行系統命令,從而可以在第一個任務完成之前開始第二個任務。Node.js和Jake - 如何在任務中同步調用系統命令?

寫入Jakefile以確保第一個任務中長時間運行的系統命令在第二個任務開始之前完成的最簡潔方法是什麼?

我想過在第一個任務結束時在一個虛擬循環中使用輪詢,但這只是味道不好。似乎必須有更好的方法。我見過this 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 
}); 

回答

2

我通過再重讀Matthew Eernisse's post找到了答案,以我自己的問題。對於那些想知道如何做到這一點:

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 
}); 
+1

爲什麼不完成第一個任務會發出信號,並且該信號的偵聽器會產生第二個任務? – Keith

1

只是以供將來參考,我也沒有其他的依賴同步執行模塊。

實施例:

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

在上述exampale,Done僅印刷find過程中存在。 exec函數基本上阻塞直到完成。