From 345af7438b9be10864177e58949f35768c6c0125 Mon Sep 17 00:00:00 2001 From: Satinder Grewal Date: Tue, 24 Jan 2017 09:10:32 +1300 Subject: [PATCH] Revert "pm2 bugfix" This reverts commit 1b4c51ea1d836cf1b4591d2faece42a507ddbe1e. --- main.js | 131 +++++++++++---------------------------------- package.json | 6 +-- routes/shepherd.js | 68 +++++++++++------------ 3 files changed, 65 insertions(+), 140 deletions(-) diff --git a/main.js b/main.js index 6340854..a8e084e 100644 --- a/main.js +++ b/main.js @@ -9,9 +9,12 @@ 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'); @@ -56,7 +59,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'); @@ -66,39 +69,33 @@ 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 iguanaPath = process.env.HOME + '/Library/Application Support/iguana'; - var komodoBinPath = path.join(__dirname, '/assets/bin/osx/komodo'); - var iguanaBinPath = iguanaOSX; - var iguanaConfsDir = iguanaPath + '/confs'; + var iguanaDir = process.env.HOME + '/Library/Application Support/iguana'; + var iguanaConfsDir = iguanaDir + '/confs'; } if (os.platform() === 'linux') { - var iguanaPath = process.env.HOME + '/.iguana' - var komodoBinPath = path.join(__dirname, '/assets/bin/linux/komodo'); - var iguanaBinPath = iguanaLinux; - var iguanaConfsDir = iguanaPath + '/confs'; + var iguanaDir = process.env.HOME + '/.iguana' + var iguanaConfsDir = iguanaDir + '/confs'; var iguanaIcon = path.join(__dirname, '/assets/icons/iguana_app_icon_png/128x128.png') } if (os.platform() === 'win32') { - var iguanaPath = process.env.APPDATA + '/iguana'; iguanaPath = path.normalize(iguanaPath) - var komodoBinPath = path.join(__dirname, '/assets/bin/win32/komodo'); - var iguanaBinPath = iguanaWin; + var iguanaDir = process.env.APPDATA + '/iguana'; iguanaDir = path.normalize(iguanaDir) 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(iguanaPath); +//console.log(iguanaDir); /* // MAKE SURE IGUANA DIR IS THERE FOR USER -mkdirp(iguanaPath, function (err) { +mkdirp(iguanaDir, function (err) { if (err) console.error(err) else - fs.readdir(iguanaPath, (err, files) => { + fs.readdir(iguanaDir, (err, files) => { files.forEach(file => { //console.log(file); }); @@ -127,7 +124,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 @@ -138,17 +135,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(iguanaPath); - //exec(iguanaWin, {cwd: iguanaPath}); //specify binary in startup + process.chdir(iguanaDir); + //exec(iguanaWin, {cwd: iguanaDir}); //specify binary in startup ig = spawn(iguanaWin); } if (os.platform() === 'linux') { - process.chdir(iguanaPath); + process.chdir(iguanaDir); ig = spawn(iguanaLinux); //corsproxy_process = spawn('corsproxy'); } if (os.platform() === 'darwin') { - //process.chdir(iguanaPath); + //process.chdir(iguanaDir); //ig = spawn(iguanaOSX); //corsproxy_process = spawn('corsproxy'); }*/ @@ -158,10 +155,7 @@ function createLoadingWindow() { app.on('ready', createLoadingWindow) - - function createWindow (status) { - if ( status === 'open') { require(path.join(__dirname, 'private/mainmenu')); @@ -177,14 +171,11 @@ 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) { @@ -198,13 +189,12 @@ 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...'); @@ -219,7 +209,7 @@ function createWindow (status) { }, 2000) }) } -*/ + var HideMainWindow = function() { return new Promise(function(resolve, reject) { @@ -242,16 +232,12 @@ function createWindow (status) { }) } -/* // - ConnectToPm2() - .then(function(result) { - //return KillPm2(); + .then(function(result) { + return KillPm2(); }) .then(HideMainWindow) .then(QuitApp) -*/ - }); } } @@ -261,71 +247,18 @@ function createWindow (status) { //}) app.on('window-all-closed', function () { - // 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') { + //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') { app.quit() - } + }*/ + }) app.on('activate', function () { if (mainWindow === null) { //createWindow('open'); } -}) - - -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); - */ -} +}) \ No newline at end of file diff --git a/package.json b/package.json index 95e6ef9..a93dd80 100644 --- a/package.json +++ b/package.json @@ -19,8 +19,7 @@ "author": "ca333, grewalsatinder", "license": "MIT", "devDependencies": { - "electron": "^1.4.1", - "fix-path": "^2.1.0" + "electron": "^1.4.1" }, "dependencies": { "bluebird": "^3.4.7", @@ -30,7 +29,6 @@ "fs-extra": "^1.0.0", "md5": "^2.2.1", "mkdirp": "^0.5.1", - "pm2": "^2.3.0", - "fix-path": "^2.1.0" + "pm2": "^2.2.3" } } diff --git a/routes/shepherd.js b/routes/shepherd.js index 2ecf124..1b7bbf0 100644 --- a/routes/shepherd.js +++ b/routes/shepherd.js @@ -11,7 +11,6 @@ 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"); @@ -25,7 +24,6 @@ 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'; @@ -73,7 +71,7 @@ shepherd.post('/herd', function(req, res) { herder(req.body.herd, req.body.options); res.end('{"msg": "success","result": "result"}'); - + }); @@ -86,7 +84,7 @@ shepherd.post('/slay', function(req, res) { slayer(req.body.slay); res.end('{"msg": "success","result": "result"}'); - + }); @@ -99,7 +97,7 @@ shepherd.post('/setconf', function(req, res) { setConf(req.body.chain); res.end('{"msg": "success","result": "result"}'); - + }); shepherd.post('/getconf', function(req, res) { @@ -113,14 +111,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') { @@ -138,33 +136,30 @@ 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') { @@ -190,7 +185,6 @@ function herder(flock, data) { }); }); } - } @@ -226,7 +220,7 @@ function setConf(flock) { } console.log(DaemonConfPath); - + var CheckFileExists = function() { return new Promise(function(resolve, reject) { @@ -235,7 +229,7 @@ function setConf(flock) { fs.ensureFile(DaemonConfPath, function (err) { console.log(err) // => null }) - + setTimeout(function() { console.log(result) resolve(result); @@ -249,7 +243,7 @@ function setConf(flock) { var result = 'Conf file permissions updated to Read/Write' fsnode.chmodSync(DaemonConfPath, '0666'); - + setTimeout(function() { console.log(result) resolve(result); @@ -292,7 +286,7 @@ function setConf(flock) { return new Promise(function(resolve, reject) { var result = 'checking rpcuser...' - + if(status[0].hasOwnProperty('rpcuser')){ console.log('rpcuser: OK'); } @@ -349,7 +343,7 @@ function setConf(flock) { console.log('server: ADDED') }); } - + //console.log(result) resolve(result); }) @@ -370,14 +364,14 @@ function setConf(flock) { console.log('addnode: ADDED') }); } - + //console.log(result) resolve(result); }) } rpcuser() - .then(function(result) { + .then(function(result) { return rpcpass(); }) .then(server) @@ -396,7 +390,7 @@ function setConf(flock) { var result = 'Conf file permissions updated to Read Only' fsnode.chmodSync(DaemonConfPath, '0400'); - + setTimeout(function() { console.log(result) resolve(result); @@ -405,7 +399,7 @@ function setConf(flock) { } CheckFileExists() - .then(function(result) { + .then(function(result) { return FixFilePermissions(); }) .then(RemoveLines) @@ -439,4 +433,4 @@ function getConf(flock) { return DaemonConfPath } -module.exports = shepherd; +module.exports = shepherd; \ No newline at end of file