diff --git a/server/engine-room/routes.ts b/server/engine-room/routes.ts index 422fb61..42204e6 100644 --- a/server/engine-room/routes.ts +++ b/server/engine-room/routes.ts @@ -9,9 +9,23 @@ import type { Express, Request, Response } from "express"; import http from "http"; // Consulta Registry do Kernel para serviços descobertos -// Usa http.get em vez de fetch por causa de problemas com loopback no Node.js -// Implementa retry com backoff para maior resiliência +// PRIORIDADE 1: Usa Registry global se disponível (mesmo processo) +// PRIORIDADE 2: Faz HTTP request para porta 5001 (fallback) async function fetchRegistryServicesWithRetry(maxRetries = 3, delay = 500): Promise { + // Tenta usar Registry global primeiro (mesmo processo Node.js) + const globalRegistry = (global as any).arcadiaKernelRegistry; + if (globalRegistry) { + try { + const services = globalRegistry.getServices(); + console.log(`[Engine Room] Usando Registry global: ${services.length} serviços`); + return services; + } catch (error) { + console.error('[Engine Room] Erro ao acessar Registry global:', error); + } + } + + // Fallback: HTTP request para porta 5001 + console.log('[Engine Room] Registry global não disponível, tentando HTTP...'); for (let attempt = 1; attempt <= maxRetries; attempt++) { const result = await fetchRegistryServicesOnce(); if (result !== null) { diff --git a/server/index.ts b/server/index.ts index 7476195..5b8c97d 100644 --- a/server/index.ts +++ b/server/index.ts @@ -397,14 +397,21 @@ export function log(message: string, source = "express") { // Inicia Arcadia Kernel (Dashboard porta 5001) - Semana 3 // Inicia ANTES do servidor HTTP para garantir que o Registry esteja pronto + let kernel: ArcadiaKernel | undefined; if (process.env.KERNEL_ENABLED === 'true') { try { - const kernel = new ArcadiaKernel({ + kernel = new ArcadiaKernel({ dashboard: { enabled: true, port: 5001, host: '0.0.0.0' }, autoStart: process.env.KERNEL_AUTOSTART === 'true', }); await kernel.start(); log('Arcadia Kernel iniciado em http://0.0.0.0:5001'); + + // Exporta o Registry globalmente para a Casa de Máquinas usar diretamente + if (kernel.serviceRegistry) { + (global as any).arcadiaKernelRegistry = kernel.serviceRegistry; + log('[Kernel] Registry exportado globalmente'); + } } catch (error) { log(`Erro ao iniciar Kernel: ${error instanceof Error ? error.message : String(error)}`); }