diff --git a/src/Febe.Core/Engine/SessionComposer.cs b/src/Febe.Core/Engine/SessionComposer.cs index e736465..cd18075 100644 --- a/src/Febe.Core/Engine/SessionComposer.cs +++ b/src/Febe.Core/Engine/SessionComposer.cs @@ -839,7 +839,12 @@ internal sealed class SessionComposer case RecipeStatus.StoppedBySignal: case RecipeStatus.Cancelled: - if (task is not null) + if (task is not null && kind == RecipeKind.Backfill && outcome.BackfillFinished) + { + // The walk got to the end before the run stopped: nothing to take up again (closed above). + writes.Add(Store.Tasks.CompleteAsync(task.Id, now, ct)); + } + else if (task is not null) { await RequeueAsync(task, outcome.Status == RecipeStatus.Cancelled ? "Interrotta" : "Fermata da un segnale", now).ConfigureAwait(false); } @@ -1014,7 +1019,18 @@ internal sealed class SessionComposer await Store.Accounts.SetBackfillAsync(account.Id, fresh.BackfillCursor, now, ct).ConfigureAwait(false); var newState = fresh.KeepUpdated ? AccountState.UpToDate : AccountState.Paused; await Store.Accounts.SetStateAsync(account.Id, newState, now, null, ct).ConfigureAwait(false); - var total = await Store.Entries.CountDiscoveredAsync(account.Id, EntryKind.Post, DateTimeOffset.UnixEpoch, now.AddSeconds(1), ct).ConfigureAwait(false); + + // The Coda follows at once, not at the next session: no «Storico» left for the account, and nothing planned + // for an account that stays paused. Commands of Alberto stay; the task in flight is settled by its outcome. + foreach (var task in await Store.Tasks.ListPendingAsync(10_000, ct).ConfigureAwait(false)) + { + if (task.AccountId == account.Id && !task.IsManual && (newState == AccountState.Paused || task.Kind == EngineTaskKind.Backfill)) + { + await Store.Tasks.CancelAsync(task.Id, now, ct).ConfigureAwait(false); + } + } + + var total =await Store.Entries.CountDiscoveredAsync(account.Id, EntryKind.Post, DateTimeOffset.UnixEpoch, now.AddSeconds(1), ct).ConfigureAwait(false); var tail = fresh.KeepUpdated ? " Da ora lo tengo aggiornato." : " «Tieni aggiornato» è spento: l'account resta in pausa."; diff --git a/tests/Febe.Tests/Engine/BackfillCompletedTests.cs b/tests/Febe.Tests/Engine/BackfillCompletedTests.cs index 3eedbd5..2973ee0 100644 --- a/tests/Febe.Tests/Engine/BackfillCompletedTests.cs +++ b/tests/Febe.Tests/Engine/BackfillCompletedTests.cs @@ -67,37 +67,60 @@ public sealed class BackfillCompletedTests } /// - /// The walk reached the beginning of the profile and stored it, then a signal or a cancellation changed how the - /// run ended: the history is complete all the same and keeps its line (it would never be walked again). + /// A run that completes the history: the walk reached the beginning of the profile and stored it, then a signal or + /// a cancellation changed how the run ended; or a profile check found the whole profile on its first page. The + /// history is closed with its line (it would never be walked again), and right after that run the Coda shows + /// nothing planned for the account, now paused: no «Storico» put back in the queue (review F3). /// [TestMethod] - [DataRow(RecipeStatus.StoppedBySignal)] - [DataRow(RecipeStatus.Cancelled)] - public async Task AHistoryCompletedRightBeforeARunEndsOtherwiseKeepsItsLine(RecipeStatus status) + [DataRow(RecipeKind.Backfill, RecipeStatus.StoppedBySignal)] + [DataRow(RecipeKind.Backfill, RecipeStatus.Cancelled)] + [DataRow(RecipeKind.ProfileCheck, RecipeStatus.Completed)] + public async Task AHistoryCompletedByARunIsClosedWithItsLineAndLeavesNothingInTheQueue(RecipeKind kind, RecipeStatus status) { await using var bed = await AlertBed.CreateAsync(start: Morning); var engine = bed.Engine; var account = await engine.AddAccountAsync("luca.prova", followed: true, state: AccountState.Backfilling, postCount: 30); await engine.Db.Accounts.UpdateAsync((await engine.Db.Accounts.GetAsync(account.Id))! with { KeepUpdated = false }); var runner = new ScriptedRunner(engine.Governor, engine.Clock); - var backfills = 0; + RecipeRequest? closing = null; + var walksAfter = 0; runner.Before = async r => { - if (r.Kind == RecipeKind.Backfill && r.AccountId == account.Id && ++backfills == 1) + if (r.AccountId != account.Id) { - // What the recipe stores when the walk meets the end of the list. + return; + } + + if (closing is not null) + { + walksAfter += r.Kind == RecipeKind.Backfill ? 1 : 0; + } + else if (r.Kind == kind) + { + // What the recipe stores: the walk met the end of the list, or the whole profile is on its first page. await engine.Db.Accounts.SetBackfillAsync(account.Id, null, engine.Clock.Now); + closing = r; } }; - runner.Script = r => r.Kind == RecipeKind.Backfill && r.AccountId == account.Id && backfills == 1 - ? new RecipeOutcome(status, 1, 6) { BackfillFinished = true } - : null; + runner.Script = r => ReferenceEquals(r, closing) ? new RecipeOutcome(status, 1, 6) { BackfillFinished = true } : null; engine.Start(runner); - await engine.RunUntilAsync(Morning.AddDays(2)); + var end = Morning.AddDays(2); + while (closing is null && engine.Clock.Now < end) + { + await engine.RunForAsync(TimeSpan.FromMinutes(1)); + } + + // Right after that run, before the next session tidies the queue. + Assert.IsNotNull(closing); + var queued = (await engine.Db.Tasks.ListPendingAsync(1000)).Where(t => t.AccountId == account.Id).ToList(); + Assert.HasCount(0, queued, "Planned for a paused account: " + string.Join(", ", queued.Select(t => $"{t.Kind} «{t.LastError}»"))); + + await engine.RunUntilAsync(end); await engine.StopAsync(); await bed.Alerts.FlushAsync(); - Assert.AreEqual(1, backfills, "Never walked again."); + Assert.AreEqual(0, walksAfter, "Never walked again."); var finished = await engine.Db.Accounts.GetAsync(account.Id); Assert.AreEqual(AccountState.Paused, finished!.State, "«Tieni aggiornato» off: one download, then paused."); var lines = await engine.Db.Events.ListAsync(new EventQuery { Codes = [EngineEventCodes.BackfillCompleted] });