From 370cc6a95d1557c3260f90e1e7c1219813edcab6 Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sat, 14 Nov 2020 10:32:18 -0300 Subject: [PATCH] new(core.gbapp): SET SCHEDULE Keyword started. --- package.json | 1 + packages/core.gbapp/models/GBModel.ts | 27 +++++++++++ packages/core.gbapp/services/GBCoreService.ts | 46 +++++++++++++++++-- 3 files changed, 69 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 3d026df0..ed54839b 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,7 @@ }, "dependencies": { "@azure/ms-rest-js": "2.0.4", + "node-cron": "2.0.3", "@microsoft/microsoft-graph-client": "2.0.0", "@semantic-release/changelog": "^5.0.1", "@semantic-release/exec": "^5.0.0", diff --git a/packages/core.gbapp/models/GBModel.ts b/packages/core.gbapp/models/GBModel.ts index 0a74cba7..56703367 100644 --- a/packages/core.gbapp/models/GBModel.ts +++ b/packages/core.gbapp/models/GBModel.ts @@ -352,3 +352,30 @@ export class GuaribasApplications extends Model { @UpdatedAt public updatedAt: Date; } + + +@Table +//tslint:disable-next-line:max-classes-per-file +export class GuaribasSchedule extends Model { + + @Column + public name: string; + + @Column + public schedule: string; + + @ForeignKey(() => GuaribasInstance) + @Column + public instanceId: number; + + @BelongsTo(() => GuaribasInstance) + public instance: GuaribasInstance; + + @Column + @CreatedAt + public createdAt: Date; + + @Column + @UpdatedAt + public updatedAt: Date; +} diff --git a/packages/core.gbapp/services/GBCoreService.ts b/packages/core.gbapp/services/GBCoreService.ts index 6cc2d927..53fcc22b 100644 --- a/packages/core.gbapp/services/GBCoreService.ts +++ b/packages/core.gbapp/services/GBCoreService.ts @@ -36,7 +36,7 @@ 'use strict'; -import { GBLog, IGBCoreService, IGBInstallationDeployer, IGBInstance, IGBPackage } from 'botlib'; +import { GBLog, GBMinInstance, IGBCoreService, IGBInstallationDeployer, IGBInstance, IGBPackage } from 'botlib'; import * as fs from 'fs'; import { Sequelize, SequelizeOptions } from 'sequelize-typescript'; import { Op, Dialect } from 'sequelize'; @@ -50,13 +50,15 @@ import { GBCustomerSatisfactionPackage } from '../../customer-satisfaction.gbapp import { GBKBPackage } from '../../kb.gbapp'; import { GBSecurityPackage } from '../../security.gbapp'; import { GBWhatsappPackage } from '../../whatsapp.gblib/index'; -import { GuaribasInstance } from '../models/GBModel'; +import { GuaribasInstance, GuaribasSchedule } from '../models/GBModel'; import { GBConfigService } from './GBConfigService'; import { GBAzureDeployerPackage } from '../../azuredeployer.gbapp'; import { GBSharePointPackage } from '../../sharepoint.gblib'; import { CollectionUtil } from 'pragmatismo-io-framework'; +import { GBVMService } from './GBVMService'; const opn = require('opn'); +const cron = require('node-cron'); /** * Core service layer. @@ -98,9 +100,7 @@ export class GBCoreService implements IGBCoreService { constructor() { this.adminService = new GBAdminService(this); } - public async ensureInstances(instances: IGBInstance[], bootInstance: any, core: IGBCoreService) { - - } + public async ensureInstances(instances: IGBInstance[], bootInstance: any, core: IGBCoreService) {} /** * Gets database config and connect to storage. */ @@ -600,4 +600,40 @@ STORAGE_SYNC=true return value; } + + public async loadSchedules() { + GBLog.info(`Loading instances from storage...`); + let schedules; + try { + const options = { where: { state: 'active' } }; + schedules = await GuaribasSchedule.findAll(options); + if (process.env.ENDPOINT_UPDATE === 'true') { + await CollectionUtil.asyncForEach(schedules, async item => { + GBLog.info(`Updating bot endpoint for ${item.botId}...`); + try { + const options = { + scheduled: true, + timezone: 'America/Sao_Paulo' + }; + + cron.schedule( + item.schedule, + async () => { + let script = item.name; + let min: GBMinInstance = GBServer.globals.minInstances.filter( + p => p.instance.instanceId === item.instanceId + )[0]; + GBVMService.callVM(script, min, null, null); + }, + options + ); + GBLog.info(`Running .gbdialog word ${item.name} on:${item.schedule}...`); + } catch (error) {} + }); + } + } catch (error) { + throw new Error(`Cannot schedule: ${error.message}.`); + } + return schedules; + } }