fix: Casa de Máquinas usa Registry global em vez de HTTP

O problema era que a Casa de Máquinas (porta 5000) não conseguia
conectar ao Kernel (porta 5001) via HTTP dentro do mesmo container.

Solução:
- Exporta o ServiceRegistry globalmente no server/index.ts
- Casa de Máquinas prioriza o Registry global (mesmo processo)
- Fallback para HTTP apenas se Registry global não disponível

Isso elimina a necessidade de loopback HTTP interno.
This commit is contained in:
Jonas Pacheco 2026-04-08 12:27:21 -03:00
parent da371af0e0
commit bb7cff6c80
2 changed files with 24 additions and 3 deletions

View File

@ -9,9 +9,23 @@ import type { Express, Request, Response } from "express";
import http from "http"; import http from "http";
// Consulta Registry do Kernel para serviços descobertos // Consulta Registry do Kernel para serviços descobertos
// Usa http.get em vez de fetch por causa de problemas com loopback no Node.js // PRIORIDADE 1: Usa Registry global se disponível (mesmo processo)
// Implementa retry com backoff para maior resiliência // PRIORIDADE 2: Faz HTTP request para porta 5001 (fallback)
async function fetchRegistryServicesWithRetry(maxRetries = 3, delay = 500): Promise<any[] | null> { async function fetchRegistryServicesWithRetry(maxRetries = 3, delay = 500): Promise<any[] | null> {
// 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++) { for (let attempt = 1; attempt <= maxRetries; attempt++) {
const result = await fetchRegistryServicesOnce(); const result = await fetchRegistryServicesOnce();
if (result !== null) { if (result !== null) {

View File

@ -397,14 +397,21 @@ export function log(message: string, source = "express") {
// Inicia Arcadia Kernel (Dashboard porta 5001) - Semana 3 // Inicia Arcadia Kernel (Dashboard porta 5001) - Semana 3
// Inicia ANTES do servidor HTTP para garantir que o Registry esteja pronto // Inicia ANTES do servidor HTTP para garantir que o Registry esteja pronto
let kernel: ArcadiaKernel | undefined;
if (process.env.KERNEL_ENABLED === 'true') { if (process.env.KERNEL_ENABLED === 'true') {
try { try {
const kernel = new ArcadiaKernel({ kernel = new ArcadiaKernel({
dashboard: { enabled: true, port: 5001, host: '0.0.0.0' }, dashboard: { enabled: true, port: 5001, host: '0.0.0.0' },
autoStart: process.env.KERNEL_AUTOSTART === 'true', autoStart: process.env.KERNEL_AUTOSTART === 'true',
}); });
await kernel.start(); await kernel.start();
log('Arcadia Kernel iniciado em http://0.0.0.0:5001'); 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) { } catch (error) {
log(`Erro ao iniciar Kernel: ${error instanceof Error ? error.message : String(error)}`); log(`Erro ao iniciar Kernel: ${error instanceof Error ? error.message : String(error)}`);
} }