From 8a1eb6ed6f69aa399355f92c655dcdc56f569840 Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Sun, 6 Jun 2021 18:47:13 -0300 Subject: [PATCH] [FIX] Support DISABLE_PRESENCE_MONITOR env var in new DB watchers (#22257) --- server/modules/watchers/watchers.module.ts | 35 ++++++++++++---------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/server/modules/watchers/watchers.module.ts b/server/modules/watchers/watchers.module.ts index a6f856a4413..61a21e1f7d4 100644 --- a/server/modules/watchers/watchers.module.ts +++ b/server/modules/watchers/watchers.module.ts @@ -66,6 +66,9 @@ type Watcher = (model: IBaseRaw, fn: (event: IChange) type BroadcastCallback = (event: T, ...args: Parameters) => Promise; +const startMonitor = typeof process.env.DISABLE_PRESENCE_MONITOR === 'undefined' + || !['true', 'yes'].includes(String(process.env.DISABLE_PRESENCE_MONITOR).toLowerCase()); + export function initWatchers(models: IModelsParam, broadcast: BroadcastCallback, watch: Watcher): void { const { Messages, @@ -164,22 +167,24 @@ export function initWatchers(models: IModelsParam, broadcast: BroadcastCallback, }); }); - watch(UsersSessions, async ({ clientAction, id, data }) => { - switch (clientAction) { - case 'inserted': - case 'updated': - data = data ?? await UsersSessions.findOneById(id); - if (!data) { - return; - } + if (startMonitor) { + watch(UsersSessions, async ({ clientAction, id, data }) => { + switch (clientAction) { + case 'inserted': + case 'updated': + data = data ?? await UsersSessions.findOneById(id); + if (!data) { + return; + } - broadcast('watch.userSessions', { clientAction, userSession: data }); - break; - case 'removed': - broadcast('watch.userSessions', { clientAction, userSession: { _id: id } }); - break; - } - }); + broadcast('watch.userSessions', { clientAction, userSession: data }); + break; + case 'removed': + broadcast('watch.userSessions', { clientAction, userSession: { _id: id } }); + break; + } + }); + } watch(LivechatInquiry, async ({ clientAction, id, data, diff }) => { switch (clientAction) {