import { randomUUID } from 'node:crypto'; import { hostname } from 'node:os'; import type { ConfigService } from '@nestjs/config'; import type { AppConfig } from './app-config.schema.js'; // A replica must be identifiable from the outside. Inside Docker the hostname is // the short container ID, so X-Instance-Id can be mapped back to exactly one // container; INSTANCE_ID exists for platforms where that mapping is not useful. export function resolveInstanceId(config: ConfigService): string { const configured = config.get('INSTANCE_ID'); if (configured) return configured; const host = hostname(); return host.length > 0 ? host : randomUUID(); }