From 1b4c51ea1d836cf1b4591d2faece42a507ddbe1e Mon Sep 17 00:00:00 2001 From: ca333 Date: Mon, 23 Jan 2017 19:59:55 +0100 Subject: [PATCH] pm2 bugfix --- main.js | 131 ++++++++++++++++++++++++++++++++++----------- package.json | 6 ++- routes/shepherd.js | 68 ++++++++++++----------- 3 files changed, 140 insertions(+), 65 deletions(-) diff --git a/main.js b/main.js index a8e084e..6340854 100644 --- a/main.js +++ b/main.js @@ -9,12 +9,9 @@ var bodyParser = require('body-parser') const path = require('path') const url = require('url') const os = require('os') -const spawn = require('child_process').spawn; -const exec = require('child_process').exec; var fs = require('fs'); var fs = require('fs-extra') var mkdirp = require('mkdirp'); -var pm2 = require('pm2'); Promise = require('bluebird'); app.setName('Iguana'); @@ -59,7 +56,7 @@ module.exports = guiapp; //require('./assets/js/iguana.js'); //below code shall be separated into asset js for public version -/* + // SELECTING IGUANA BUILD TO RUN AS PER OS DETECTED BY DESKTOP APP var iguanaOSX = path.join(__dirname, '/assets/bin/osx/iguana'); var iguanaLinux = path.join(__dirname, '/assets/bin/linux64/iguana'); @@ -69,33 +66,39 @@ var iguanaConfsDirSrc = path.join(__dirname, '/assets/deps/confs'); // SETTING OS DIR TO RUN IGUANA FROM // SETTING APP ICON FOR LINUX AND WINDOWS if (os.platform() === 'darwin') { - var iguanaDir = process.env.HOME + '/Library/Application Support/iguana'; - var iguanaConfsDir = iguanaDir + '/confs'; + var iguanaPath = process.env.HOME + '/Library/Application Support/iguana'; + var komodoBinPath = path.join(__dirname, '/assets/bin/osx/komodo'); + var iguanaBinPath = iguanaOSX; + var iguanaConfsDir = iguanaPath + '/confs'; } if (os.platform() === 'linux') { - var iguanaDir = process.env.HOME + '/.iguana' - var iguanaConfsDir = iguanaDir + '/confs'; + var iguanaPath = process.env.HOME + '/.iguana' + var komodoBinPath = path.join(__dirname, '/assets/bin/linux/komodo'); + var iguanaBinPath = iguanaLinux; + var iguanaConfsDir = iguanaPath + '/confs'; var iguanaIcon = path.join(__dirname, '/assets/icons/iguana_app_icon_png/128x128.png') } if (os.platform() === 'win32') { - var iguanaDir = process.env.APPDATA + '/iguana'; iguanaDir = path.normalize(iguanaDir) + var iguanaPath = process.env.APPDATA + '/iguana'; iguanaPath = path.normalize(iguanaPath) + var komodoBinPath = path.join(__dirname, '/assets/bin/win32/komodo'); + var iguanaBinPath = iguanaWin; var iguanaConfsDir = process.env.APPDATA + '/iguana/confs'; iguanaConfsDir = path.normalize(iguanaConfsDir) var iguanaIcon = path.join(__dirname, '/assets/icons/iguana_app_icon.ico') iguanaConfsDirSrc = path.normalize(iguanaConfsDirSrc); } -*/ + if (os.platform() === 'linux') { var iguanaIcon = path.join(__dirname, '/assets/icons/iguana_app_icon_png/128x128.png') } if (os.platform() === 'win32') { var iguanaIcon = path.join(__dirname, '/assets/icons/iguana_app_icon.ico') } -//console.log(iguanaDir); +//console.log(iguanaPath); /* // MAKE SURE IGUANA DIR IS THERE FOR USER -mkdirp(iguanaDir, function (err) { +mkdirp(iguanaPath, function (err) { if (err) console.error(err) else - fs.readdir(iguanaDir, (err, files) => { + fs.readdir(iguanaPath, (err, files) => { files.forEach(file => { //console.log(file); }); @@ -124,7 +127,7 @@ function createLoadingWindow() { // DEVTOOLS - only for dev purposes - ca333 //loadingWindow.webContents.openDevTools() - + //launchPM2(); // if window closed we kill iguana proc loadingWindow.on('closed', function () { // our app does not have multiwindow - so we dereference the window object instead of @@ -135,17 +138,17 @@ function createLoadingWindow() { //ca333 todo - add os detector to use correct binary - so we can use the same bundle on ALL OS platforms /*if (os.platform() === 'win32') { - process.chdir(iguanaDir); - //exec(iguanaWin, {cwd: iguanaDir}); //specify binary in startup + process.chdir(iguanaPath); + //exec(iguanaWin, {cwd: iguanaPath}); //specify binary in startup ig = spawn(iguanaWin); } if (os.platform() === 'linux') { - process.chdir(iguanaDir); + process.chdir(iguanaPath); ig = spawn(iguanaLinux); //corsproxy_process = spawn('corsproxy'); } if (os.platform() === 'darwin') { - //process.chdir(iguanaDir); + //process.chdir(iguanaPath); //ig = spawn(iguanaOSX); //corsproxy_process = spawn('corsproxy'); }*/ @@ -155,7 +158,10 @@ function createLoadingWindow() { app.on('ready', createLoadingWindow) + + function createWindow (status) { + if ( status === 'open') { require(path.join(__dirname, 'private/mainmenu')); @@ -171,11 +177,14 @@ function createWindow (status) { // if window closed we kill iguana proc mainWindow.on('closed', function () { + +/* + var ConnectToPm2 = function() { return new Promise(function(resolve, reject) { console.log('Closing Main Window...'); - + pm2.connect(function(err) { console.log('connecting to pm2...'); if (err) { @@ -189,12 +198,13 @@ function createWindow (status) { resolve(result); }) } - +*/ +/* //we launch in nodaemonmode - pm2 exits with process var KillPm2 = function() { return new Promise(function(resolve, reject) { console.log('killing to pm2...'); - + pm2.killDaemon(function(err) { pm2.disconnect(); console.log('killed to pm2...'); @@ -209,7 +219,7 @@ function createWindow (status) { }, 2000) }) } - +*/ var HideMainWindow = function() { return new Promise(function(resolve, reject) { @@ -232,12 +242,16 @@ function createWindow (status) { }) } +/* // + ConnectToPm2() - .then(function(result) { - return KillPm2(); + .then(function(result) { + //return KillPm2(); }) .then(HideMainWindow) .then(QuitApp) +*/ + }); } } @@ -247,18 +261,71 @@ function createWindow (status) { //}) app.on('window-all-closed', function () { - //if (os.platform() !== 'win32') { ig.kill(); } - // in osx apps stay active in menu bar until explictly closed or quitted by CMD Q - // so we do not kill the app --> for the case user clicks again on the iguana icon - // we open just a new window and respawn iguana proc - /*if (process.platform !== 'darwin' || process.platform !== 'linux' || process.platform !== 'win32') { + // On OS X it is common for applications and their menu bar + // to stay active until the user quits explicitly with Cmd + Q + if (process.platform !== 'darwin') { app.quit() - }*/ - + } }) app.on('activate', function () { if (mainWindow === null) { //createWindow('open'); } -}) \ No newline at end of file +}) + + +function launchPM2() { +/* +if (os.platform() === 'darwin') { + fixPath(); +} + +pm2.connect(true, function(err) { //start up pm2 god + if (err) { + console.log(err); + process.exit(2); + } + + pm2.start({ + script : iguanaBinPath, // path to binary + exec_mode : 'fork', + cwd: iguanaPath, + //exec_interpreter = none /Users/crypto/Desktop/dev_projects/pm2_iguana_respawn + //exec_mode : cluster --> for server production enviroment... if highload... + //instances : 4, + //max_memory_restart : '100M' // mem limit for restart + }, function(err, apps) { + pm2.disconnect(); // Disconnect from PM2 + if (err) throw err + }); + + + pm2.start({ + script : komodoBinPath, // path to binary + exec_mode : 'fork' + // cwd: './iguana_dir/', + //exec_interpreter = none /Users/crypto/Desktop/dev_projects/pm2_iguana_respawn + //exec_mode : cluster --> for server production enviroment... if highload... + //instances : 4, + //max_memory_restart : '100M' // mem limit for restart + }, function(err, apps) { + pm2.disconnect(); // Disconnect from PM2 + if (err) throw err + }); +}); +//pm2 startup workaround + /* var fpath = path.join(__dirname, '/node_modules/pm2/bin/pm2 start iguana2.json'); + + exec(fpath, (error, stdout, stderr) => { + if (error) { + console.error(`exec error: ${error}`); + return; + } + console.log(`stdout: ${stdout}`); + console.log(`stderr: ${stderr}`); +}); + console.log("pm2 launched..."); + //process.exit(2); + */ +} diff --git a/package.json b/package.json index a93dd80..95e6ef9 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,8 @@ "author": "ca333, grewalsatinder", "license": "MIT", "devDependencies": { - "electron": "^1.4.1" + "electron": "^1.4.1", + "fix-path": "^2.1.0" }, "dependencies": { "bluebird": "^3.4.7", @@ -29,6 +30,7 @@ "fs-extra": "^1.0.0", "md5": "^2.2.1", "mkdirp": "^0.5.1", - "pm2": "^2.2.3" + "pm2": "^2.3.0", + "fix-path": "^2.1.0" } } diff --git a/routes/shepherd.js b/routes/shepherd.js index 1b7bbf0..2ecf124 100644 --- a/routes/shepherd.js +++ b/routes/shepherd.js @@ -11,6 +11,7 @@ const express = require('express'); const md5 = require('md5'); const pm2 = require('pm2'); Promise = require('bluebird'); +var fixPath = require('fix-path'); var setconf = require("../private/setconf.js"); @@ -24,6 +25,7 @@ var iguanaConfsDirSrc = path.join(__dirname, '../assets/deps/confs'); // SETTING OS DIR TO RUN IGUANA FROM // SETTING APP ICON FOR LINUX AND WINDOWS if (os.platform() === 'darwin') { + fixPath(); var iguanaBin = path.join(__dirname, '../assets/bin/osx/iguana'); var iguanaDir = process.env.HOME + '/Library/Application Support/iguana'; var iguanaConfsDir = iguanaDir + '/confs'; @@ -71,7 +73,7 @@ shepherd.post('/herd', function(req, res) { herder(req.body.herd, req.body.options); res.end('{"msg": "success","result": "result"}'); - + }); @@ -84,7 +86,7 @@ shepherd.post('/slay', function(req, res) { slayer(req.body.slay); res.end('{"msg": "success","result": "result"}'); - + }); @@ -97,7 +99,7 @@ shepherd.post('/setconf', function(req, res) { setConf(req.body.chain); res.end('{"msg": "success","result": "result"}'); - + }); shepherd.post('/getconf', function(req, res) { @@ -111,14 +113,14 @@ shepherd.post('/getconf', function(req, res) { console.log(confpath); res.end('{"msg": "success","result": "' + confpath + '"}'); - + }); function herder(flock, data) { //console.log(flock); //console.log(data); - + if (data == undefined) { data = 'none'; console.log('it is undefined'); } if (flock === 'iguana') { @@ -136,30 +138,33 @@ function herder(flock, data) { }); }) }); - + // COPY CONFS DIR WITH PEERS FILE TO IGUANA DIR, AND KEEP IT IN SYNC fs.copy(iguanaConfsDirSrc, iguanaConfsDir, function (err) { if (err) return console.error(err) console.log('confs files copied successfully at: '+ iguanaConfsDir ) }) + pm2.connect(true, function(err) { //start up pm2 god + if (err) { + console.log(err); + process.exit(2); + } + + pm2.start({ + script : iguanaBin, // path to binary + exec_mode : 'fork', + cwd: iguanaDir, + //exec_interpreter = none /Users/crypto/Desktop/dev_projects/pm2_iguana_respawn + //exec_mode : cluster --> for server production enviroment... if highload... + //instances : 4, + //max_memory_restart : '100M' // mem limit for restart + }, function(err, apps) { + pm2.disconnect(); // Disconnect from PM2 + if (err) throw err + }); - pm2.connect(function(err) { //start up pm2 god - if (err) { - console.error(err); - process.exit(2); - } - - pm2.start({ - script : iguanaBin, // path to binary - name: 'IGUANA', - exec_mode : 'fork', - cwd: iguanaDir, //set correct iguana directory - }, function(err, apps) { - pm2.disconnect(); // Disconnect from PM2 - if (err) throw err }); - }); } if (flock === 'komodod') { @@ -185,6 +190,7 @@ function herder(flock, data) { }); }); } + } @@ -220,7 +226,7 @@ function setConf(flock) { } console.log(DaemonConfPath); - + var CheckFileExists = function() { return new Promise(function(resolve, reject) { @@ -229,7 +235,7 @@ function setConf(flock) { fs.ensureFile(DaemonConfPath, function (err) { console.log(err) // => null }) - + setTimeout(function() { console.log(result) resolve(result); @@ -243,7 +249,7 @@ function setConf(flock) { var result = 'Conf file permissions updated to Read/Write' fsnode.chmodSync(DaemonConfPath, '0666'); - + setTimeout(function() { console.log(result) resolve(result); @@ -286,7 +292,7 @@ function setConf(flock) { return new Promise(function(resolve, reject) { var result = 'checking rpcuser...' - + if(status[0].hasOwnProperty('rpcuser')){ console.log('rpcuser: OK'); } @@ -343,7 +349,7 @@ function setConf(flock) { console.log('server: ADDED') }); } - + //console.log(result) resolve(result); }) @@ -364,14 +370,14 @@ function setConf(flock) { console.log('addnode: ADDED') }); } - + //console.log(result) resolve(result); }) } rpcuser() - .then(function(result) { + .then(function(result) { return rpcpass(); }) .then(server) @@ -390,7 +396,7 @@ function setConf(flock) { var result = 'Conf file permissions updated to Read Only' fsnode.chmodSync(DaemonConfPath, '0400'); - + setTimeout(function() { console.log(result) resolve(result); @@ -399,7 +405,7 @@ function setConf(flock) { } CheckFileExists() - .then(function(result) { + .then(function(result) { return FixFilePermissions(); }) .then(RemoveLines) @@ -433,4 +439,4 @@ function getConf(flock) { return DaemonConfPath } -module.exports = shepherd; \ No newline at end of file +module.exports = shepherd;