2015-12-05 12 views
5

私は2つのサーバースクリプトを持っています(どちらもsocket.ioに依存しています;異なるポートで実行しています)。Gulp:複数のノードスクリプトを並列に実行する

私はgulpで両方を並行して起動したいと思います。しかし、私はそれらのうちの1つを停止する可能性を持っていたいと思います。また、各スクリプトのコンソール出力にアクセスすることさえできます。

これには既存の解決策がありますか?それとも、ガルプ以外の何かを使うことをお勧めしますか?

答えて

3

私はさらにMongoDBのサーバを起動する解決策を見つけた:

var child_process = require('child_process'); 
var nodemon = require('gulp-nodemon'); 

var processes = {server1: null, server2: null, mongo: null}; 

gulp.task('start:server', function (cb) { 
    // The magic happens here ... 
    processes.server1 = nodemon({ 
     script: "server1.js", 
     ext: "js" 
    }); 

    // ... and here 
    processes.server2 = nodemon({ 
     script: "server2.js", 
     ext: "js" 
    }); 

    cb(); // For parallel execution accept a callback. 
      // For further info see "Async task support" section here: 
      // https://github.com/gulpjs/gulp/blob/master/docs/API.md 
}); 

gulp.task('start:mongo', function (cb) { 
    processes.mongo = child_process.exec('mongod', function (err, stdout, stderr) {}); 

    cb(); 
}); 

process.on('exit', function() { 
    // In case the gulp process is closed (e.g. by pressing [CTRL + C]) stop both processes 
    processes.server1.kill(); 
    processes.server2.kill(); 
    processes.mongo.kill(); 
}); 

gulp.task('run', ['start:mongo', 'start:server']); 
gulp.task('default', ['run']); 
+0

私は提案を開いて、より良いソリューションにチェックマークを与えて喜びました。 – tmuecksch

0

nodemon/foreverjsは複雑ではない場合に適したソリューションですが、彼らはpm2があるほどスケーラブルではありません。したがって、スケーラブルで信頼性の高いソリューションが必要な場合は、pm2を使用することをお勧めします。 また、pm2は起動後にforeverjs/nodemonのようにdaemonizeすることに注意してください。それはバグまたはあなたのための機能であり、一般的にあなたのニーズに依存します。

pm2 start script1.js 
pm2 start script2.js 
pm2 status // show status of running processes 
pm2 logs // tail -f logs from running processes 
関連する問題