Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: blockchain logs show in cockpit #1367

Merged
merged 1 commit into from
Mar 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/embark/src/lib/core/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class IPC {
cb = cb || (() => {});
return cb();
}

if (cb) {
this.once(action, cb);
}
Expand Down
10 changes: 5 additions & 5 deletions packages/embark/src/lib/modules/blockchain_listener/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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() {
Expand Down
13 changes: 10 additions & 3 deletions packages/embark/src/lib/modules/blockchain_process/blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}
});

Expand All @@ -135,10 +139,13 @@ Blockchain.prototype.initStandaloneProcess = function () {
// `embark run` without restarting `embark blockchain`)
setInterval(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than use setInterval, my preference would be for a recursive setTimeout. Reason: although unlikely, because the interval is 2000ms, there is a possibility of overlapped calls of this.ipc.connect, i.e. if for some reason the previous invocation's callback hasn't fired yet.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense, agreed.

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(() => {});
});
}
Expand Down