new(core.gbapp): Healthly status for bots.

This commit is contained in:
Rodrigo Rodriguez 2020-05-12 19:20:59 -03:00
parent 17122c79a1
commit bb95f3d116
4 changed files with 58 additions and 31 deletions

View file

@ -48,7 +48,7 @@ const rimraf = require('rimraf');
import { GBError, GBLog, GBMinInstance, IGBCoreService, IGBInstance, IGBPackage, IGBDeployer } from 'botlib';
import { AzureSearch } from 'pragmatismo-io-framework';
import { GBServer } from '../../../src/app';
import { GuaribasPackage} from '../models/GBModel';
import { GuaribasPackage } from '../models/GBModel';
import { GBAdminService } from './../../admin.gbapp/services/GBAdminService';
import { AzureDeployerService } from './../../azuredeployer.gbapp/services/AzureDeployerService';
import { KBService } from './../../kb.gbapp/services/KBService';
@ -138,28 +138,19 @@ export class GBDeployer implements IGBDeployer {
const appPackagesProcessed = await this.deployAppPackages(gbappPackages, core, appPackages);
WaitUntil()
.interval(1000)
.times(5)
.condition(cb => {
GBLog.info(`Waiting for app package deployment...`);
cb(appPackagesProcessed === gbappPackages.length);
})
.done(async () => {
GBLog.info(`App Package deployment done.`);
GBLog.info(`App Package deployment done.`);
({ generalPackages, totalPackages } = await this.deployDataPackages(
({ generalPackages, totalPackages } = await this.deployDataPackages(
core,
botPackages,
_this,
generalPackages,
server,
reject,
totalPackages,
resolve
));
});
core,
botPackages,
_this,
generalPackages,
server,
reject,
totalPackages,
resolve
));
}
);
}
@ -316,7 +307,7 @@ export class GBDeployer implements IGBDeployer {
case '.gbkb':
const service = new KBService(this.core.sequelize);
await service.deployKb(this.core, this, localPath);
await service.deployKb(this.core, this, localPath, min);
break;
case '.gbdialog':
@ -336,7 +327,7 @@ export class GBDeployer implements IGBDeployer {
const packageName = Path.basename(localPath);
const p = await this.getStoragePackageByName(instance.instanceId, packageName);
switch (packageType) {
case '.gbot':
const packageObject = JSON.parse(Fs.readFileSync(urlJoin(localPath, 'package.json'), 'utf8'));
@ -503,7 +494,7 @@ export class GBDeployer implements IGBDeployer {
return { generalPackages, totalPackages };
}
private mountGBKBAssets(packageName: any, filename: string) {
public mountGBKBAssets(packageName: any, filename: string) {
GBServer.globals.server.use(`/kb/${packageName}/subjects`, express.static(urlJoin(filename, 'subjects')));
GBServer.globals.server.use(`/kb/${packageName}/assets`, express.static(urlJoin(filename, 'assets')));
GBServer.globals.server.use(`/kb/${packageName}/images`, express.static(urlJoin(filename, 'images')));

View file

@ -185,7 +185,7 @@ export class GBMinService {
});
await CollectionUtil.asyncForEach(instances, async instance => {
try{
try {
await this.mountBot(instance);
} catch (error) {
GBLog.error(`Error mounting bot ${instance.botId}: ${error.message}`);
@ -211,9 +211,9 @@ export class GBMinService {
// Install default BASIC module.
// this.deployer.deployPackage(min, 'packages/default.gbdialog');
// Call the loadBot context.activity for all packages.
await this.invokeLoadBot(GBServer.globals.appPackages, GBServer.globals.sysPackages, min);
@ -241,8 +241,25 @@ export class GBMinService {
// is invoked via the AuthenticationContext and retrieves an
// access token that can be used to access the user owned resource.
this.handleOAuthTokenRequests(GBServer.globals.server, min, instance);
this.createCheckHealthAddress(GBServer.globals.server, min, min.instance);
}
private createCheckHealthAddress(server: any, min: GBMinInstance, instance: IGBInstance) {
server.get(`/${min.instance.botId}/check`, async (req, res) => {
try {
if (min.whatsAppDirectLine != undefined && instance.whatsappServiceKey !== null) {
min.whatsAppDirectLine.check(min);
}
res.status(200).send(`General Bot ${min.botId} is healthly.`);
} catch (error) {
GBLog.error(error);
res.status(500).send(error.toString());
}
});
}
private handleOAuthTokenRequests(server: any, min: GBMinInstance, instance: IGBInstance) {
server.get(`/${min.instance.botId}/token`, async (req, res) => {
const state = await min.adminService.getValue(instance.instanceId, 'AntiCSRFAttackState');

View file

@ -273,6 +273,8 @@ export class KBService implements IGBKBService {
let lastAnswer: GuaribasAnswer;
let rows = data._worksheets[1]._rows;
GBLog.info(`Now importing ${rows.length} rows from tabular file ${filePath}...`);
return asyncPromise.eachSeries(rows, async line => {
// Skips the first line.
@ -554,16 +556,17 @@ export class KBService implements IGBKBService {
*
* @param localPath Path to the .gbkb folder.
*/
public async deployKb(core: IGBCoreService, deployer: GBDeployer, localPath: string) {
public async deployKb(core: IGBCoreService, deployer: GBDeployer, localPath: string, min: GBMinInstance) {
const packageType = Path.extname(localPath);
const packageName = Path.basename(localPath);
GBLog.info(`[GBDeployer] Opening package: ${localPath}`);
const packageObject = JSON.parse(Fs.readFileSync(urlJoin(localPath, 'package.json'), 'utf8'));
const instance = await core.loadInstanceByBotId(packageObject.botId);
const instance = await core.loadInstanceByBotId(min.botId);
GBLog.info(`[GBDeployer] Importing: ${localPath}`);
const p = await deployer.deployPackageToStorage(instance.instanceId, packageName);
await this.importKbPackage(localPath, p, instance);
deployer.mountGBKBAssets(packageName, localPath);
deployer.rebuildIndex(instance, new AzureDeployerService(deployer).getKBSearchSchema(instance.searchIndex));
GBLog.info(`[GBDeployer] Finished import of ${localPath}`);

View file

@ -130,6 +130,22 @@ export class WhatsappDirectLine extends GBService {
this.conversationIds[number] = undefined;
}
public async check() {
GBLog.info(`GBWhatsapp: Cheking server...`);
const options = {
url: urlJoin(this.whatsappServiceUrl, 'status') + `?token=${this.min.instance.whatsappServiceKey}` ,
method: 'GET',
};
const res = await request(options);
return res.body.accountStatus === "authenticated";
}
public async received(req, res) {
if (req.body.messages === undefined) {
@ -183,11 +199,11 @@ export class WhatsappDirectLine extends GBService {
if (text.startsWith(cmd)) {
let filename = text.substr(cmd.length);
let message = await this.min.kbService.getAnswerTextByMediaName(this.min.instance.instanceId, filename);
if (message === null) {
await this.sendToDevice(user.userSystemId, `File ${filename} not found in any .gbkb published. Check the name or publish again the associated .gbkb.`);
} else {
await this.min.conversationalService.sendMarkdownToMobile(this.min,null, user.userSystemId, message);
await this.min.conversationalService.sendMarkdownToMobile(this.min, null, user.userSystemId, message);
}
} else if (text === '/qt') {
// TODO: Transfers only in pt-br for now.