Recover missed work after an absence by the rules of SPEC 5.6 (refs #49)
At the start the host measures how long the bot was away from the last job that finished, records the absence and writes it in the diary; one longer than a day is kept for the Panoramica until the user has seen it. A job can now say how a run missed while the bot was off is treated: the nightly backup, the exchange rates, the market calendar and the cleanup run once at the restart, queued, however late; a job with a deadline is skipped past it and the diary says so. Corporate actions are read before the reconciliation puts any stop back. A decision the engine makes late because of an absence is written in the diary as recovered, as are the orders it releases late; entries that moved too far or expired are dropped with their reason. Tests switch the bot off for 3 hours, 20 hours and 3 days with a manual clock and check each of those rows. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
41e067e7d1
commit
680cab4bb6
9 files changed
+454
-18
No files matched your search
@@ -312,7 +312,28 @@ internal sealed partial class AnubisHost(
|
||||
// The laboratory computes on its own thread and reads only local data; the demo shows appendix F and runs none.
|
||||
_lab = options.Demo ? null : new Laboratory(_db, clock, Config.Data.Research, logger);
|
||||
_services = new AppServices(_db, settings, secrets, access, backups, scheduler, _market, _trading, _lab);
|
||||
StartScheduler(scheduler, backups);
|
||||
|
||||
// Before the scheduler writes its first run: how long the bot was away, and which runs it missed (SPEC 5.6).
|
||||
var lastDues = new Dictionary<string, DateTimeOffset>(StringComparer.Ordinal);
|
||||
try
|
||||
{
|
||||
if (!options.Demo)
|
||||
{
|
||||
await new AbsenceBook(_db, clock) { Zone = Config.TimeZone }.RecordAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
foreach (var (name, due) in await _db.ReadAsync(s => s.QueryAsync("SELECT name, last_due_at FROM jobs WHERE last_due_at IS NOT NULL",
|
||||
r => (r.Text("name"), r.Moment("last_due_at"))), cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
lastDues[name] = due;
|
||||
}
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
LogStepFailed(logger, ex);
|
||||
}
|
||||
|
||||
StartScheduler(scheduler, backups, lastDues);
|
||||
SetStatus(HostMode.Ready, null);
|
||||
}
|
||||
|
||||
@@ -431,14 +452,15 @@ internal sealed partial class AnubisHost(
|
||||
!options.Demo && (overrides is null ? true : overrides.Trading == true) &&
|
||||
environment.GetValueOrDefault("ANUBIS_TRADING") is not ("0" or "off" or "false");
|
||||
|
||||
private void StartScheduler(Scheduler scheduler, Backups backups)
|
||||
private void StartScheduler(Scheduler scheduler, Backups backups, Dictionary<string, DateTimeOffset> lastDues)
|
||||
{
|
||||
scheduler.Add(new ScheduledJob("backup", "Salvataggio del database", Schedule.Daily(new TimeOnly(3, 30), Config.TimeZone),
|
||||
// A backup missed while the bot was off is taken once at the restart, whenever it comes (SPEC 5.6).
|
||||
var backup = new ScheduledJob("backup", "Salvataggio del database", Schedule.Daily(new TimeOnly(3, 30), Config.TimeZone),
|
||||
async ct => await backups.TakeAsync(_db!, "notturno", ct).ConfigureAwait(false))
|
||||
{ MaxLateness = TimeSpan.FromHours(2) });
|
||||
foreach (var job in (_market?.Jobs() ?? []).Concat(_trading?.Jobs() ?? []))
|
||||
{ MaxLateness = TimeSpan.FromHours(2), CatchUpWithin = TimeSpan.FromDays(3650) };
|
||||
foreach (var job in new[] { backup }.Concat(_market?.Jobs() ?? []).Concat(_trading?.Jobs() ?? []))
|
||||
{
|
||||
scheduler.Add(job);
|
||||
scheduler.Add(job, lastDues.TryGetValue(job.Name, out var due) ? due : null);
|
||||
}
|
||||
|
||||
_schedulerStop = new CancellationTokenSource();
|
||||
@@ -525,7 +547,13 @@ internal sealed partial class AnubisHost(
|
||||
{
|
||||
await _db!.WriteAsync(async s =>
|
||||
{
|
||||
var description = _services?.Scheduler.Upcoming(100).FirstOrDefault(u => u.Name == run.Name)?.Description ?? run.Name;
|
||||
var description = run.Description ?? _services?.Scheduler.Upcoming(100).FirstOrDefault(u => u.Name == run.Name)?.Description ?? run.Name;
|
||||
if (AbsenceBook.MissedLine(run with { Description = description }, Config.TimeZone) is { } line)
|
||||
{
|
||||
await Diary.WriteAsync(s, clock, DiaryKinds.System, run.Outcome == JobOutcome.Completed ? "job_recovered" : "job_skipped", line,
|
||||
new { job = run.Name, due = run.DueUtc }, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var updated = await s.ExecuteAsync(
|
||||
"UPDATE jobs SET last_due_at = @d, last_started_at = @s, last_finished_at = @f, last_outcome = @o, last_error = @e, updated_at = @f WHERE name = @n",
|
||||
("d", run.DueUtc), ("s", run.StartedUtc), ("f", run.FinishedUtc), ("o", run.Outcome.ToString().ToLowerInvariant()), ("e", run.Error), ("n", run.Name)).ConfigureAwait(false);
|
||||
|
||||
Reference in new issue
Block a user