mirror of
https://github.com/RedWizardsLab/EchoHub.git
synced 2026-09-04 08:36:11 +02:00
refactor: Remove diagnostic logging from IrcGatewayService, Program, and ServerDirectoryService
This commit is contained in:
@@ -39,9 +39,7 @@ public sealed class IrcGatewayService : BackgroundService
|
|||||||
|
|
||||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
{
|
{
|
||||||
Console.Error.WriteLine("[DIAG] IrcGatewayService.ExecuteAsync entered.");
|
|
||||||
await Task.Yield();
|
await Task.Yield();
|
||||||
Console.Error.WriteLine($"[DIAG] IrcGatewayService.ExecuteAsync resumed after Task.Yield(). Enabled={_options.Enabled}");
|
|
||||||
|
|
||||||
if (!_options.Enabled)
|
if (!_options.Enabled)
|
||||||
{
|
{
|
||||||
@@ -149,10 +147,8 @@ public sealed class IrcGatewayService : BackgroundService
|
|||||||
|
|
||||||
public override async Task StopAsync(CancellationToken cancellationToken)
|
public override async Task StopAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
Console.Error.WriteLine("[DIAG] IrcGatewayService.StopAsync entered.");
|
|
||||||
// Cancel ExecuteAsync first so listeners stop accepting
|
// Cancel ExecuteAsync first so listeners stop accepting
|
||||||
await base.StopAsync(cancellationToken);
|
await base.StopAsync(cancellationToken);
|
||||||
Console.Error.WriteLine("[DIAG] IrcGatewayService.StopAsync: base.StopAsync returned.");
|
|
||||||
|
|
||||||
// Force-close any remaining client connections
|
// Force-close any remaining client connections
|
||||||
foreach (var (_, conn) in _connections)
|
foreach (var (_, conn) in _connections)
|
||||||
|
|||||||
@@ -169,9 +169,7 @@ while (true)
|
|||||||
await using var app = builder.Build();
|
await using var app = builder.Build();
|
||||||
|
|
||||||
// ── Database initialization ──────────────────────────────────────────
|
// ── Database initialization ──────────────────────────────────────────
|
||||||
Console.Error.WriteLine("[DIAG] Database initialization starting...");
|
|
||||||
await DatabaseSetup.InitializeAsync(app.Services);
|
await DatabaseSetup.InitializeAsync(app.Services);
|
||||||
Console.Error.WriteLine("[DIAG] Database initialization complete.");
|
|
||||||
|
|
||||||
// ── Middleware ────────────────────────────────────────────────────────
|
// ── Middleware ────────────────────────────────────────────────────────
|
||||||
app.UseCors();
|
app.UseCors();
|
||||||
@@ -183,65 +181,7 @@ while (true)
|
|||||||
app.MapControllers();
|
app.MapControllers();
|
||||||
app.MapHub<ChatHub>(HubConstants.ChatHubPath);
|
app.MapHub<ChatHub>(HubConstants.ChatHubPath);
|
||||||
|
|
||||||
// ── Diagnostic hooks ────────────────────────────────────────────────
|
await app.RunAsync();
|
||||||
app.Lifetime.ApplicationStarted.Register(
|
|
||||||
() => Console.Error.WriteLine("[DIAG] ApplicationStarted fired"));
|
|
||||||
app.Lifetime.ApplicationStopping.Register(
|
|
||||||
() => Console.Error.WriteLine("[DIAG] ApplicationStopping fired"));
|
|
||||||
app.Lifetime.ApplicationStopped.Register(
|
|
||||||
() => Console.Error.WriteLine("[DIAG] ApplicationStopped fired"));
|
|
||||||
|
|
||||||
// Heartbeat — proves the process is alive even if nothing else logs
|
|
||||||
var heartbeatCts = new CancellationTokenSource();
|
|
||||||
_ = Task.Run(async () =>
|
|
||||||
{
|
|
||||||
while (!heartbeatCts.Token.IsCancellationRequested)
|
|
||||||
{
|
|
||||||
await Task.Delay(5000, heartbeatCts.Token).ConfigureAwait(false);
|
|
||||||
Console.Error.WriteLine($"[DIAG] heartbeat {DateTimeOffset.UtcNow:HH:mm:ss}");
|
|
||||||
}
|
|
||||||
}, heartbeatCts.Token);
|
|
||||||
|
|
||||||
// ── Config diagnostics ─────────────────────────────────────────────
|
|
||||||
Console.Error.WriteLine($"[DIAG] Irc:Enabled = {app.Configuration.GetValue<bool>("Irc:Enabled")}");
|
|
||||||
Console.Error.WriteLine($"[DIAG] Environment = {app.Environment.EnvironmentName}");
|
|
||||||
|
|
||||||
// ── Resolve singletons one-by-one to find which one hangs ────────
|
|
||||||
Console.Error.WriteLine("[DIAG] Resolving PresenceTracker...");
|
|
||||||
_ = app.Services.GetRequiredService<PresenceTracker>();
|
|
||||||
Console.Error.WriteLine("[DIAG] PresenceTracker OK.");
|
|
||||||
|
|
||||||
Console.Error.WriteLine("[DIAG] Resolving JwtTokenService...");
|
|
||||||
_ = app.Services.GetRequiredService<JwtTokenService>();
|
|
||||||
Console.Error.WriteLine("[DIAG] JwtTokenService OK.");
|
|
||||||
|
|
||||||
Console.Error.WriteLine("[DIAG] Resolving FileStorageService...");
|
|
||||||
_ = app.Services.GetRequiredService<FileStorageService>();
|
|
||||||
Console.Error.WriteLine("[DIAG] FileStorageService OK.");
|
|
||||||
|
|
||||||
Console.Error.WriteLine("[DIAG] Resolving IChatBroadcaster...");
|
|
||||||
_ = app.Services.GetServices<IChatBroadcaster>().ToList();
|
|
||||||
Console.Error.WriteLine("[DIAG] IChatBroadcaster OK.");
|
|
||||||
|
|
||||||
Console.Error.WriteLine("[DIAG] Resolving IChatService...");
|
|
||||||
_ = app.Services.GetRequiredService<IChatService>();
|
|
||||||
Console.Error.WriteLine("[DIAG] IChatService OK.");
|
|
||||||
|
|
||||||
Console.Error.WriteLine("[DIAG] Resolving IHostedService instances...");
|
|
||||||
var hostedServices = app.Services.GetServices<IHostedService>().ToList();
|
|
||||||
Console.Error.WriteLine($"[DIAG] Found {hostedServices.Count} hosted services:");
|
|
||||||
foreach (var svc in hostedServices)
|
|
||||||
Console.Error.WriteLine($"[DIAG] - {svc.GetType().FullName}");
|
|
||||||
Console.Error.Flush();
|
|
||||||
|
|
||||||
// ── Start with per-service timing ─────────────────────────────────
|
|
||||||
Console.Error.WriteLine("[DIAG] Calling app.StartAsync()...");
|
|
||||||
await app.StartAsync();
|
|
||||||
Console.Error.WriteLine("[DIAG] app.StartAsync() completed — server is running.");
|
|
||||||
|
|
||||||
await app.WaitForShutdownAsync();
|
|
||||||
Console.Error.WriteLine("[DIAG] WaitForShutdownAsync returned.");
|
|
||||||
heartbeatCts.Cancel();
|
|
||||||
|
|
||||||
// Graceful shutdown (Ctrl+C) — exit the loop
|
// Graceful shutdown (Ctrl+C) — exit the loop
|
||||||
Log.Information("Server shut down gracefully");
|
Log.Information("Server shut down gracefully");
|
||||||
@@ -249,8 +189,6 @@ while (true)
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.Error.WriteLine($"[DIAG] Top-level exception: {ex}");
|
|
||||||
|
|
||||||
var uptime = DateTimeOffset.UtcNow - startTime;
|
var uptime = DateTimeOffset.UtcNow - startTime;
|
||||||
|
|
||||||
// If server ran for over 60 seconds, it's a runtime crash — reset failure count
|
// If server ran for over 60 seconds, it's a runtime crash — reset failure count
|
||||||
@@ -273,6 +211,4 @@ while (true)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.Error.WriteLine("[DIAG] Calling Log.CloseAndFlush()...");
|
|
||||||
Log.CloseAndFlush();
|
Log.CloseAndFlush();
|
||||||
Console.Error.WriteLine("[DIAG] Log.CloseAndFlush() done. Exiting process.");
|
|
||||||
|
|||||||
@@ -28,13 +28,10 @@ public sealed class ServerDirectoryService : BackgroundService
|
|||||||
|
|
||||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
{
|
{
|
||||||
Console.Error.WriteLine("[DIAG] ServerDirectoryService.ExecuteAsync entered.");
|
|
||||||
// Yield to let the host finish starting before we log or connect
|
// Yield to let the host finish starting before we log or connect
|
||||||
await Task.Yield();
|
await Task.Yield();
|
||||||
Console.Error.WriteLine("[DIAG] ServerDirectoryService.ExecuteAsync resumed after Task.Yield().");
|
|
||||||
|
|
||||||
var isPublic = _configuration.GetValue<bool>("Server:PublicServer");
|
var isPublic = _configuration.GetValue<bool>("Server:PublicServer");
|
||||||
Console.Error.WriteLine($"[DIAG] ServerDirectoryService: PublicServer={isPublic}");
|
|
||||||
if (!isPublic)
|
if (!isPublic)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("PublicServer is disabled — not registering with directory");
|
_logger.LogInformation("PublicServer is disabled — not registering with directory");
|
||||||
@@ -42,7 +39,6 @@ public sealed class ServerDirectoryService : BackgroundService
|
|||||||
}
|
}
|
||||||
|
|
||||||
var host = _configuration["Server:PublicHost"];
|
var host = _configuration["Server:PublicHost"];
|
||||||
Console.Error.WriteLine($"[DIAG] ServerDirectoryService: PublicHost={host}");
|
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(host))
|
if (string.IsNullOrWhiteSpace(host))
|
||||||
{
|
{
|
||||||
@@ -63,7 +59,6 @@ public sealed class ServerDirectoryService : BackgroundService
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Console.Error.WriteLine("[DIAG] ServerDirectoryService: Building new connection, entering try block.");
|
|
||||||
var connectionPermanentlyClosed = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
var connectionPermanentlyClosed = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||||
|
|
||||||
connection.Reconnected += async _ =>
|
connection.Reconnected += async _ =>
|
||||||
@@ -85,14 +80,9 @@ public sealed class ServerDirectoryService : BackgroundService
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Connect with retry
|
// Connect with retry
|
||||||
Console.Error.WriteLine("[DIAG] ServerDirectoryService: Calling ConnectWithRetryAsync...");
|
|
||||||
if (!await ConnectWithRetryAsync(connection, stoppingToken))
|
if (!await ConnectWithRetryAsync(connection, stoppingToken))
|
||||||
{
|
|
||||||
Console.Error.WriteLine("[DIAG] ServerDirectoryService: ConnectWithRetryAsync returned false (cancelled).");
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
Console.Error.WriteLine("[DIAG] ServerDirectoryService: Connected successfully!");
|
|
||||||
_logger.LogInformation("Successfully connected to EchoHubSpace API at {Url}", DirectoryHubUrl);
|
_logger.LogInformation("Successfully connected to EchoHubSpace API at {Url}", DirectoryHubUrl);
|
||||||
await RegisterAsync(serverName, description, host);
|
await RegisterAsync(serverName, description, host);
|
||||||
|
|
||||||
@@ -209,25 +199,20 @@ public sealed class ServerDirectoryService : BackgroundService
|
|||||||
|
|
||||||
private static async Task DisposeConnectionAsync(HubConnection connection)
|
private static async Task DisposeConnectionAsync(HubConnection connection)
|
||||||
{
|
{
|
||||||
Console.Error.WriteLine("[DIAG] ServerDirectoryService.DisposeConnectionAsync entered.");
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await connection.DisposeAsync()
|
await connection.DisposeAsync()
|
||||||
.AsTask().WaitAsync(TimeSpan.FromSeconds(3));
|
.AsTask().WaitAsync(TimeSpan.FromSeconds(3));
|
||||||
Console.Error.WriteLine("[DIAG] ServerDirectoryService.DisposeConnectionAsync completed normally.");
|
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
Console.Error.WriteLine("[DIAG] ServerDirectoryService.DisposeConnectionAsync timed out or failed (3s).");
|
|
||||||
// Don't let a slow dispose block shutdown
|
// Don't let a slow dispose block shutdown
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override async Task StopAsync(CancellationToken cancellationToken)
|
public override async Task StopAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
Console.Error.WriteLine("[DIAG] ServerDirectoryService.StopAsync entered.");
|
|
||||||
await base.StopAsync(cancellationToken);
|
await base.StopAsync(cancellationToken);
|
||||||
Console.Error.WriteLine("[DIAG] ServerDirectoryService.StopAsync: base.StopAsync returned.");
|
|
||||||
_connection = null;
|
_connection = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user