Fix: abandon stale Initializing sessions on Game startup
When SOR.Game is redeployed, sessions in Initializing status are unreachable — clients already sent game.ready to the old process and won't retry. GameStartupCleanupService runs once at startup, marks all Initializing sessions as Abandoned (WinCondition = "server_restart"), and pushes game.session_abandoned to both players so clients can return to the main menu instead of hanging on the loading screen. Also fix MatchmakerService to include WinnerOpReward, LoserOpReward, and RankDiffOpMultiplierPct in the rule snapshot so FinalizeGameAsync picks up the correct OP rewards from the snapshot. Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
@@ -180,6 +180,13 @@ public sealed record GameOverPush(
|
||||
int WinnerOpDelta,
|
||||
int LoserOpDelta);
|
||||
|
||||
/// <summary>
|
||||
/// Pushed to both players when a session that was Initializing is abandoned
|
||||
/// (e.g., server restart before both players sent game.ready).
|
||||
/// Clients should return to the main menu and show an appropriate message.
|
||||
/// </summary>
|
||||
public sealed record GameSessionAbandonedPush(Guid SessionId, string Reason);
|
||||
|
||||
/// <summary>
|
||||
/// Pushed to both players when a custom lobby is full and a GameSession has been created.
|
||||
/// Players should navigate to the game screen and send game.ready.
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
namespace SOR.Game;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SOR.Database.Contexts;
|
||||
using SOR.Database.Enums;
|
||||
using SOR.Networking.Abstractions.Push;
|
||||
using SOR.Shared.Networking;
|
||||
|
||||
/// <summary>
|
||||
/// Runs once at startup before the service begins accepting WebSocket messages.
|
||||
///
|
||||
/// Any GameSession stuck in the Initializing state means the previous Game process
|
||||
/// died (redeploy, crash) before both players sent game.ready. The clients on
|
||||
/// those sessions are stuck on a loading screen and will never recover on their own
|
||||
/// because they already sent game.ready to the old process.
|
||||
///
|
||||
/// This service abandons every Initializing session and pushes game.session_abandoned
|
||||
/// to both players so their clients can return to the main menu.
|
||||
/// </summary>
|
||||
internal sealed class GameStartupCleanupService : IHostedService
|
||||
{
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
private readonly ILogger<GameStartupCleanupService> _logger;
|
||||
|
||||
public GameStartupCleanupService(
|
||||
IServiceScopeFactory scopeFactory,
|
||||
ILogger<GameStartupCleanupService> logger)
|
||||
{
|
||||
_scopeFactory = scopeFactory;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await using var scope = _scopeFactory.CreateAsyncScope();
|
||||
var gameDb = scope.ServiceProvider.GetRequiredService<GameDbContext>();
|
||||
var clientPush = scope.ServiceProvider.GetRequiredService<IClientPush>();
|
||||
|
||||
var stuck = await gameDb.GameSessions
|
||||
.Where(s => s.Status == GameSessionStatus.Initializing)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
if (stuck.Count == 0)
|
||||
return;
|
||||
|
||||
_logger.LogWarning(
|
||||
"Startup cleanup: abandoning {Count} Initializing session(s) left over from previous process.",
|
||||
stuck.Count);
|
||||
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
var push = new GameSessionAbandonedPush(Guid.Empty, "server_restart");
|
||||
|
||||
foreach (var session in stuck)
|
||||
{
|
||||
session.Status = GameSessionStatus.Abandoned;
|
||||
session.EndedAt = now;
|
||||
session.WinCondition = "server_restart";
|
||||
|
||||
// Best-effort push — clients on the loading screen will get a signal to
|
||||
// return to menu. Ignore errors so a bad Redis connection doesn't block
|
||||
// the rest of the cleanup or prevent the service from starting.
|
||||
try
|
||||
{
|
||||
var sessionPush = push with { SessionId = session.Id };
|
||||
await Task.WhenAll(
|
||||
clientPush.PushAsync(
|
||||
session.PlayerOneAccountId.ToString(),
|
||||
ServerMessage.Push("game.session_abandoned", sessionPush),
|
||||
cancellationToken),
|
||||
clientPush.PushAsync(
|
||||
session.PlayerTwoAccountId.ToString(),
|
||||
ServerMessage.Push("game.session_abandoned", sessionPush),
|
||||
cancellationToken));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex,
|
||||
"Could not push session_abandoned for session {SessionId}; clients will time out naturally.",
|
||||
session.Id);
|
||||
}
|
||||
}
|
||||
|
||||
await gameDb.SaveChangesAsync(cancellationToken);
|
||||
|
||||
_logger.LogInformation(
|
||||
"Startup cleanup complete: {Count} session(s) abandoned.", stuck.Count);
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||
}
|
||||
@@ -21,6 +21,9 @@ public static class ServiceRegistration
|
||||
services.AddCollectionDatabase(configuration);
|
||||
services.AddCatalogDatabase(configuration);
|
||||
|
||||
// Abandons any Initializing sessions left over from a previous process (redeploy/crash).
|
||||
services.AddHostedService<GameStartupCleanupService>();
|
||||
|
||||
services.AddTransient<IMessageHandler, GameReadyHandler>();
|
||||
services.AddTransient<IMessageHandler, GameGetStateHandler>();
|
||||
services.AddTransient<IMessageHandler, GameDrawHandler>();
|
||||
|
||||
@@ -128,6 +128,9 @@ internal sealed class MatchmakerService : BackgroundService
|
||||
rankedRules.DrawPerTurn,
|
||||
rankedRules.SpHasCap,
|
||||
rankedRules.SpCap,
|
||||
rankedRules.WinnerOpReward,
|
||||
rankedRules.LoserOpReward,
|
||||
rankedRules.RankDiffOpMultiplierPct,
|
||||
})
|
||||
: null,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user