This commit is contained in:
2025-09-07 21:28:53 +07:00
parent ac8716a933
commit e455417cfc

View File

@@ -1,5 +1,4 @@
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using Discord.Commands;
using Discord.WebSocket;
@@ -114,26 +113,22 @@ public static class ApiBootstrap
Environment.GetEnvironmentVariable("COMPUTERNAME") ?? // Windows hostname
"localhost";
// Parse the hostname to IP address for Orleans
IPAddress advertisedIP;
if (IPAddress.TryParse(hostname, out advertisedIP))
// For Docker containers on the same server, use localhost binding with unique ports
IPAddress bindIP = IPAddress.Any; // Bind to all interfaces (0.0.0.0)
IPAddress advertisedIP = IPAddress.Loopback; // Advertise as localhost for same-server clustering
// Only use external IP if specifically provided for multi-server scenarios
var externalIP = Environment.GetEnvironmentVariable("CAPROVER_SERVER_IP") ??
Environment.GetEnvironmentVariable("EXTERNAL_IP");
if (!string.IsNullOrEmpty(externalIP) && IPAddress.TryParse(externalIP, out var parsedExternalIP))
{
// hostname is already an IP address
advertisedIP = parsedExternalIP;
Console.WriteLine($"Using external IP for multi-server clustering: {advertisedIP}");
}
else
{
// Try to resolve hostname to IP address
try
{
var hostEntry = Dns.GetHostEntry(hostname);
advertisedIP = hostEntry.AddressList.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork)
?? hostEntry.AddressList.FirstOrDefault();
}
catch
{
// Fallback to localhost if resolution fails
advertisedIP = IPAddress.Loopback;
}
Console.WriteLine($"Using localhost for same-server clustering: {advertisedIP}");
}
var postgreSqlConnectionString = configuration.GetSection("PostgreSql")["Orleans"];
@@ -156,7 +151,12 @@ public static class ApiBootstrap
options.ConnectionString = postgreSqlConnectionString;
options.Invariant = "Npgsql";
})
.ConfigureEndpoints(advertisedIP, siloPort, gatewayPort)
.Configure<EndpointOptions>(options =>
{
options.AdvertisedIPAddress = advertisedIP;
options.SiloPort = siloPort;
options.GatewayPort = gatewayPort;
})
.Configure<ClusterOptions>(options =>
{
options.ServiceId = "ManagingApp";
@@ -193,8 +193,13 @@ public static class ApiBootstrap
}
else if (string.IsNullOrEmpty(postgreSqlConnectionString))
{
// In Docker/containerized environments, use the resolved IP address
siloBuilder.ConfigureEndpoints(advertisedIP, siloPort, gatewayPort);
// In Docker/containerized environments, use endpoint options
siloBuilder.Configure<EndpointOptions>(options =>
{
options.AdvertisedIPAddress = advertisedIP;
options.SiloPort = siloPort;
options.GatewayPort = gatewayPort;
});
}
siloBuilder