Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
106 lines
3.0 KiB
TypeScript
106 lines
3.0 KiB
TypeScript
import { Agenda } from '@rocket.chat/agenda';
|
|
import type { IUser } from '@rocket.chat/core-typings';
|
|
import type { MainLogger } from '@rocket.chat/logger';
|
|
import { LivechatRooms, Users } from '@rocket.chat/models';
|
|
import { Meteor } from 'meteor/meteor';
|
|
import { MongoInternals } from 'meteor/mongo';
|
|
import moment from 'moment';
|
|
|
|
import { schedulerLogger } from './logger';
|
|
import { closeRoom } from '../../../../server/lib/omnichannel/closeRoom';
|
|
|
|
const SCHEDULER_NAME = 'omnichannel_auto_close_on_hold_scheduler';
|
|
|
|
export class AutoCloseOnHoldSchedulerClass {
|
|
scheduler: Agenda;
|
|
|
|
schedulerUser: IUser;
|
|
|
|
running: boolean;
|
|
|
|
logger: MainLogger;
|
|
|
|
constructor() {
|
|
this.logger = schedulerLogger.section('AutoCloseOnHoldScheduler');
|
|
}
|
|
|
|
public async init(): Promise<void> {
|
|
if (this.running) {
|
|
return;
|
|
}
|
|
|
|
this.scheduler = new Agenda({
|
|
mongo: (MongoInternals.defaultRemoteCollectionDriver().mongo as any).client.db(),
|
|
db: { collection: SCHEDULER_NAME },
|
|
defaultConcurrency: 1,
|
|
processEvery: process.env.TEST_MODE === 'true' || process.env.TEST_MODE === 'api' ? '3 seconds' : '1 minute',
|
|
});
|
|
|
|
await this.scheduler.start();
|
|
this.running = true;
|
|
this.logger.info('Service started');
|
|
}
|
|
|
|
public async scheduleRoom(roomId: string, timeout: number, comment: string): Promise<void> {
|
|
if (!this.running) {
|
|
throw new Error('AutoCloseOnHoldScheduler is not running');
|
|
}
|
|
|
|
this.logger.debug({ msg: 'Scheduling room to be closed', roomId, timeoutSeconds: timeout });
|
|
await this.unscheduleRoom(roomId);
|
|
|
|
const jobName = `${SCHEDULER_NAME}-${roomId}`;
|
|
const when = moment(new Date()).add(timeout, 's').toDate();
|
|
|
|
this.scheduler.define(jobName, this.executeJob.bind(this));
|
|
await this.scheduler.schedule(when, jobName, { roomId, comment });
|
|
}
|
|
|
|
public async unscheduleRoom(roomId: string): Promise<void> {
|
|
if (!this.running) {
|
|
throw new Error('AutoCloseOnHoldScheduler is not running');
|
|
}
|
|
this.logger.debug({ msg: 'Unscheduling room', roomId });
|
|
const jobName = `${SCHEDULER_NAME}-${roomId}`;
|
|
await this.scheduler.cancel({ name: jobName });
|
|
}
|
|
|
|
private async executeJob({ attrs: { data } }: any = {}): Promise<void> {
|
|
this.logger.debug({ msg: 'Executing job for room', roomId: data.roomId });
|
|
const { roomId, comment } = data;
|
|
|
|
const [room, user] = await Promise.all([LivechatRooms.findOneById(roomId), this.getSchedulerUser()]);
|
|
if (!room || !user) {
|
|
throw new Error(
|
|
`Unable to process AutoCloseOnHoldScheduler job because room or user not found for roomId: ${roomId} and userId: rocket.cat`,
|
|
);
|
|
}
|
|
|
|
const payload = {
|
|
room,
|
|
user,
|
|
comment,
|
|
};
|
|
|
|
await closeRoom(payload);
|
|
}
|
|
|
|
private async getSchedulerUser(): Promise<IUser> {
|
|
if (!this.schedulerUser) {
|
|
const schedulerUser = await Users.findOneById('rocket.cat');
|
|
if (!schedulerUser) {
|
|
throw new Error('Scheduler user not found');
|
|
}
|
|
this.schedulerUser = schedulerUser;
|
|
}
|
|
|
|
return this.schedulerUser;
|
|
}
|
|
}
|
|
|
|
export const AutoCloseOnHoldScheduler = new AutoCloseOnHoldSchedulerClass();
|
|
|
|
Meteor.startup(() => {
|
|
void AutoCloseOnHoldScheduler.init();
|
|
});
|