Skip to content

Commit

Permalink
dedupe events received from license
Browse files Browse the repository at this point in the history
  • Loading branch information
KevLehman committed Oct 10, 2023
1 parent dd0ee24 commit b78d5ae
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
11 changes: 10 additions & 1 deletion apps/meteor/server/services/omnichannel/queue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { InquiryWithAgentInfo, IOmnichannelQueue } from '@rocket.chat/core-typings';
import { License } from '@rocket.chat/license';
import { LivechatInquiry } from '@rocket.chat/models';

import { dispatchAgentDelegated } from '../../../app/livechat/server/lib/Helper';
Expand All @@ -19,6 +20,10 @@ export class OmnichannelQueue implements IOmnichannelQueue {
return timeout < 1 ? DEFAULT_RACE_TIMEOUT : timeout * 1000;
}

public isRunning() {
return this.running;
}

async start() {
if (this.running) {
return;
Expand Down Expand Up @@ -94,12 +99,16 @@ export class OmnichannelQueue implements IOmnichannelQueue {
}
}

shouldStart() {
async shouldStart() {
if (!settings.get('Livechat_enabled')) {
void this.stop();
return;
}

if (await License.shouldPreventAction('monthlyActiveContacts')) {
return;
}

const routingSupportsAutoAssign = RoutingManager.getConfig()?.autoAssignAgent;
queueLogger.debug({
msg: 'Routing method supports auto assignment',
Expand Down
12 changes: 11 additions & 1 deletion apps/meteor/server/services/omnichannel/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export class OmnichannelService extends ServiceClassInternal implements IOmnicha

private queueWorker: IOmnichannelQueue;

private macLimitReached = false;

constructor() {
super();
this.queueWorker = new OmnichannelQueue();
Expand All @@ -38,11 +40,19 @@ export class OmnichannelService extends ServiceClassInternal implements IOmnicha
});

License.onLimitReached('monthlyActiveContacts', async (): Promise<void> => {
if (this.macLimitReached) {
// Dupe events
return;
}

this.macLimitReached = true;
void this.api?.broadcast('mac.limitReached');
await this.queueWorker.stop();
// @ts-expect-error - isRunning
this.queueWorker.isRunning() && (await this.queueWorker.stop());
});

License.onValidateLicense(async (): Promise<void> => {
this.macLimitReached = false;
void this.api?.broadcast('mac.limitRestored');
RoutingManager.isMethodSet() && (await this.queueWorker.shouldStart());
});
Expand Down
2 changes: 0 additions & 2 deletions apps/meteor/tests/end-to-end/api/livechat/04-dashboards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,11 @@ describe('LIVECHAT - dashboards', function () {
});

const minMessages = TOTAL_MESSAGES.min * TOTAL_ROOMS;
const maxMessages = TOTAL_MESSAGES.max * TOTAL_ROOMS;

const totalMessages = result.body.totalizers.find((item: any) => item.title === 'Total_messages');
expect(totalMessages).to.not.be.undefined;
const totalMessagesValue = parseInt(totalMessages.value);
expect(totalMessagesValue).to.be.greaterThanOrEqual(minMessages);
expect(totalMessagesValue).to.be.lessThanOrEqual(maxMessages);
});
});

Expand Down
1 change: 1 addition & 0 deletions packages/core-typings/src/omnichannel/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export interface IOmnichannelQueue {
start(): Promise<void>;
shouldStart(): void;
stop(): Promise<void>;
isRunning(): Promise<boolean>;
}

0 comments on commit b78d5ae

Please sign in to comment.