Storage sync logic and some refactoring.

This commit is contained in:
Rodrigo Rodriguez (pragmatismo.io) 2018-10-11 10:53:22 -03:00
parent 37f9fced09
commit eee48ee520
2 changed files with 25 additions and 21 deletions

View file

@ -52,7 +52,7 @@ class GBUIApp extends React.Component {
super(); super();
this.state = { this.state = {
botConnection: null, line: null,
instance: null, instance: null,
token: null, token: null,
instanceClient: null instanceClient: null
@ -63,7 +63,7 @@ class GBUIApp extends React.Component {
sendToken(token) { sendToken(token) {
setTimeout(() => { setTimeout(() => {
window.botConnection window.line
.postActivity({ .postActivity({
type: "event", type: "event",
name: "updateToken", name: "updateToken",
@ -81,7 +81,7 @@ class GBUIApp extends React.Component {
} }
send(command) { send(command) {
window.botConnection window.line
.postActivity({ .postActivity({
type: "event", type: "event",
name: command, name: command,
@ -98,7 +98,7 @@ class GBUIApp extends React.Component {
} }
postEvent(name, value) { postEvent(name, value) {
window.botConnection.postActivity({ window.line.postActivity({
type: "event", type: "event",
value: value, value: value,
from: this.getUser(), from: this.getUser(),
@ -107,7 +107,7 @@ class GBUIApp extends React.Component {
} }
postMessage(value) { postMessage(value) {
window.botConnection.postActivity({ window.line.postActivity({
type: "message", type: "message",
text: value, text: value,
from: this.getUser() from: this.getUser()
@ -175,14 +175,14 @@ class GBUIApp extends React.Component {
let _this_ = this; let _this_ = this;
window["botchatDebug"] = true; window["botchatDebug"] = true;
const botConnection = new DirectLine({ const line = new DirectLine({
secret: this.state.instanceClient.secret secret: this.state.instanceClient.secret
}); });
botConnection.connectionStatus$.subscribe(connectionStatus => { line.connectionStatus$.subscribe(connectionStatus => {
if (connectionStatus === ConnectionStatus.Online) { if (connectionStatus === ConnectionStatus.Online) {
_this_.setState({ botConnection: botConnection }); _this_.setState({ line: line });
botConnection.postActivity({ line.postActivity({
type: "event", type: "event",
value: "startGB", value: "startGB",
from: this.getUser(), from: this.getUser(),
@ -191,10 +191,10 @@ class GBUIApp extends React.Component {
} }
}); });
window.botConnection = botConnection; window.line = line;
this.postEvent("startGB", true); this.postEvent("startGB", true);
botConnection.activity$ line.activity$
.filter( .filter(
activity => activity =>
activity.type === "event" && activity.name === "loadInstance" activity.type === "event" && activity.name === "loadInstance"
@ -204,7 +204,7 @@ class GBUIApp extends React.Component {
_this_.authenticate() _this_.authenticate()
}); });
botConnection.activity$ line.activity$
.filter(activity => activity.type === "event" && activity.name === "stop") .filter(activity => activity.type === "event" && activity.name === "stop")
.subscribe(activity => { .subscribe(activity => {
if (_this_.player) { if (_this_.player) {
@ -212,7 +212,7 @@ class GBUIApp extends React.Component {
} }
}); });
botConnection.activity$ line.activity$
.filter(activity => activity.type === "event" && activity.name === "play") .filter(activity => activity.type === "event" && activity.name === "play")
.subscribe(activity => { .subscribe(activity => {
_this_.setState({ playerType: activity.value.playerType }); _this_.setState({ playerType: activity.value.playerType });
@ -312,7 +312,7 @@ class GBUIApp extends React.Component {
</div> </div>
); );
if (this.state.botConnection && this.state.instance) { if (this.state.line && this.state.instance) {
let token = this.state.instanceClient.speechToken; let token = this.state.instanceClient.speechToken;
gbCss = <GBCss instance={this.state.instance} />; gbCss = <GBCss instance={this.state.instance} />;
@ -342,7 +342,7 @@ class GBUIApp extends React.Component {
this.chat = chat; this.chat = chat;
}} }}
locale={'pt-br'} locale={'pt-br'}
botConnection={this.state.botConnection} line={this.state.line}
user={this.getUser()} user={this.getUser()}
bot={{ id: "bot@gb", name: "Bot" }} bot={{ id: "bot@gb", name: "Bot" }}
speechOptions={speechOptions} speechOptions={speechOptions}

View file

@ -145,17 +145,21 @@ export class GBServer {
try { try {
instances = await core.loadInstances(); instances = await core.loadInstances();
} catch (error) { } catch (error) {
// Check if storage is empty and needs formatting. // Check if storage is empty and needs formatting.
let isInvalidObject = let isInvalidObject =
error.parent.number == 208 || error.parent.errno == 1; // MSSQL or SQLITE. error.parent.number == 208 || error.parent.errno == 1; // MSSQL or SQLITE.
if (
isInvalidObject && if (isInvalidObject) {
GBConfigService.get("STORAGE_SYNC") !== "true" if (GBConfigService.get("STORAGE_SYNC") != "true") {
) {
throw `Operating storage is out of sync or there is a storage connection error. Try setting STORAGE_SYNC to true in .env file. Error: ${ throw `Operating storage is out of sync or there is a storage connection error. Try setting STORAGE_SYNC to true in .env file. Error: ${
error.message error.message
}.`; }.`;
}
else{
logger.info(`Storage is empty. After collecting storage structure from all .gbapps it will get synced.`);
}
} else { } else {
throw `Cannot connect to operating storage: ${error.message}.`; throw `Cannot connect to operating storage: ${error.message}.`;
} }