Files
ClaudeDo/src/ClaudeDo.Worker/Lifecycle/AttachmentOrphanRecovery.cs
T
mika kuns 6ec81f653b feat(worker): surface startup recovery via OperationProgress channel
The six Lifecycle/*Recovery hosted services now broadcast one
OperationProgress("startup-recovery", <phase>, current, total) message each
after they finish, instead of leaving the UI on a bare "connecting" text
during worker startup. IslandsShellViewModel subscribes and swaps in
"Recovering... (i/n)" (existing ops.worker.startupRecovery key, no locale
changes) while Worker.IsReconnecting is true, and clears it once actually
connected so a later transient reconnect doesn't replay stale text.

OperationProgress broadcasts to Clients.All with no replay-on-connect, so a
UI that hasn't finished its SignalR handshake yet can miss some or all of
these messages and simply keep showing "connecting" as before -- accepted
rather than adding a cached-state + reconnect-replay path (mirroring
RefreshExternalMergeConflictsAsync) for what is a fast, best-effort,
local-only startup sweep with no UI-visible failure mode beyond that.
2026-08-21 13:36:25 +02:00

61 lines
2.2 KiB
C#

using ClaudeDo.Data;
using ClaudeDo.Worker.Hub;
using Microsoft.EntityFrameworkCore;
namespace ClaudeDo.Worker.Lifecycle;
/// <summary>
/// Startup-only sweep: deletes attachment directories whose task no longer exists in the DB.
/// </summary>
public sealed class AttachmentOrphanRecovery : IHostedService
{
public const string Phase = "attachments";
private readonly IDbContextFactory<ClaudeDoDbContext> _dbFactory;
private readonly AttachmentStore _store;
private readonly HubBroadcaster _broadcaster;
private readonly ILogger<AttachmentOrphanRecovery> _logger;
public AttachmentOrphanRecovery(
IDbContextFactory<ClaudeDoDbContext> dbFactory,
AttachmentStore store,
HubBroadcaster broadcaster,
ILogger<AttachmentOrphanRecovery> logger)
{
_dbFactory = dbFactory;
_store = store;
_broadcaster = broadcaster;
_logger = logger;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
var taskIds = _store.EnumerateTaskIds();
if (taskIds.Count == 0)
{
_logger.LogInformation("Attachment orphan recovery: no attachment directories found");
await _broadcaster.OperationProgress(OperationProgressOpKeys.StartupRecovery, Phase, 0, 0);
return;
}
await using var ctx = await _dbFactory.CreateDbContextAsync(cancellationToken);
var existingIds = (await ctx.Tasks
.Where(t => taskIds.Contains(t.Id))
.Select(t => t.Id)
.ToListAsync(cancellationToken)).ToHashSet();
var orphans = taskIds.Where(id => !existingIds.Contains(id)).ToList();
foreach (var id in orphans)
_store.DeleteTaskDir(id);
if (orphans.Count > 0)
_logger.LogWarning("Attachment orphan recovery: removed {Count} orphaned attachment director(ies)", orphans.Count);
else
_logger.LogInformation("Attachment orphan recovery: no orphaned attachment directories found");
await _broadcaster.OperationProgress(OperationProgressOpKeys.StartupRecovery, Phase, taskIds.Count, taskIds.Count);
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}