Gulp: execute multiple parallel node scripts

I have two server scripts (both rely on socket.io, working on different ports).

I would like to start both in parallel using gulp. But, in addition, I would like to be able to stop one of them. And perhaps even access the console output of each script.

Is there an existing solution for this? Or do you even recommend using anything else besides gulp?

+5
source share
2 answers

I found a solution in which I additionally start the mongoDB server:

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 (eg 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']); 
+3
source

nodemon/foreverjs is a good solution for difficult cases, but they are not as scalable as pm2 . So, if you want a scalable and reliable solution, I would recommend using pm2 . In addition, it is worth mentioning that pm2 demononizes after launch, unlike foreverjs/nodemon . This may be a bug or feature for you and generally depends on your needs.

 pm2 start script1.js pm2 start script2.js pm2 status // show status of running processes pm2 logs // tail -f logs from running processes 
0
source

Source: https://habr.com/ru/post/1237449/


All Articles