fix(core.gbapp): Optimization of BASIC loading.

This commit is contained in:
Rodrigo Rodriguez 2020-06-04 13:44:02 -03:00
parent 7b84ee112d
commit b268882225
3 changed files with 88 additions and 46 deletions

View file

@ -211,7 +211,16 @@ export class GBCoreService implements IGBCoreService {
*/ */
public async loadInstances(): Promise<IGBInstance[]> { public async loadInstances(): Promise<IGBInstance[]> {
if (process.env.LOAD_ONLY !== undefined) { 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); return GuaribasInstance.findAll(options);
} }
else { else {
@ -349,24 +358,26 @@ STORAGE_SYNC=true
let instances: IGBInstance[]; let instances: IGBInstance[];
try { try {
instances = await core.loadInstances(); instances = await core.loadInstances();
await CollectionUtil.asyncForEach(instances, async instance => { if (process.env.ENDPOINT_UPDATE === "true") {
GBLog.info(`Updating bot endpoint for ${instance.botId}...`); await CollectionUtil.asyncForEach(instances, async instance => {
try { GBLog.info(`Updating bot endpoint for ${instance.botId}...`);
await installationDeployer.updateBotProxy( try {
instance.botId, await installationDeployer.updateBotProxy(
GBConfigService.get("CLOUD_GROUP"), instance.botId,
`${proxyAddress}/api/messages/${instance.botId}` GBConfigService.get("CLOUD_GROUP"),
); `${proxyAddress}/api/messages/${instance.botId}`
} catch (error) { );
if (error.code === "ResourceNotFound") { } catch (error) {
GBLog.warn(`Bot ${instance.botId} not found on resource group ${GBConfigService.get("CLOUD_GROUP")}.`); if (error.code === "ResourceNotFound") {
} GBLog.warn(`Bot ${instance.botId} not found on resource group ${GBConfigService.get("CLOUD_GROUP")}.`);
else { }
else {
throw new Error(`Error updating bot proxy, details: ${error}.`); throw new Error(`Error updating bot proxy, details: ${error}.`);
}
} }
} });
}); }
} catch (error) { } catch (error) {
if (error.parent === undefined) { if (error.parent === undefined) {
throw new Error(`Cannot connect to operating storage: ${error.message}.`); throw new Error(`Cannot connect to operating storage: ${error.message}.`);

View file

@ -74,19 +74,26 @@ export class GBVMService extends GBService {
let filename: string = file.name; let filename: string = file.name;
if ( if (filename.endsWith('.docx')) {
filename.endsWith('.vbs') || const wordFile = filename;
filename.endsWith('.vb') || const vbsFile = filename.substr(0, filename.indexOf('docx')) + 'vbs';
filename.endsWith('.basic') || const fullVbsFile = urlJoin(folder, vbsFile);
filename.endsWith('.bas') || const docxStat = fs.statSync(urlJoin(folder, wordFile));
filename.endsWith('.docx') const interval = 30000; // If compiled is older 30 seconds, then recompile.
) { let writeVBS = true;
if (fs.existsSync(fullVbsFile)) {
if (filename.endsWith('.docx')) { const vbsStat = fs.statSync(fullVbsFile);
let text = await this.getTextFromWord(folder, filename); if (docxStat.mtimeMs < (vbsStat.mtimeMs + interval)) {
filename = filename.substr(0, filename.indexOf('docx')) + 'vbs'; writeVBS = false;
fs.writeFileSync(urlJoin(folder, filename), text); }
} }
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]; let mainName = filename.replace(/\s|\-/gi, '').split('.')[0];
mainName = mainName.toLowerCase(); mainName = mainName.toLowerCase();
@ -98,7 +105,24 @@ export class GBVMService extends GBService {
// await this.run(fullFilename, min, deployer, mainName); // 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);
}
} }
}) })
); );
@ -180,7 +204,7 @@ export class GBVMService extends GBService {
return code; 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 // Converts General Bots BASIC into regular VBS
const basicCode: string = fs.readFileSync(filename, 'utf8'); const basicCode: string = fs.readFileSync(filename, 'utf8');
@ -272,15 +296,20 @@ export class GBVMService extends GBService {
parsedCode = beautify(parsedCode, { indent_size: 2, space_in_empty_paren: true }) parsedCode = beautify(parsedCode, { indent_size: 2, space_in_empty_paren: true })
fs.writeFileSync(jsfile, parsedCode); fs.writeFileSync(jsfile, parsedCode);
try { this.executeJS(min, deployer, parsedCode, mainName);
const sandbox: DialogClass = new DialogClass(min, deployer); GBLog.info(`[GBVMService] Finished loading of ${filename}`);
const context = vm.createContext(sandbox); }
vm.runInContext(parsedCode, context); }
min.sandBoxMap[mainName.toLowerCase()] = sandbox;
GBLog.info(`[GBVMService] Finished loading of ${filename}`); private executeJS(min: GBMinInstance, deployer: GBDeployer, parsedCode: string, mainName: string) {
} catch (error) { try {
GBLog.error(`[GBVMService] ERROR loading ${error}`); 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}`);
} }
} }
@ -320,7 +349,7 @@ export class GBVMService extends GBService {
step.activeDialog.state.options = {}; step.activeDialog.state.options = {};
step.activeDialog.state.options.cbId = (step.options as any).id; step.activeDialog.state.options.cbId = (step.options as any).id;
step.activeDialog.state.options.previousResolve = (step.options as any).previousResolve; 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 => { async step => {
const cbId = step.activeDialog.state.options.cbId; const cbId = step.activeDialog.state.options.cbId;

View file

@ -110,11 +110,13 @@ export class WhatsappDirectLine extends GBService {
const express = require('express'); const express = require('express');
GBServer.globals.server.use(`/audios`, express.static('work')); GBServer.globals.server.use(`/audios`, express.static('work'));
try { if (process.env.ENDPOINT_UPDATE === "true") {
let res = await request.post(options); try {
GBLog.info(res); let res = await request.post(options);
} catch (error) { GBLog.info(res);
GBLog.error(`Error initializing 3rd party Whatsapp provider(1) ${error.message}`); } catch (error) {
GBLog.error(`Error initializing 3rd party Whatsapp provider(1) ${error.message}`);
}
} }
} }