new(core.gbapp): SET SCHEDULE Keyword started.

This commit is contained in:
Rodrigo Rodriguez 2020-11-14 10:32:18 -03:00
parent 4cdd9a9c46
commit 370cc6a95d
3 changed files with 69 additions and 5 deletions

View file

@ -49,6 +49,7 @@
}, },
"dependencies": { "dependencies": {
"@azure/ms-rest-js": "2.0.4", "@azure/ms-rest-js": "2.0.4",
"node-cron": "2.0.3",
"@microsoft/microsoft-graph-client": "2.0.0", "@microsoft/microsoft-graph-client": "2.0.0",
"@semantic-release/changelog": "^5.0.1", "@semantic-release/changelog": "^5.0.1",
"@semantic-release/exec": "^5.0.0", "@semantic-release/exec": "^5.0.0",

View file

@ -352,3 +352,30 @@ export class GuaribasApplications extends Model<GuaribasApplications> {
@UpdatedAt @UpdatedAt
public updatedAt: Date; public updatedAt: Date;
} }
@Table
//tslint:disable-next-line:max-classes-per-file
export class GuaribasSchedule extends Model<GuaribasSchedule> {
@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;
}

View file

@ -36,7 +36,7 @@
'use strict'; '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 * as fs from 'fs';
import { Sequelize, SequelizeOptions } from 'sequelize-typescript'; import { Sequelize, SequelizeOptions } from 'sequelize-typescript';
import { Op, Dialect } from 'sequelize'; import { Op, Dialect } from 'sequelize';
@ -50,13 +50,15 @@ import { GBCustomerSatisfactionPackage } from '../../customer-satisfaction.gbapp
import { GBKBPackage } from '../../kb.gbapp'; import { GBKBPackage } from '../../kb.gbapp';
import { GBSecurityPackage } from '../../security.gbapp'; import { GBSecurityPackage } from '../../security.gbapp';
import { GBWhatsappPackage } from '../../whatsapp.gblib/index'; import { GBWhatsappPackage } from '../../whatsapp.gblib/index';
import { GuaribasInstance } from '../models/GBModel'; import { GuaribasInstance, GuaribasSchedule } from '../models/GBModel';
import { GBConfigService } from './GBConfigService'; import { GBConfigService } from './GBConfigService';
import { GBAzureDeployerPackage } from '../../azuredeployer.gbapp'; import { GBAzureDeployerPackage } from '../../azuredeployer.gbapp';
import { GBSharePointPackage } from '../../sharepoint.gblib'; import { GBSharePointPackage } from '../../sharepoint.gblib';
import { CollectionUtil } from 'pragmatismo-io-framework'; import { CollectionUtil } from 'pragmatismo-io-framework';
import { GBVMService } from './GBVMService';
const opn = require('opn'); const opn = require('opn');
const cron = require('node-cron');
/** /**
* Core service layer. * Core service layer.
@ -98,9 +100,7 @@ export class GBCoreService implements IGBCoreService {
constructor() { constructor() {
this.adminService = new GBAdminService(this); 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. * Gets database config and connect to storage.
*/ */
@ -600,4 +600,40 @@ STORAGE_SYNC=true
return value; 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;
}
} }