Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Omnichannel queue starting multiple times due to race condition #34062

Merged
merged 15 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/green-shirts-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---

Fixes condition causing Omnichannel queue to start more than once.
23 changes: 15 additions & 8 deletions apps/meteor/server/services/omnichannel/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export class OmnichannelQueue implements IOmnichannelQueue {

private queues: (string | undefined)[] = [];

private lock = Promise.resolve();

private delay() {
const timeout = settings.get<number>('Omnichannel_queue_delay_timeout') ?? 5;
return timeout < 1 ? DEFAULT_RACE_TIMEOUT : timeout * 1000;
Expand All @@ -25,16 +27,20 @@ export class OmnichannelQueue implements IOmnichannelQueue {
}

async start() {
if (this.running) {
return;
}
this.lock = this.lock.then(async () => {
if (this.running) {
return;
}

const activeQueues = await this.getActiveQueues();
queueLogger.debug(`Active queues: ${activeQueues.length}`);
this.running = true;

const activeQueues = await this.getActiveQueues();
queueLogger.debug(`Active queues: ${activeQueues.length}`);
this.running = true;
queueLogger.info('Service started');
await this.execute();
});

queueLogger.info('Service started');
return this.execute();
return this.lock;
}

async stop() {
Expand All @@ -45,6 +51,7 @@ export class OmnichannelQueue implements IOmnichannelQueue {
await LivechatInquiry.unlockAll();

this.running = false;
this.lock = Promise.resolve();
aleksandernsilva marked this conversation as resolved.
Show resolved Hide resolved
queueLogger.info('Service stopped');
}

Expand Down
6 changes: 1 addition & 5 deletions apps/meteor/server/services/omnichannel/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ export class OmnichannelService extends ServiceClassInternal implements IOmnicha
}

async started() {
settings.watch<boolean>('Livechat_enabled', (enabled) => {
void (enabled && RoutingManager.isMethodSet() ? this.queueWorker.shouldStart() : this.queueWorker.stop());
});

settings.watch<string>('Livechat_Routing_Method', async () => {
settings.watchMultiple(['Livechat_enabled', 'Livechat_Routing_Method'], () => {
this.queueWorker.shouldStart();
});

Expand Down
Loading