fix(engine-room): usa http.get em vez de fetch
O fetch do Node.js estava falhando com loopback. Trocado para http.get nativo que é mais confiável. Refs: engine-room, registry, fix
This commit is contained in:
parent
b0a8e73f67
commit
ea58a45ea1
|
|
@ -6,33 +6,53 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import type { Express, Request, Response } from "express";
|
import type { Express, Request, Response } from "express";
|
||||||
|
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
|
||||||
async function fetchRegistryServices(): Promise<any[] | null> {
|
async function fetchRegistryServices(): Promise<any[] | null> {
|
||||||
try {
|
return new Promise((resolve) => {
|
||||||
const controller = new AbortController();
|
const options = {
|
||||||
const timeout = setTimeout(() => controller.abort(), 3000);
|
hostname: '127.0.0.1',
|
||||||
|
port: 5001,
|
||||||
|
path: '/api/registry/services',
|
||||||
|
method: 'GET',
|
||||||
|
timeout: 3000,
|
||||||
|
};
|
||||||
|
|
||||||
// Tenta 127.0.0.1 primeiro (mais confiável que localhost)
|
const req = http.request(options, (res) => {
|
||||||
const response = await fetch('http://127.0.0.1:5001/api/registry/services', {
|
let data = '';
|
||||||
signal: controller.signal,
|
|
||||||
|
res.on('data', (chunk) => {
|
||||||
|
data += chunk;
|
||||||
|
});
|
||||||
|
|
||||||
|
res.on('end', () => {
|
||||||
|
try {
|
||||||
|
const json = JSON.parse(data);
|
||||||
|
const services = json.success && Array.isArray(json.data?.services) ? json.data.services : null;
|
||||||
|
console.log(`[Engine Room] Registry retornou ${services?.length || 0} serviços`);
|
||||||
|
resolve(services);
|
||||||
|
} catch (e) {
|
||||||
|
console.error('[Engine Room] Erro ao parsear resposta:', e);
|
||||||
|
resolve(null);
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
clearTimeout(timeout);
|
req.on('error', (error) => {
|
||||||
|
console.error('[Engine Room] Erro ao consultar Registry:', error.message);
|
||||||
|
resolve(null);
|
||||||
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
req.on('timeout', () => {
|
||||||
console.log('[Engine Room] Registry retornou:', response.status);
|
console.error('[Engine Room] Timeout ao consultar Registry');
|
||||||
return null;
|
req.destroy();
|
||||||
}
|
resolve(null);
|
||||||
|
});
|
||||||
|
|
||||||
const data = await response.json();
|
req.end();
|
||||||
const services = data.success && Array.isArray(data.data?.services) ? data.data.services : null;
|
});
|
||||||
console.log(`[Engine Room] Registry retornou ${services?.length || 0} serviços`);
|
|
||||||
return services;
|
|
||||||
} catch (error) {
|
|
||||||
console.error('[Engine Room] Erro ao consultar Registry:', error);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Converte serviço do Registry para formato EngineStatus
|
// Converte serviço do Registry para formato EngineStatus
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue