Browse Source

pm2 bugfix

ca333-dev
ca333 8 years ago
parent
commit
1b4c51ea1d
  1. 123
      main.js
  2. 6
      package.json
  3. 36
      routes/shepherd.js

123
main.js

@ -9,12 +9,9 @@ var bodyParser = require('body-parser')
const path = require('path') const path = require('path')
const url = require('url') const url = require('url')
const os = require('os') const os = require('os')
const spawn = require('child_process').spawn;
const exec = require('child_process').exec;
var fs = require('fs'); var fs = require('fs');
var fs = require('fs-extra') var fs = require('fs-extra')
var mkdirp = require('mkdirp'); var mkdirp = require('mkdirp');
var pm2 = require('pm2');
Promise = require('bluebird'); Promise = require('bluebird');
app.setName('Iguana'); 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 //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 // SELECTING IGUANA BUILD TO RUN AS PER OS DETECTED BY DESKTOP APP
var iguanaOSX = path.join(__dirname, '/assets/bin/osx/iguana'); var iguanaOSX = path.join(__dirname, '/assets/bin/osx/iguana');
var iguanaLinux = path.join(__dirname, '/assets/bin/linux64/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 OS DIR TO RUN IGUANA FROM
// SETTING APP ICON FOR LINUX AND WINDOWS // SETTING APP ICON FOR LINUX AND WINDOWS
if (os.platform() === 'darwin') { if (os.platform() === 'darwin') {
var iguanaDir = process.env.HOME + '/Library/Application Support/iguana'; var iguanaPath = process.env.HOME + '/Library/Application Support/iguana';
var iguanaConfsDir = iguanaDir + '/confs'; var komodoBinPath = path.join(__dirname, '/assets/bin/osx/komodo');
var iguanaBinPath = iguanaOSX;
var iguanaConfsDir = iguanaPath + '/confs';
} }
if (os.platform() === 'linux') { if (os.platform() === 'linux') {
var iguanaDir = process.env.HOME + '/.iguana' var iguanaPath = process.env.HOME + '/.iguana'
var iguanaConfsDir = iguanaDir + '/confs'; 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') var iguanaIcon = path.join(__dirname, '/assets/icons/iguana_app_icon_png/128x128.png')
} }
if (os.platform() === 'win32') { 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 iguanaConfsDir = process.env.APPDATA + '/iguana/confs'; iguanaConfsDir = path.normalize(iguanaConfsDir)
var iguanaIcon = path.join(__dirname, '/assets/icons/iguana_app_icon.ico') var iguanaIcon = path.join(__dirname, '/assets/icons/iguana_app_icon.ico')
iguanaConfsDirSrc = path.normalize(iguanaConfsDirSrc); iguanaConfsDirSrc = path.normalize(iguanaConfsDirSrc);
} }
*/
if (os.platform() === 'linux') { var iguanaIcon = path.join(__dirname, '/assets/icons/iguana_app_icon_png/128x128.png') } 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') } 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 // MAKE SURE IGUANA DIR IS THERE FOR USER
mkdirp(iguanaDir, function (err) { mkdirp(iguanaPath, function (err) {
if (err) if (err)
console.error(err) console.error(err)
else else
fs.readdir(iguanaDir, (err, files) => { fs.readdir(iguanaPath, (err, files) => {
files.forEach(file => { files.forEach(file => {
//console.log(file); //console.log(file);
}); });
@ -124,7 +127,7 @@ function createLoadingWindow() {
// DEVTOOLS - only for dev purposes - ca333 // DEVTOOLS - only for dev purposes - ca333
//loadingWindow.webContents.openDevTools() //loadingWindow.webContents.openDevTools()
//launchPM2();
// if window closed we kill iguana proc // if window closed we kill iguana proc
loadingWindow.on('closed', function () { loadingWindow.on('closed', function () {
// our app does not have multiwindow - so we dereference the window object instead of // 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 //ca333 todo - add os detector to use correct binary - so we can use the same bundle on ALL OS platforms
/*if (os.platform() === 'win32') { /*if (os.platform() === 'win32') {
process.chdir(iguanaDir); process.chdir(iguanaPath);
//exec(iguanaWin, {cwd: iguanaDir}); //specify binary in startup //exec(iguanaWin, {cwd: iguanaPath}); //specify binary in startup
ig = spawn(iguanaWin); ig = spawn(iguanaWin);
} }
if (os.platform() === 'linux') { if (os.platform() === 'linux') {
process.chdir(iguanaDir); process.chdir(iguanaPath);
ig = spawn(iguanaLinux); ig = spawn(iguanaLinux);
//corsproxy_process = spawn('corsproxy'); //corsproxy_process = spawn('corsproxy');
} }
if (os.platform() === 'darwin') { if (os.platform() === 'darwin') {
//process.chdir(iguanaDir); //process.chdir(iguanaPath);
//ig = spawn(iguanaOSX); //ig = spawn(iguanaOSX);
//corsproxy_process = spawn('corsproxy'); //corsproxy_process = spawn('corsproxy');
}*/ }*/
@ -155,7 +158,10 @@ function createLoadingWindow() {
app.on('ready', createLoadingWindow) app.on('ready', createLoadingWindow)
function createWindow (status) { function createWindow (status) {
if ( status === 'open') { if ( status === 'open') {
require(path.join(__dirname, 'private/mainmenu')); require(path.join(__dirname, 'private/mainmenu'));
@ -171,6 +177,9 @@ function createWindow (status) {
// if window closed we kill iguana proc // if window closed we kill iguana proc
mainWindow.on('closed', function () { mainWindow.on('closed', function () {
/*
var ConnectToPm2 = function() { var ConnectToPm2 = function() {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
@ -189,7 +198,8 @@ function createWindow (status) {
resolve(result); resolve(result);
}) })
} }
*/
/* //we launch in nodaemonmode - pm2 exits with process
var KillPm2 = function() { var KillPm2 = function() {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
@ -209,7 +219,7 @@ function createWindow (status) {
}, 2000) }, 2000)
}) })
} }
*/
var HideMainWindow = function() { var HideMainWindow = function() {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
@ -232,12 +242,16 @@ function createWindow (status) {
}) })
} }
/* //
ConnectToPm2() ConnectToPm2()
.then(function(result) { .then(function(result) {
return KillPm2(); //return KillPm2();
}) })
.then(HideMainWindow) .then(HideMainWindow)
.then(QuitApp) .then(QuitApp)
*/
}); });
} }
} }
@ -247,14 +261,11 @@ function createWindow (status) {
//}) //})
app.on('window-all-closed', function () { app.on('window-all-closed', function () {
//if (os.platform() !== 'win32') { ig.kill(); } // On OS X it is common for applications and their menu bar
// in osx apps stay active in menu bar until explictly closed or quitted by CMD Q // to stay active until the user quits explicitly with Cmd + Q
// so we do not kill the app --> for the case user clicks again on the iguana icon if (process.platform !== 'darwin') {
// we open just a new window and respawn iguana proc
/*if (process.platform !== 'darwin' || process.platform !== 'linux' || process.platform !== 'win32') {
app.quit() app.quit()
}*/ }
}) })
app.on('activate', function () { app.on('activate', function () {
@ -262,3 +273,59 @@ app.on('activate', function () {
//createWindow('open'); //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);
*/
}

6
package.json

@ -19,7 +19,8 @@
"author": "ca333, grewalsatinder", "author": "ca333, grewalsatinder",
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"electron": "^1.4.1" "electron": "^1.4.1",
"fix-path": "^2.1.0"
}, },
"dependencies": { "dependencies": {
"bluebird": "^3.4.7", "bluebird": "^3.4.7",
@ -29,6 +30,7 @@
"fs-extra": "^1.0.0", "fs-extra": "^1.0.0",
"md5": "^2.2.1", "md5": "^2.2.1",
"mkdirp": "^0.5.1", "mkdirp": "^0.5.1",
"pm2": "^2.2.3" "pm2": "^2.3.0",
"fix-path": "^2.1.0"
} }
} }

36
routes/shepherd.js

@ -11,6 +11,7 @@ const express = require('express');
const md5 = require('md5'); const md5 = require('md5');
const pm2 = require('pm2'); const pm2 = require('pm2');
Promise = require('bluebird'); Promise = require('bluebird');
var fixPath = require('fix-path');
var setconf = require("../private/setconf.js"); 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 OS DIR TO RUN IGUANA FROM
// SETTING APP ICON FOR LINUX AND WINDOWS // SETTING APP ICON FOR LINUX AND WINDOWS
if (os.platform() === 'darwin') { if (os.platform() === 'darwin') {
fixPath();
var iguanaBin = path.join(__dirname, '../assets/bin/osx/iguana'); var iguanaBin = path.join(__dirname, '../assets/bin/osx/iguana');
var iguanaDir = process.env.HOME + '/Library/Application Support/iguana'; var iguanaDir = process.env.HOME + '/Library/Application Support/iguana';
var iguanaConfsDir = iguanaDir + '/confs'; var iguanaConfsDir = iguanaDir + '/confs';
@ -143,23 +145,26 @@ function herder(flock, data) {
console.log('confs files copied successfully at: '+ iguanaConfsDir ) 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') { if (flock === 'komodod') {
@ -185,6 +190,7 @@ function herder(flock, data) {
}); });
}); });
} }
} }

Loading…
Cancel
Save