Skip to content

Commit

Permalink
Fogbugz 3426: Kill and restart the process in pm2 (#88)
Browse files Browse the repository at this point in the history
* Fogbugz 3426: Kill and restart the process in pm2

* Remove console log's
  • Loading branch information
varshajnagaraja authored and matharuajay committed May 23, 2019
1 parent 07d9f4b commit f5606ba
Showing 1 changed file with 47 additions and 8 deletions.
55 changes: 47 additions & 8 deletions pm2.js
@@ -1,5 +1,7 @@
const pm2 = require('pm2');
const fs = require('fs');
const COMPILER = "eosio compiler";
const EXPLORER = "eosio explorer";

const displayStatus = (app) => {
if (app.pm2_env.status === "online") {
Expand All @@ -11,16 +13,11 @@ const displayStatus = (app) => {
}
}

pm2.connect(function(err) {
if (err) {
console.error(err);
process.exit(2);
}

const startPm2 = () =>{
console.log("\x1b[36mstarting app and compiler using pm2\x1b[0m");
if (fs.existsSync(process.argv.slice(2)[0])) {
pm2.start({
name : "eosio compiler",
name : COMPILER,
script : 'yarn start',
cwd: process.argv.slice(2)[0]
}, function(err, app) {
Expand All @@ -32,10 +29,52 @@ pm2.connect(function(err) {
console.log ("\x1b[31merror finding compiler path\x1b[0m");

pm2.start({
name : "eosio explorer",
name : EXPLORER,
script : 'serve.js' // Script to be run
}, function(err, app) {
if (err) throw err
displayStatus(app[0]);
});
}

const deletePm2 = (list) =>{
let delete_count = 0;
list.forEach((app) =>{
pm2.delete(app.pm2_env.pm_id, (err, res)=> {
delete_count++;
if(delete_count === list.length){
startPm2();
}
});
});
}

pm2.connect(function(err) {
if (err) {
console.error(err);
process.exit(2);
}
// clearing the already running pm2 instances before starting
pm2.list((err, apps)=>{
if(err)
console.log("error encountered while fetching pm2 list-> ", err);
else {
if(apps.length > 0){
let deleteList = [];
apps.forEach((app, index)=>{
if(app.pm2_env.name === EXPLORER || app.pm2_env.name === COMPILER){
deleteList.push(app);
}
if(index === apps.length-1){
if(deleteList.length > 0)
deletePm2(deleteList);
else
startPm2();
}
});
}else{
startPm2();
}
}
});
});

0 comments on commit f5606ba

Please sign in to comment.