diff --git a/.changeset/five-clouds-obey.md b/.changeset/five-clouds-obey.md new file mode 100644 index 000000000000..2a00dc5ce031 --- /dev/null +++ b/.changeset/five-clouds-obey.md @@ -0,0 +1,5 @@ +--- +"@rocket.chat/meteor": patch +--- + +fix: Wrong IP usage on monolith TCP transporter configuration diff --git a/apps/meteor/ee/server/local-services/instance/getLogger.ts b/apps/meteor/ee/server/local-services/instance/getLogger.ts new file mode 100644 index 000000000000..bbab1ad28a6f --- /dev/null +++ b/apps/meteor/ee/server/local-services/instance/getLogger.ts @@ -0,0 +1,35 @@ +import { pino } from 'pino'; + +export function getLogger({ MOLECULER_LOG_LEVEL: level, NODE_ENV: mode }: Record = {}) { + if (!level || typeof level !== 'string') { + return {}; + } + + if (!['fatal', 'error', 'warn', 'info', 'debug', 'trace'].includes(level)) { + return {}; + } + + return { + logger: { + type: 'Pino', + options: { + level, + pino: { + options: { + timestamp: pino.stdTimeFunctions.isoTime, + ...(mode !== 'production' + ? { + transport: { + target: 'pino-pretty', + options: { + colorize: true, + }, + }, + } + : {}), + }, + }, + }, + }, + }; +} diff --git a/apps/meteor/ee/server/local-services/instance/getTransporter.ts b/apps/meteor/ee/server/local-services/instance/getTransporter.ts index c075dd2ad357..747321ea0edb 100644 --- a/apps/meteor/ee/server/local-services/instance/getTransporter.ts +++ b/apps/meteor/ee/server/local-services/instance/getTransporter.ts @@ -1,4 +1,4 @@ -export function getTransporter({ transporter, port }: { transporter?: string; port?: string } = {}) { +export function getTransporter({ transporter, port, extra }: { transporter?: string; port?: string; extra?: string } = {}) { if (transporter) { if (!transporter.match(/^(?:monolith\+)/)) { throw new Error('invalid transporter'); @@ -11,5 +11,6 @@ export function getTransporter({ transporter, port }: { transporter?: string; po return { port: port ? port.trim() : 0, udpDiscovery: false, + ...(extra ? JSON.parse(extra) : {}), }; } diff --git a/apps/meteor/ee/server/local-services/instance/service.ts b/apps/meteor/ee/server/local-services/instance/service.ts index 9d85058008ca..b8074d414fd2 100644 --- a/apps/meteor/ee/server/local-services/instance/service.ts +++ b/apps/meteor/ee/server/local-services/instance/service.ts @@ -9,6 +9,9 @@ import { InstanceStatus } from '@rocket.chat/instance-status'; import { StreamerCentral } from '../../../../server/modules/streamer/streamer.module'; import type { IInstanceService } from '../../sdk/types/IInstanceService'; import { getTransporter } from './getTransporter'; +import { getLogger } from './getLogger'; + +const hostIP = process.env.INSTANCE_IP ? String(process.env.INSTANCE_IP).trim() : 'localhost'; export class InstanceService extends ServiceClassInternal implements IInstanceService { protected name = 'instance'; @@ -26,7 +29,7 @@ export class InstanceService extends ServiceClassInternal implements IInstanceSe constructor() { super(); - const tx = getTransporter({ transporter: process.env.TRANSPORTER, port: process.env.TCP_PORT }); + const tx = getTransporter({ transporter: process.env.TRANSPORTER, port: process.env.TCP_PORT, extra: process.env.TRANSPORTER_EXTRA }); if (typeof tx === 'string') { this.transporter = new Transporters.NATS({ url: tx }); this.isTransporterTCP = false; @@ -37,6 +40,8 @@ export class InstanceService extends ServiceClassInternal implements IInstanceSe if (this.isTransporterTCP) { this.onEvent('watch.instanceStatus', async ({ clientAction, data }): Promise => { if (clientAction === 'removed') { + (this.broker.transit?.tx as any).nodes.disconnected(data?._id, false); + (this.broker.transit?.tx as any).nodes.nodes.delete(data?._id); return; } @@ -78,8 +83,14 @@ export class InstanceService extends ServiceClassInternal implements IInstanceSe this.broker = new ServiceBroker({ nodeID: InstanceStatus.id(), transporter: this.transporter, + + ...getLogger(process.env), }); + if ((this.broker.transit?.tx as any)?.nodes?.localNode) { + (this.broker.transit?.tx as any).nodes.localNode.ipList = [hostIP]; + } + this.broker.createService({ name: 'matrix', events: { @@ -107,7 +118,7 @@ export class InstanceService extends ServiceClassInternal implements IInstanceSe await this.broker.start(); const instance = { - host: process.env.INSTANCE_IP ? String(process.env.INSTANCE_IP).trim() : 'localhost', + host: hostIP, port: String(process.env.PORT).trim(), tcpPort: (this.broker.transit?.tx as any)?.nodes?.localNode?.port, os: { diff --git a/apps/meteor/server/modules/watchers/watchers.module.ts b/apps/meteor/server/modules/watchers/watchers.module.ts index 6eb2556e7a67..4ce7432bf124 100644 --- a/apps/meteor/server/modules/watchers/watchers.module.ts +++ b/apps/meteor/server/modules/watchers/watchers.module.ts @@ -372,6 +372,11 @@ export function initWatchers(watcher: DatabaseWatcher, broadcast: BroadcastCallb }); watcher.on(InstanceStatus.getCollectionName(), ({ clientAction, id, data, diff }) => { + if (clientAction === 'removed') { + void broadcast('watch.instanceStatus', { clientAction, id, data: { _id: id } }); + return; + } + void broadcast('watch.instanceStatus', { clientAction, data, diff, id }); });