From 41256cfb00e5ac159e8bd433faf863d98073df51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Medeiros?= Date: Tue, 5 Mar 2019 14:14:41 -0500 Subject: [PATCH] fix: blockchain logs show in cockpit (#1367) --- packages/embark/src/lib/core/ipc.js | 1 + .../src/lib/modules/blockchain_listener/index.js | 10 +++++----- .../lib/modules/blockchain_process/blockchain.js | 13 ++++++++++--- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/packages/embark/src/lib/core/ipc.js b/packages/embark/src/lib/core/ipc.js index 30ee5c88c..4f70e7fb9 100644 --- a/packages/embark/src/lib/core/ipc.js +++ b/packages/embark/src/lib/core/ipc.js @@ -118,6 +118,7 @@ class IPC { cb = cb || (() => {}); return cb(); } + if (cb) { this.once(action, cb); } diff --git a/packages/embark/src/lib/modules/blockchain_listener/index.js b/packages/embark/src/lib/modules/blockchain_listener/index.js index 527506313..67934dbca 100644 --- a/packages/embark/src/lib/modules/blockchain_listener/index.js +++ b/packages/embark/src/lib/modules/blockchain_listener/index.js @@ -6,7 +6,7 @@ const PROCESS_NAME = 'blockchain'; * BlockchainListener has two functions: * 1. Register API endpoints (HTTP GET and WS) to retrieve blockchain logs * when in standalone mode (ie `embark blockchain`). - * 2. Listen to log events from the IPC connection (to `embark blockchain`) + * 2. Listen to log events from the IPC connection (to `embark blockchain`) * and ensure they are processed through the LogHandler. */ class BlockchainListener { @@ -22,20 +22,20 @@ class BlockchainListener { this.events = embark.events; this.logger = embark.logger; this.ipc = ipc; - this.processLogsApi = new ProcessLogsApi({embark: this.embark, processName: PROCESS_NAME, silent: true}); - if (this.ipc.isServer()) { + this.ipc.server.once('connect', () => { + this.processLogsApi = new ProcessLogsApi({embark: this.embark, processName: PROCESS_NAME, silent: true}); this._listenToBlockchainLogs(); this._listenToCommands(); this._registerConsoleCommands(); this._registerApiEndpoint(); - } + }); } /** * Listens to log events emitted by the standalone blockchain and ensures * they are processed through the LogHandler. - * + * * @return {void} */ _listenToBlockchainLogs() { diff --git a/packages/embark/src/lib/modules/blockchain_process/blockchain.js b/packages/embark/src/lib/modules/blockchain_process/blockchain.js index 9da45f8af..835950a7d 100644 --- a/packages/embark/src/lib/modules/blockchain_process/blockchain.js +++ b/packages/embark/src/lib/modules/blockchain_process/blockchain.js @@ -120,11 +120,15 @@ var Blockchain = function(userConfig, clientClass) { */ Blockchain.prototype.initStandaloneProcess = function () { if (this.isStandalone) { + let logQueue = []; + // on every log logged in logger (say that 3x fast), send the log // to the IPC serve listening (only if we're connected of course) this.logger.events.on('log', (logLevel, message) => { if (this.ipc.connected) { this.ipc.request('blockchain:log', {logLevel, message}); + } else { + logQueue.push({logLevel, message}); } }); @@ -135,10 +139,13 @@ Blockchain.prototype.initStandaloneProcess = function () { // `embark run` without restarting `embark blockchain`) setInterval(() => { if (!this.ipc.connected) { - this.ipc.connect(() => { + this.ipc.connect(() => { if (this.ipc.connected) { - this.ipc.listenTo('regularTxs', (mode) => { - if(mode === 'start') this.startRegularTxs(() => {}); + logQueue.forEach(message => { this.ipc.request('blockchain:log', message); }); + logQueue = []; + + this.ipc.listenTo('regularTxs', (mode) => { + if(mode === 'start') this.startRegularTxs(() => {}); else if (mode === 'stop') this.stopRegularTxs(() => {}); }); }