2021-08-09 17:55:55 -03:00
|
|
|
/*****************************************************************************\
|
2024-01-09 17:40:48 -03:00
|
|
|
| █████ █████ ██ █ █████ █████ ████ ██ ████ █████ █████ ███ ® |
|
|
|
|
| ██ █ ███ █ █ ██ ██ ██ ██ ██ ██ █ ██ ██ █ █ |
|
|
|
|
| ██ ███ ████ █ ██ █ ████ █████ ██████ ██ ████ █ █ █ ██ |
|
|
|
|
| ██ ██ █ █ ██ █ █ ██ ██ ██ ██ ██ ██ █ ██ ██ █ █ |
|
|
|
|
| █████ █████ █ ███ █████ ██ ██ ██ ██ █████ ████ █████ █ ███ |
|
2021-08-09 17:55:55 -03:00
|
|
|
| |
|
2024-08-17 20:30:00 -03:00
|
|
|
| General Bots Copyright (c) pragmatismo.cloud. All rights reserved. |
|
2021-08-09 17:55:55 -03:00
|
|
|
| Licensed under the AGPL-3.0. |
|
|
|
|
| |
|
|
|
|
| According to our dual licensing model, this program can be used either |
|
|
|
|
| under the terms of the GNU Affero General Public License, version 3, |
|
|
|
|
| or under a proprietary license. |
|
|
|
|
| |
|
|
|
|
| The texts of the GNU Affero General Public License with an additional |
|
|
|
|
| permission and of our proprietary license can be found at and |
|
|
|
|
| in the LICENSE file you have received along with this program. |
|
|
|
|
| |
|
|
|
|
| This program is distributed in the hope that it will be useful, |
|
|
|
|
| but WITHOUT ANY WARRANTY, without even the implied warranty of |
|
|
|
|
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
|
|
| GNU Affero General Public License for more details. |
|
|
|
|
| |
|
2024-08-17 20:30:00 -03:00
|
|
|
| "General Bots" is a registered trademark of pragmatismo.cloud. |
|
2021-08-09 17:55:55 -03:00
|
|
|
| The licensing of the program under the AGPLv3 does not imply a |
|
|
|
|
| trademark license. Therefore any rights, title and interest in |
|
|
|
|
| our trademarks remain entirely with us. |
|
|
|
|
| |
|
|
|
|
\*****************************************************************************/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import { GBLog, GBMinInstance, GBService } from 'botlib';
|
2022-11-18 22:39:14 -03:00
|
|
|
import { GBServer } from '../../../src/app.js';
|
2021-08-09 17:55:55 -03:00
|
|
|
import { CollectionUtil } from 'pragmatismo-io-framework';
|
2022-11-18 22:39:14 -03:00
|
|
|
import { GBVMService } from '../../basic.gblib/services/GBVMService.js';
|
|
|
|
import { GuaribasSchedule } from '../../core.gbapp/models/GBModel.js';
|
2021-08-09 17:55:55 -03:00
|
|
|
|
2022-11-18 22:39:14 -03:00
|
|
|
import cron from 'node-cron';
|
2023-04-20 22:41:58 -03:00
|
|
|
import { GBLogEx } from '../../core.gbapp/services/GBLogEx.js';
|
2021-08-09 17:55:55 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @fileoverview Schedule Services.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Basic services for BASIC manipulation.
|
|
|
|
*/
|
|
|
|
export class ScheduleServices extends GBService {
|
2024-03-16 21:36:03 -03:00
|
|
|
public async deleteScheduleIfAny(min: GBMinInstance, name: string) {
|
|
|
|
let i = 1;
|
|
|
|
while (i <= 10) {
|
|
|
|
const task = min['scheduleMap'] ? min['scheduleMap'][name + i] : null;
|
2021-08-09 17:55:55 -03:00
|
|
|
|
2024-03-16 21:36:03 -03:00
|
|
|
if (task) {
|
|
|
|
task.destroy();
|
2024-08-21 13:09:50 -03:00
|
|
|
}
|
|
|
|
const id = `${name};${i}`;
|
|
|
|
|
|
|
|
delete min['scheduleMap'][id];
|
|
|
|
const count = await GuaribasSchedule.destroy({
|
|
|
|
where: {
|
|
|
|
instanceId: min.instance.instanceId,
|
|
|
|
name: id
|
2024-03-16 21:36:03 -03:00
|
|
|
}
|
2024-08-21 13:09:50 -03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (count > 0) {
|
2024-08-23 23:36:20 -03:00
|
|
|
GBLogEx.info(min, `Removed ${name} SET SCHEDULE and ${count} rows from storage on: ${min.botId}...`);
|
2021-08-09 17:55:55 -03:00
|
|
|
}
|
|
|
|
|
2024-08-21 13:09:50 -03:00
|
|
|
i++;
|
2021-08-09 17:55:55 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds and update user agent information to a next available person.
|
|
|
|
*/
|
2024-03-16 21:36:03 -03:00
|
|
|
public async createOrUpdateSchedule(min: GBMinInstance, schedule: string, name: string): Promise<GuaribasSchedule> {
|
2021-08-09 17:55:55 -03:00
|
|
|
let record = await GuaribasSchedule.findOne({
|
|
|
|
where: {
|
|
|
|
instanceId: min.instance.instanceId,
|
|
|
|
name: name
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (record === null) {
|
2022-01-03 13:11:21 -03:00
|
|
|
record = await GuaribasSchedule.create(<GuaribasSchedule>{
|
2021-08-09 17:55:55 -03:00
|
|
|
instanceId: min.instance.instanceId,
|
|
|
|
name: name,
|
|
|
|
schedule: schedule
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
record.schedule = schedule;
|
|
|
|
await record.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.ScheduleItem(record, min);
|
|
|
|
|
|
|
|
return record;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-11-19 23:34:58 -03:00
|
|
|
* Load all cached schedule from BASIC SET SCHEDULE keyword.
|
|
|
|
*/
|
2024-03-16 21:36:03 -03:00
|
|
|
public async scheduleAll() {
|
2021-08-09 17:55:55 -03:00
|
|
|
let schedules;
|
|
|
|
try {
|
2022-10-09 23:41:37 -03:00
|
|
|
schedules = await GuaribasSchedule.findAll();
|
2024-03-16 21:36:03 -03:00
|
|
|
let i = 0;
|
|
|
|
let lastName = '';
|
|
|
|
|
2024-08-21 13:09:50 -03:00
|
|
|
await CollectionUtil.asyncForEach(schedules, async item => {
|
2024-03-16 21:36:03 -03:00
|
|
|
if (item.name === lastName) {
|
|
|
|
item.name = item.name + ++i;
|
2024-08-21 13:09:50 -03:00
|
|
|
} else {
|
2024-03-16 21:36:03 -03:00
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
|
2022-10-09 23:41:37 -03:00
|
|
|
let min: GBMinInstance = GBServer.globals.minInstances.filter(
|
|
|
|
p => p.instance.instanceId === item.instanceId
|
|
|
|
)[0];
|
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
if (min) {
|
2022-10-11 20:18:12 -03:00
|
|
|
this.ScheduleItem(item, min);
|
|
|
|
}
|
2022-02-04 18:50:19 -03:00
|
|
|
});
|
2021-08-09 17:55:55 -03:00
|
|
|
} catch (error) {
|
|
|
|
throw new Error(`Cannot schedule: ${error.message}.`);
|
|
|
|
}
|
|
|
|
return schedules;
|
|
|
|
}
|
|
|
|
|
2024-03-16 21:36:03 -03:00
|
|
|
private ScheduleItem(item: GuaribasSchedule, min: GBMinInstance) {
|
|
|
|
GBLogEx.info(min, `Scheduling ${item.name} on ${min.botId}...`);
|
2021-08-09 17:55:55 -03:00
|
|
|
try {
|
|
|
|
const options = {
|
|
|
|
scheduled: true,
|
|
|
|
timezone: 'America/Sao_Paulo'
|
|
|
|
};
|
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
const task = min['scheduleMap'][item.name];
|
2021-08-09 17:55:55 -03:00
|
|
|
if (task) {
|
2021-08-09 21:47:36 -03:00
|
|
|
task.stop();
|
2022-11-19 23:34:58 -03:00
|
|
|
min['scheduleMap'][item.name] = null;
|
2021-08-09 17:55:55 -03:00
|
|
|
}
|
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
min['scheduleMap'][item.name] = cron.schedule(
|
2021-08-09 17:55:55 -03:00
|
|
|
item.schedule,
|
2021-08-09 21:47:36 -03:00
|
|
|
function () {
|
|
|
|
const finalData = async () => {
|
2024-03-16 21:36:03 -03:00
|
|
|
let script = item.name.split(';')[0];
|
2021-08-09 21:47:36 -03:00
|
|
|
let min: GBMinInstance = GBServer.globals.minInstances.filter(
|
|
|
|
p => p.instance.instanceId === item.instanceId
|
|
|
|
)[0];
|
2024-03-16 21:36:03 -03:00
|
|
|
GBLogEx.info(min, `Running .gbdialog word ${item.name} on:${item.schedule}...`);
|
|
|
|
|
2024-03-03 16:20:50 -03:00
|
|
|
const pid = GBVMService.createProcessInfo(null, min, 'batch', null);
|
|
|
|
await GBVMService.callVM(script, min, null, pid);
|
2021-08-09 21:47:36 -03:00
|
|
|
};
|
|
|
|
(async () => {
|
|
|
|
await finalData();
|
|
|
|
})();
|
2022-11-19 23:34:58 -03:00
|
|
|
},
|
|
|
|
options
|
2021-08-09 17:55:55 -03:00
|
|
|
);
|
2024-02-22 13:54:54 -03:00
|
|
|
} catch (error) {
|
2024-03-16 21:36:03 -03:00
|
|
|
GBLogEx.error(min, `Running .gbdialog word ${item.name} : ${error}...`);
|
2024-02-22 13:54:54 -03:00
|
|
|
}
|
2021-08-09 17:55:55 -03:00
|
|
|
}
|
|
|
|
}
|