Skip to content

Commit

Permalink
fix(core.gbapp): Optimization of BASIC loading.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigorodriguez committed Jun 4, 2020
1 parent 7b84ee1 commit b268882
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 47 deletions.
47 changes: 29 additions & 18 deletions packages/core.gbapp/services/GBCoreService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,16 @@ export class GBCoreService implements IGBCoreService {
*/
public async loadInstances(): Promise<IGBInstance[]> {
if (process.env.LOAD_ONLY !== undefined) {
const options = { where: { botId: process.env.LOAD_ONLY } };

const bots = process.env.LOAD_ONLY.split(`;`);
const and = [];
await CollectionUtil.asyncForEach(bots, async e => {
and.push({botId: e});
});

const options = {where: {
[Op.or]: and
}};
return GuaribasInstance.findAll(options);
}
else {
Expand Down Expand Up @@ -349,24 +358,26 @@ STORAGE_SYNC=true
let instances: IGBInstance[];
try {
instances = await core.loadInstances();
await CollectionUtil.asyncForEach(instances, async instance => {
GBLog.info(`Updating bot endpoint for ${instance.botId}...`);
try {
await installationDeployer.updateBotProxy(
instance.botId,
GBConfigService.get("CLOUD_GROUP"),
`${proxyAddress}/api/messages/${instance.botId}`
);
} catch (error) {
if (error.code === "ResourceNotFound") {
GBLog.warn(`Bot ${instance.botId} not found on resource group ${GBConfigService.get("CLOUD_GROUP")}.`);
}
else {

throw new Error(`Error updating bot proxy, details: ${error}.`);
if (process.env.ENDPOINT_UPDATE === "true") {
await CollectionUtil.asyncForEach(instances, async instance => {
GBLog.info(`Updating bot endpoint for ${instance.botId}...`);
try {
await installationDeployer.updateBotProxy(
instance.botId,
GBConfigService.get("CLOUD_GROUP"),
`${proxyAddress}/api/messages/${instance.botId}`
);
} catch (error) {
if (error.code === "ResourceNotFound") {
GBLog.warn(`Bot ${instance.botId} not found on resource group ${GBConfigService.get("CLOUD_GROUP")}.`);
}
else {

throw new Error(`Error updating bot proxy, details: ${error}.`);
}
}
}
});
});
}
} catch (error) {
if (error.parent === undefined) {
throw new Error(`Cannot connect to operating storage: ${error.message}.`);
Expand Down
77 changes: 53 additions & 24 deletions packages/core.gbapp/services/GBVMService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,26 @@ export class GBVMService extends GBService {

let filename: string = file.name;

if (
filename.endsWith('.vbs') ||
filename.endsWith('.vb') ||
filename.endsWith('.basic') ||
filename.endsWith('.bas') ||
filename.endsWith('.docx')
) {

if (filename.endsWith('.docx')) {
let text = await this.getTextFromWord(folder, filename);
filename = filename.substr(0, filename.indexOf('docx')) + 'vbs';
fs.writeFileSync(urlJoin(folder, filename), text);
if (filename.endsWith('.docx')) {
const wordFile = filename;
const vbsFile = filename.substr(0, filename.indexOf('docx')) + 'vbs';
const fullVbsFile = urlJoin(folder, vbsFile);
const docxStat = fs.statSync(urlJoin(folder, wordFile));
const interval = 30000; // If compiled is older 30 seconds, then recompile.
let writeVBS = true;
if (fs.existsSync(fullVbsFile)) {
const vbsStat = fs.statSync(fullVbsFile);
if (docxStat.mtimeMs < (vbsStat.mtimeMs + interval)) {
writeVBS = false;
}
}
if (writeVBS) {

let text = await this.getTextFromWord(folder, wordFile);
fs.writeFileSync(urlJoin(folder, vbsFile), text);
}

filename = vbsFile;

let mainName = filename.replace(/\s|\-/gi, '').split('.')[0];
mainName = mainName.toLowerCase();
Expand All @@ -98,7 +105,24 @@ export class GBVMService extends GBService {
// await this.run(fullFilename, min, deployer, mainName);
// });

await this.run(fullFilename, min, deployer, mainName);
const compiledAt = fs.statSync(fullFilename);
const jsfile = urlJoin(folder, `${filename}.js`);

if (fs.existsSync(jsfile)) {
const jsStat = fs.statSync(jsfile);
const interval = 30000; // If compiled is older 30 seconds, then recompile.
if (compiledAt.isFile() && compiledAt.mtimeMs > (jsStat.mtimeMs + interval)) {
await this.executeBASIC(fullFilename, min, deployer, mainName);
}
else {
const parsedCode: string = fs.readFileSync(jsfile, 'utf8');
this.executeJS(min, deployer, parsedCode, mainName);
}
}
else {
await this.executeBASIC(fullFilename, min, deployer, mainName);
}

}
})
);
Expand Down Expand Up @@ -180,7 +204,7 @@ export class GBVMService extends GBService {
return code;
}

public async run(filename: any, min: GBMinInstance, deployer: GBDeployer, mainName: string) {
public async executeBASIC(filename: any, min: GBMinInstance, deployer: GBDeployer, mainName: string) {
// Converts General Bots BASIC into regular VBS

const basicCode: string = fs.readFileSync(filename, 'utf8');
Expand Down Expand Up @@ -272,15 +296,20 @@ export class GBVMService extends GBService {
parsedCode = beautify(parsedCode, { indent_size: 2, space_in_empty_paren: true })
fs.writeFileSync(jsfile, parsedCode);

try {
const sandbox: DialogClass = new DialogClass(min, deployer);
const context = vm.createContext(sandbox);
vm.runInContext(parsedCode, context);
min.sandBoxMap[mainName.toLowerCase()] = sandbox;
GBLog.info(`[GBVMService] Finished loading of ${filename}`);
} catch (error) {
GBLog.error(`[GBVMService] ERROR loading ${error}`);
}
this.executeJS(min, deployer, parsedCode, mainName);
GBLog.info(`[GBVMService] Finished loading of ${filename}`);
}
}

private executeJS(min: GBMinInstance, deployer: GBDeployer, parsedCode: string, mainName: string) {
try {
const sandbox: DialogClass = new DialogClass(min, deployer);
const context = vm.createContext(sandbox);
vm.runInContext(parsedCode, context);
min.sandBoxMap[mainName.toLowerCase()] = sandbox;
}
catch (error) {
GBLog.error(`[GBVMService] ERROR loading ${error}`);
}
}

Expand Down Expand Up @@ -320,7 +349,7 @@ export class GBVMService extends GBService {
step.activeDialog.state.options = {};
step.activeDialog.state.options.cbId = (step.options as any).id;
step.activeDialog.state.options.previousResolve = (step.options as any).previousResolve;
return await min.conversationalService.prompt (min, step,null);
return await min.conversationalService.prompt(min, step, null);
},
async step => {
const cbId = step.activeDialog.state.options.cbId;
Expand Down
12 changes: 7 additions & 5 deletions packages/whatsapp.gblib/services/WhatsappDirectLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,13 @@ export class WhatsappDirectLine extends GBService {
const express = require('express');
GBServer.globals.server.use(`/audios`, express.static('work'));

try {
let res = await request.post(options);
GBLog.info(res);
} catch (error) {
GBLog.error(`Error initializing 3rd party Whatsapp provider(1) ${error.message}`);
if (process.env.ENDPOINT_UPDATE === "true") {
try {
let res = await request.post(options);
GBLog.info(res);
} catch (error) {
GBLog.error(`Error initializing 3rd party Whatsapp provider(1) ${error.message}`);
}
}
}

Expand Down

0 comments on commit b268882

Please sign in to comment.