Browse Source

Merge pull request #40 from pbca26/master

added komodo debug.log truncate on herd start
all-modes
Satinder Grewal 8 years ago
committed by GitHub
parent
commit
8cfef99edb
  1. 481
      main.js
  2. 14
      routes/shepherd.js

481
main.js

@ -2,112 +2,111 @@
//this app spawns iguana in background in nontech-mode //this app spawns iguana in background in nontech-mode
const electron = require('electron'), const electron = require('electron'),
app = electron.app, app = electron.app,
BrowserWindow = electron.BrowserWindow, BrowserWindow = electron.BrowserWindow,
path = require('path'), path = require('path'),
url = require('url'), url = require('url'),
os = require('os'), os = require('os'),
spawn = require('child_process').spawn, spawn = require('child_process').spawn,
exec = require('child_process').exec, exec = require('child_process').exec,
fixPath = require('fix-path'); fixPath = require('fix-path');
var express = require('express'), var express = require('express'),
bodyParser = require('body-parser'), bodyParser = require('body-parser'),
fs = require('fs'), fs = require('fs'),
fsnode = require('fs'), fsnode = require('fs'),
fs = require('fs-extra'), fs = require('fs-extra'),
mkdirp = require('mkdirp'), mkdirp = require('mkdirp'),
pm2 = require('pm2'); pm2 = require('pm2');
cluster = require('cluster'); cluster = require('cluster');
numCPUs = require('os').cpus().length; numCPUs = require('os').cpus().length;
Promise = require('bluebird'); Promise = require('bluebird');
app.setName('Iguana'); app.setName('Iguana');
if (os.platform() === 'linux') { if (os.platform() === 'linux') {
process.env.ELECTRON_RUN_AS_NODE = true; process.env.ELECTRON_RUN_AS_NODE = true;
console.log(process.env); console.log(process.env);
} }
// GUI APP settings and starting gui on address http://120.0.0.1:17777 // GUI APP settings and starting gui on address http://120.0.0.1:17777
var shepherd = require('./routes/shepherd'), var shepherd = require('./routes/shepherd'),
guiapp = express(); guiapp = express(),
appConfig = shepherd.loadLocalConfig(); // load app config
guiapp.use(function(req, res, next) { guiapp.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "http://127.0.0.1:17777"); res.header('Access-Control-Allow-Origin', 'http://127.0.0.1:' + appConfig.iguanaAppPort);
res.header("Access-Control-Allow-Headers", "X-Requested-With"); res.header('Access-Control-Allow-Headers', 'X-Requested-With');
res.header("Access-Control-Allow-Credentials", "true"); res.header('Access-Control-Allow-Credentials', 'true');
res.header("Access-Control-Allow-Headers", "Content-Type"); res.header('Access-Control-Allow-Headers', 'Content-Type');
res.header("Access-Control-Allow-Methods", "PUT, GET, POST, DELETE, OPTIONS"); res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS'); // TODO: limit allowed methods
next(); next();
}); });
var appConfig = shepherd.loadLocalConfig(); // load app config
// preload.js // preload.js
const _setImmediate = setImmediate, const _setImmediate = setImmediate,
_clearImmediate = clearImmediate; _clearImmediate = clearImmediate;
process.once('loaded', () => { process.once('loaded', () => {
global.setImmediate = _setImmediate; global.setImmediate = _setImmediate;
global.clearImmediate = _clearImmediate; global.clearImmediate = _clearImmediate;
if (os.platform() === 'darwin') { if (os.platform() === 'darwin') {
process.setFdLimit(appConfig.maxDescriptors.darwin); process.setFdLimit(appConfig.maxDescriptors.darwin);
} }
if (os.platform() === 'linux') { if (os.platform() === 'linux') {
process.setFdLimit(appConfig.maxDescriptors.linux); process.setFdLimit(appConfig.maxDescriptors.linux);
} }
}); });
guiapp.use(bodyParser.json()); // support json encoded bodies guiapp.use(bodyParser.json()); // support json encoded bodies
guiapp.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies guiapp.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
guiapp.get('/', function (req, res) { guiapp.get('/', function (req, res) {
res.send('Iguana app server'); res.send('Iguana app server');
}); });
var guipath = path.join(__dirname, '/gui'); var guipath = path.join(__dirname, '/gui');
guiapp.use('/gui', express.static(guipath)); guiapp.use('/gui', express.static(guipath));
guiapp.use('/shepherd', shepherd); guiapp.use('/shepherd', shepherd);
/*if (cluster.isMaster && process.env.NODE_ENV !== "development") { /*if (cluster.isMaster && process.env.NODE_ENV !== "development") {
for (var i = 0; i < numCPUs; i++) { for (var i = 0; i < numCPUs; i++) {
cluster.fork(); cluster.fork();
} }
} }
console.log(cluster) console.log(cluster)
*/ */
/*var rungui = guiapp.listen(appConfig.iguanaAppPort, function () { /*var rungui = guiapp.listen(appConfig.iguanaAppPort, function () {
console.log('guiapp listening on port ' + appConfig.iguanaAppPort + '!'); console.log('guiapp listening on port ' + appConfig.iguanaAppPort + '!');
});*/ });*/
var server = require('http').createServer(guiapp); var server = require('http').createServer(guiapp),
var io = require('socket.io').listen(server); io = require('socket.io').listen(server);
server.listen(appConfig.iguanaAppPort, function() { server.listen(appConfig.iguanaAppPort, function() {
console.log('guiapp and sockets.io are listening on port ' + appConfig.iguanaAppPort + '!'); console.log('guiapp and sockets.io are listening on port ' + appConfig.iguanaAppPort + '!');
}); });
io.set('origins', 'http://127.0.0.1:17777'); io.set('origins', 'http://127.0.0.1:17777'); // set origin
io.on('connection', function(client) { io.on('connection', function(client) {
console.log('EDEX GUI is connected...'); console.log('EDEX GUI is connected...');
client.on('event', function(data) { client.on('event', function(data) { // listen for client requests
console.log(data); console.log(data);
}); });
client.on('disconnect', function(data) { client.on('disconnect', function(data) {
console.log('EDEX GUI is disconnected'); console.log('EDEX GUI is disconnected');
}); });
client.on('join', function(data) { client.on('join', function(data) {
console.log(data); console.log(data);
client.emit('messages', 'Sockets server is listening'); client.emit('messages', 'Sockets server is listening');
}); });
}); });
shepherd.setIO(io); shepherd.setIO(io); // pass sockets object to shepherd router
//io.emit('an event sent to all connected clients'); //io.emit('an event sent to all connected clients');
@ -125,47 +124,47 @@ 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 iguanaDir = process.env.HOME + '/Library/Application Support/iguana';
var iguanaConfsDir = iguanaDir + '/confs'; var iguanaConfsDir = iguanaDir + '/confs';
} }
if (os.platform() === 'linux') { if (os.platform() === 'linux') {
var iguanaDir = process.env.HOME + '/.iguana' var iguanaDir = process.env.HOME + '/.iguana'
var iguanaConfsDir = iguanaDir + '/confs'; var iguanaConfsDir = iguanaDir + '/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 iguanaDir = process.env.APPDATA + '/iguana'; iguanaDir = path.normalize(iguanaDir)
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') { if (os.platform() === 'linux') {
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 iguanaIcon = path.join(__dirname, '/assets/icons/iguana_app_icon.ico'); var iguanaIcon = path.join(__dirname, '/assets/icons/iguana_app_icon.ico');
} }
//console.log(iguanaDir); //console.log(iguanaDir);
/* /*
// MAKE SURE IGUANA DIR IS THERE FOR USER // MAKE SURE IGUANA DIR IS THERE FOR USER
mkdirp(iguanaDir, function (err) { mkdirp(iguanaDir, function (err) {
if (err) if (err)
console.error(err) console.error(err)
else else
fs.readdir(iguanaDir, (err, files) => { fs.readdir(iguanaDir, (err, files) => {
files.forEach(file => { files.forEach(file => {
//console.log(file); //console.log(file);
}); });
}) })
}); });
// COPY CONFS DIR WITH PEERS FILE TO IGUANA DIR, AND KEEP IT IN SYNC // COPY CONFS DIR WITH PEERS FILE TO IGUANA DIR, AND KEEP IT IN SYNC
fs.copy(iguanaConfsDirSrc, iguanaConfsDir, function (err) { fs.copy(iguanaConfsDirSrc, iguanaConfsDir, function (err) {
if (err) return console.error(err) if (err) return console.error(err)
console.log('confs files copied successfully at: '+ iguanaConfsDir ) console.log('confs files copied successfully at: '+ iguanaConfsDir )
}) })
*/ */
@ -173,192 +172,192 @@ let mainWindow;
let loadingWindow; let loadingWindow;
function createLoadingWindow() { function createLoadingWindow() {
mainWindow = null; mainWindow = null;
// initialise window // initialise window
loadingWindow = new BrowserWindow({ loadingWindow = new BrowserWindow({
width: 500, width: 500,
height: 300, height: 300,
frame: false, frame: false,
icon: iguanaIcon icon: iguanaIcon
}); });
// load our index.html (i.e. easyDEX GUI) // load our index.html (i.e. easyDEX GUI)
loadingWindow.loadURL('http://' + appConfig.host + ':' + appConfig.iguanaAppPort + '/gui/'); loadingWindow.loadURL('http://' + appConfig.host + ':' + appConfig.iguanaAppPort + '/gui/');
// DEVTOOLS - only for dev purposes - ca333 // DEVTOOLS - only for dev purposes - ca333
//loadingWindow.webContents.openDevTools() //loadingWindow.webContents.openDevTools()
// if window closed we kill iguana proc // if window closed we kill iguana proc
loadingWindow.on('hide', function () { loadingWindow.on('hide', 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
// putting them into an window_arr // putting them into an window_arr
loadingWindow = null; loadingWindow = null;
createWindow('open'); createWindow('open');
}) })
//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(iguanaDir);
//exec(iguanaWin, {cwd: iguanaDir}); //specify binary in startup //exec(iguanaWin, {cwd: iguanaDir}); //specify binary in startup
ig = spawn(iguanaWin); ig = spawn(iguanaWin);
} }
if (os.platform() === 'linux') { if (os.platform() === 'linux') {
process.chdir(iguanaDir); process.chdir(iguanaDir);
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(iguanaDir);
//ig = spawn(iguanaOSX); //ig = spawn(iguanaOSX);
//corsproxy_process = spawn('corsproxy'); //corsproxy_process = spawn('corsproxy');
}*/ }*/
//if (os.platform() !== 'win32') { ig.stderr.on( 'error: ', data => { console.log( `stderr: ${data}` ); }); } //if (os.platform() !== 'win32') { ig.stderr.on( 'error: ', data => { console.log( `stderr: ${data}` ); }); }
} }
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'));
// initialise window // initialise window
mainWindow = new BrowserWindow({ mainWindow = new BrowserWindow({
width: 1280, width: 1280,
height: 800, height: 800,
icon: iguanaIcon icon: iguanaIcon
}); });
// load our index.html (i.e. easyDEX GUI) // load our index.html (i.e. easyDEX GUI)
if (appConfig.edexGuiOnly) { if (appConfig.edexGuiOnly) {
mainWindow.loadURL('http://' + appConfig.host + ':' + appConfig.iguanaAppPort + '/gui/EasyDEX-GUI/'); mainWindow.loadURL('http://' + appConfig.host + ':' + appConfig.iguanaAppPort + '/gui/EasyDEX-GUI/');
} else { } else {
mainWindow.loadURL('http://' + appConfig.host + ':' + appConfig.iguanaAppPort + '/gui/main.html'); mainWindow.loadURL('http://' + appConfig.host + ':' + appConfig.iguanaAppPort + '/gui/main.html');
} }
// DEVTOOLS - only for dev purposes - ca333 // DEVTOOLS - only for dev purposes - ca333
//mainWindow.webContents.openDevTools() //mainWindow.webContents.openDevTools()
// 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) {
console.log('Closing Main Window...'); console.log('Closing Main Window...');
pm2.connect(true, function(err) { pm2.connect(true, function(err) {
console.log('connecting to pm2...'); console.log('connecting to pm2...');
if (err) { if (err) {
console.log(err); console.log(err);
} }
}); });
var result = 'Connecting To Pm2: done'; var result = 'Connecting To Pm2: done';
console.log(result); console.log(result);
resolve(result); resolve(result);
}) })
} }
var KillPm2 = function() { var KillPm2 = function() {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
console.log('killing to pm2...'); console.log('killing to pm2...');
pm2.killDaemon(function(err) { pm2.killDaemon(function(err) {
pm2.disconnect(); pm2.disconnect();
console.log('killed to pm2...'); console.log('killed to pm2...');
if (err) if (err)
throw err; throw err;
}); });
var result = 'Killing Pm2: done'; var result = 'Killing Pm2: done';
setTimeout(function() { setTimeout(function() {
console.log(result); console.log(result);
resolve(result); resolve(result);
}, 2000) }, 2000)
}) })
} }
var HideMainWindow = function() { var HideMainWindow = function() {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
console.log('Exiting App...'); console.log('Exiting App...');
mainWindow = null; mainWindow = null;
var result = 'Hiding Main Window: done'; var result = 'Hiding Main Window: done';
console.log(result); console.log(result);
resolve(result); resolve(result);
}); });
} }
var QuitApp = function() { var QuitApp = function() {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
app.quit(); app.quit();
var result = 'Quiting App: done'; var result = 'Quiting App: done';
console.log(result); console.log(result);
resolve(result); resolve(result);
}); });
} }
ConnectToPm2() ConnectToPm2()
.then(function(result) { .then(function(result) {
return KillPm2(); return KillPm2();
}) })
.then(HideMainWindow) .then(HideMainWindow)
.then(QuitApp); .then(QuitApp);
}); });
} }
} }
//app.on('ready', function() { //app.on('ready', function() {
//createLoadingWindow //createLoadingWindow
//}) //})
app.on('window-all-closed', function () { app.on('window-all-closed', function () {
//if (os.platform() !== 'win32') { ig.kill(); } //if (os.platform() !== 'win32') { ig.kill(); }
// in osx apps stay active in menu bar until explictly closed or quitted by CMD Q // 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 // 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 // we open just a new window and respawn iguana proc
/*if (process.platform !== 'darwin' || process.platform !== 'linux' || process.platform !== 'win32') { /*if (process.platform !== 'darwin' || process.platform !== 'linux' || process.platform !== 'win32') {
app.quit() app.quit()
}*/ }*/
}) })
//Emitted before the application starts closing its windows. //Emitted before the application starts closing its windows.
//Calling event.preventDefault() will prevent the default behaviour, which is terminating the application. //Calling event.preventDefault() will prevent the default behaviour, which is terminating the application.
app.on('before-quit', function (event) { app.on('before-quit', function (event) {
if (mainWindow === null && loadingWindow != null) { //mainWindow not intitialised and loadingWindow not dereferenced if (mainWindow === null && loadingWindow != null) { //mainWindow not intitialised and loadingWindow not dereferenced
//loading window is still open //loading window is still open
console.log('before-quit prevented'); console.log('before-quit prevented');
event.preventDefault(); event.preventDefault();
} }
}); });
//Emitted when all windows have been closed and the application will quit. //Emitted when all windows have been closed and the application will quit.
//Calling event.preventDefault() will prevent the default behaviour, which is terminating the application. //Calling event.preventDefault() will prevent the default behaviour, which is terminating the application.
app.on('will-quit', function (event) { app.on('will-quit', function (event) {
if (mainWindow === null && loadingWindow != null) { if (mainWindow === null && loadingWindow != null) {
//loading window is still open //loading window is still open
console.log('will-quit while loading window active'); console.log('will-quit while loading window active');
event.preventDefault(); event.preventDefault();
} }
}); });
//Emitted when the application is quitting. //Emitted when the application is quitting.
//Calling event.preventDefault() will prevent the default behaviour, which is terminating the application. //Calling event.preventDefault() will prevent the default behaviour, which is terminating the application.
app.on('quit', function (event) { app.on('quit', function (event) {
if (mainWindow === null && loadingWindow != null) { if (mainWindow === null && loadingWindow != null) {
console.log('quit while loading window active'); console.log('quit while loading window active');
event.preventDefault(); event.preventDefault();
} }
}) })
app.on('activate', function () { app.on('activate', function () {
if (mainWindow === null) { if (mainWindow === null) {
//createWindow('open'); //createWindow('open');
} }
}); });

14
routes/shepherd.js

@ -311,7 +311,8 @@ shepherd.get('/cache-all', function(req, res, next) {
}); });
outObj.basilisk[coin].addresses = JSON.parse(body).result; outObj.basilisk[coin].addresses = JSON.parse(body).result;
writeCache(); writeCache();
callStack[coin] = callStack[coin] + outObj.basilisk[coin].addresses.length * (coin === 'BTC' || coin === 'SYS' ? 2 : 4); var addrCount = outObj.basilisk[coin].addresses ? outObj.basilisk[coin].addresses.length : 0;
callStack[coin] = callStack[coin] + addrCount * (coin === 'BTC' || coin === 'SYS' ? 2 : 4);
console.log(coin + ' stack len ' + callStack[coin]); console.log(coin + ' stack len ' + callStack[coin]);
async.each(outObj.basilisk[coin].addresses, function(address) { async.each(outObj.basilisk[coin].addresses, function(address) {
@ -613,7 +614,8 @@ shepherd.get('/cache-one', function(req, res, next) {
outObj.basilisk[coin].addresses = JSON.parse(body).result; outObj.basilisk[coin].addresses = JSON.parse(body).result;
console.log(JSON.parse(body).result); console.log(JSON.parse(body).result);
writeCache(); writeCache();
callStack[coin] = callStack[coin] + outObj.basilisk[coin].addresses.length * (coin === 'BTC' || coin === 'SYS' ? callsArray.length - 2 : callsArray.length); var addrCount = outObj.basilisk[coin].addresses ? outObj.basilisk[coin].addresses.length : 0;
callStack[coin] = callStack[coin] + addrCount * (coin === 'BTC' || coin === 'SYS' ? callsArray.length - 2 : callsArray.length);
console.log(coin + ' stack len ' + callStack[coin]); console.log(coin + ' stack len ' + callStack[coin]);
async.each(outObj.basilisk[coin].addresses, function(address) { async.each(outObj.basilisk[coin].addresses, function(address) {
@ -1048,9 +1050,8 @@ shepherd.readDebugLog = function(fileLocation, lastNLines) {
_fs.readFile(fileLocation, 'utf-8', function(err, data) { _fs.readFile(fileLocation, 'utf-8', function(err, data) {
if (err) throw err; if (err) throw err;
// TODO: truncate komodod debug.log on app start var lines = data.trim().split('\n'),
var lines = data.trim().split('\n'); lastLine = lines.slice(lines.length - lastNLines, lines.length).join('\n');
var lastLine = lines.slice(lines.length - lastNLines, lines.length).join('\n');
resolve(lastLine); resolve(lastLine);
}); });
} }
@ -1156,6 +1157,9 @@ function herder(flock, data) {
console.log('komodod flock selected...'); console.log('komodod flock selected...');
console.log('selected data: ' + data); console.log('selected data: ' + data);
// truncate debug.log
fs.unlink(komodoDir + '/debug.log');
pm2.connect(true, function(err) { // start up pm2 god pm2.connect(true, function(err) { // start up pm2 god
if (err) { if (err) {
console.error(err); console.error(err);

Loading…
Cancel
Save