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

@ -138,14 +138,6 @@ export class GBDeployer implements IGBDeployer {
const appPackagesProcessed = await this.deployAppPackages(gbappPackages, core, appPackages); 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(
@ -159,7 +151,6 @@ export class GBDeployer implements IGBDeployer {
totalPackages, totalPackages,
resolve resolve
)); ));
});
} }
); );
} }
@ -316,7 +307,7 @@ export class GBDeployer implements IGBDeployer {
case '.gbkb': case '.gbkb':
const service = new KBService(this.core.sequelize); const service = new KBService(this.core.sequelize);
await service.deployKb(this.core, this, localPath); await service.deployKb(this.core, this, localPath, min);
break; break;
case '.gbdialog': case '.gbdialog':
@ -503,7 +494,7 @@ export class GBDeployer implements IGBDeployer {
return { generalPackages, totalPackages }; 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}/subjects`, express.static(urlJoin(filename, 'subjects')));
GBServer.globals.server.use(`/kb/${packageName}/assets`, express.static(urlJoin(filename, 'assets'))); GBServer.globals.server.use(`/kb/${packageName}/assets`, express.static(urlJoin(filename, 'assets')));
GBServer.globals.server.use(`/kb/${packageName}/images`, express.static(urlJoin(filename, 'images'))); GBServer.globals.server.use(`/kb/${packageName}/images`, express.static(urlJoin(filename, 'images')));

View file

@ -241,8 +241,25 @@ export class GBMinService {
// is invoked via the AuthenticationContext and retrieves an // is invoked via the AuthenticationContext and retrieves an
// access token that can be used to access the user owned resource. // access token that can be used to access the user owned resource.
this.handleOAuthTokenRequests(GBServer.globals.server, min, instance); 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) { private handleOAuthTokenRequests(server: any, min: GBMinInstance, instance: IGBInstance) {
server.get(`/${min.instance.botId}/token`, async (req, res) => { server.get(`/${min.instance.botId}/token`, async (req, res) => {
const state = await min.adminService.getValue(instance.instanceId, 'AntiCSRFAttackState'); const state = await min.adminService.getValue(instance.instanceId, 'AntiCSRFAttackState');

View file

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

View file

@ -130,6 +130,22 @@ export class WhatsappDirectLine extends GBService {
this.conversationIds[number] = undefined; 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) { public async received(req, res) {
if (req.body.messages === undefined) { if (req.body.messages === undefined) {