From 2df1935046557c58f5ee72d4415518f9219de5cc Mon Sep 17 00:00:00 2001 From: Leonardo Viva Date: Tue, 17 Dec 2024 16:34:17 -0300 Subject: [PATCH] fix: prevent db watch when mongo replicaset --- src/imports/meta/loadMetaObjects.ts | 6 +++- src/imports/utils/mongo.js | 22 -------------- src/imports/utils/mongo.ts | 46 +++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 23 deletions(-) delete mode 100644 src/imports/utils/mongo.js create mode 100644 src/imports/utils/mongo.ts diff --git a/src/imports/meta/loadMetaObjects.ts b/src/imports/meta/loadMetaObjects.ts index 9f67c3f6..5d15c72a 100644 --- a/src/imports/meta/loadMetaObjects.ts +++ b/src/imports/meta/loadMetaObjects.ts @@ -8,6 +8,7 @@ import unset from 'lodash/unset'; import { Document } from '@imports/model/Document'; import { MetaObject } from '@imports/model/MetaObject'; import { MetaObjectType } from '@imports/types/metadata'; +import { isReplicaSet } from '@imports/utils/mongo'; import { Promise as BluebirdPromise } from 'bluebird'; import path from 'path'; import { checkInitialData } from '../data/initialData'; @@ -299,5 +300,8 @@ export async function loadMetaObjects() { logger.info('Loading MetaObject.Meta from database'); await dbLoad(); - dbWatch(); + if (await isReplicaSet()) { + logger.info('[kondata] Starting watch for changes in the database'); + dbWatch(); + } } diff --git a/src/imports/utils/mongo.js b/src/imports/utils/mongo.js deleted file mode 100644 index 1b566bb5..00000000 --- a/src/imports/utils/mongo.js +++ /dev/null @@ -1,22 +0,0 @@ -import { ObjectId } from 'mongodb'; -import isArray from 'lodash/isArray'; -import isObject from 'lodash/isObject'; - -export function convertObjectIds(records) { - if (isArray(records)) { - return records.map(convertObjectIds); - } - - if (isObject(records)) { - if (records instanceof ObjectId) { - return records.toString(); - } - - return Object.keys(records).reduce((result, key) => { - result[key] = convertObjectIds(records[key]); - return result; - }, {}); - } - - return records; -} diff --git a/src/imports/utils/mongo.ts b/src/imports/utils/mongo.ts new file mode 100644 index 00000000..d0559c06 --- /dev/null +++ b/src/imports/utils/mongo.ts @@ -0,0 +1,46 @@ +import { db } from '@imports/database'; +import isArray from 'lodash/isArray'; +import isObject from 'lodash/isObject'; +import { MongoServerError, ObjectId } from 'mongodb'; +import { logger } from './logger'; + +export function convertObjectIds( + records: T, +): T extends ObjectId + ? string + : T extends (infer U)[] + ? U[] + : T extends object + ? { [K in keyof T]: T[K] extends ObjectId ? string : T[K] extends object | object[] ? ReturnType> : T[K] } + : T { + if (isArray(records)) { + return records.map(item => convertObjectIds(item)) as any; + } + + if (isObject(records)) { + if (records instanceof ObjectId) { + return records.toString() as any; + } + + return Object.keys(records).reduce((result, key) => { + const k = key as keyof T; + result[k] = convertObjectIds(records[k]); + return result; + }, {} as any); + } + + return records as any; +} + +export async function isReplicaSet() { + try { + const status = await db.admin().replSetGetStatus(); + return status != null; + } catch (error) { + if ((error as MongoServerError).codeName === 'NoReplicationEnabled') { + return false; + } + logger.error('Erro ao verificar status do replica set:', error); + return false; + } +}