fix(@embark/storage): revise timing for process:started and code eval to avoid race conditions

This commit is contained in:
Michael Bradley, Jr 2019-07-10 13:09:40 -05:00 committed by Michael Bradley
parent 2531fc1d26
commit 5828ae6cc5
5 changed files with 37 additions and 33 deletions

View File

@ -96,11 +96,7 @@ export default class CodeRunner {
console.dir(err);
return;
}
this.events.request("code-generator:embarkjs:set-provider-code", (providerCode: string) => {
this.evalCode(providerCode, (errInitProvider, _result) => {
if (cb) { cb(errInitProvider); }
}, false);
});
if (cb) { cb(); }
}, false);
});
}

View File

@ -1,4 +1,5 @@
import { __ } from 'embark-i18n';
import * as async from 'async';
class Storage {
constructor(embark, options){
@ -50,11 +51,31 @@ class Storage {
};
this.embark.addProviderInit('storage', code, shouldInit);
this.embark.events.request("runcode:storage:providerRegistered", () => {
async.parallel([
(next) => {
if (!this.storageConfig.available_providers.includes('ipfs')) {
return next();
}
this.embark.events.once('ipfs:process:started', next);
},
(next) => {
if (!this.storageConfig.available_providers.includes('swarm')) {
return next();
}
this.embark.events.once('swarm:process:started', next);
}
], (err) => {
if (err) {
console.error(__('Error starting storage process(es): %s', err));
}
this.embark.addConsoleProviderInit('storage', code, shouldInit);
this.embark.events.request("runcode:storage:providerSet", () => {
cb();
});
// TODO: fix me, this is an ugly workaround for race conditions
// in the case where the storage process is too slow when starting up we
// execute ourselves the setProviders because the console provider init
// was already executed
this.embark.events.request('runcode:eval', `if (Object.keys(EmbarkJS.Storage.Providers).length) { ${code} }`, cb, true);
});
}

View File

@ -1,4 +1,3 @@
import { __ } from 'embark-i18n';
import { ProcessManager, IPC } from 'embark-core';
const async = require('async');
@ -270,27 +269,13 @@ class Engine {
}
storageService(_options) {
async.parallel([
(next) => {
if (!this.config.storageConfig.available_providers.includes("ipfs")) {
return next();
}
this.events.once("ipfs:process:started", next);
this.registerModulePackage('embark-ipfs');
},
(next) => {
if (!this.config.storageConfig.available_providers.includes("swarm")) {
return next();
}
this.events.once("swarm:process:started", next);
this.registerModulePackage('embark-swarm');
}
], (err) => {
if(err) {
console.error(__("Error starting storage process(es): %s", err));
}
this.registerModulePackage('embark-storage', {plugins: this.plugins});
});
if (this.config.storageConfig.available_providers.includes("ipfs")) {
this.registerModulePackage('embark-ipfs');
}
if (this.config.storageConfig.available_providers.includes("swarm")) {
this.registerModulePackage('embark-swarm');
}
this.registerModulePackage('embark-storage', {plugins: this.plugins});
}
web3Service(options) {

View File

@ -58,7 +58,8 @@
"dependencies": {
"@babel/runtime-corejs2": "7.3.1",
"async": "2.6.1",
"async-es": "2.6.1"
"async-es": "2.6.1",
"colors": "1.3.2"
},
"devDependencies": {
"@babel/cli": "7.2.3",

View File

@ -1,4 +1,5 @@
import {detectSeries} from './async';
require('colors');
const Storage = {};
@ -92,7 +93,7 @@ Storage.setProviders = function (dappConnOptions, addlOpts) {
callback(null, false);
}
}, function (err, result) {
if (!result) console.error('Could not connect to a storage provider using any of the dappConnections in the storage config');
if (!result) console.log('Could not connect to a storage provider using any of the dappConnections in the storage config'.red);
});
};