Skip to content

Commit

Permalink
refactor: Omnichannel queue starting multiple times due to race condi…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
aleksandernsilva committed Nov 29, 2024
1 parent e476a01 commit 2e859f4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
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();
queueLogger.info('Service stopped');
}

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

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

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

Expand Down

0 comments on commit 2e859f4

Please sign in to comment.