M3 Motore: Governatore con tetti da action_log, interruttore con fermi 1/6/24 ore e ripresa manuale, fascia giornaliera spostata a caso e salvata, sessioni con pause log-normali e pavimento, composizione delle sessioni sulla coda persistente, «Controlla ora» con conferma fuori fascia, cruscotto per l'interfaccia e calcolatore «Con queste impostazioni», simulazione con orologio virtuale e comando --simula; prove del motore e dell'interruttore
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
This commit is contained in:
38 files changed
+9308
No files matched your search
@@ -0,0 +1,148 @@
|
||||
using System.Globalization;
|
||||
using Febe.Core.Abstractions;
|
||||
using Febe.Core.Domain;
|
||||
using Febe.Core.Storage;
|
||||
|
||||
namespace Febe.Core.Engine;
|
||||
|
||||
/// <summary>
|
||||
/// The parts of the engine that recipes and the interface need before the engine host exists: runtime state,
|
||||
/// timeline, day planner, circuit breaker and Governor. Built in two steps because the real recipe runner needs the
|
||||
/// Governor and the breaker, and the host needs the recipe runner:
|
||||
/// <code>
|
||||
/// var services = await EngineServices.CreateAsync(storage, clock, random);
|
||||
/// var runner = new SomeRecipeRunner(services.Governor, services.Breaker, ...);
|
||||
/// await using var engine = new EngineHost(services, runner);
|
||||
/// await engine.StartAsync(ct);
|
||||
/// </code>
|
||||
/// </summary>
|
||||
public sealed class EngineServices
|
||||
{
|
||||
private EngineServices(
|
||||
IFebeStorage storage,
|
||||
IClock clock,
|
||||
IRandomSource random,
|
||||
EngineOptions options,
|
||||
EngineRuntime runtime,
|
||||
EngineEvents events,
|
||||
DayPlanner planner,
|
||||
CircuitBreaker breaker,
|
||||
Governor governor,
|
||||
EngineSettings settings)
|
||||
{
|
||||
Storage = storage;
|
||||
Clock = clock;
|
||||
Random = random;
|
||||
Options = options;
|
||||
Runtime = runtime;
|
||||
Events = events;
|
||||
Planner = planner;
|
||||
Breaker = breaker;
|
||||
Governor = governor;
|
||||
Settings = settings;
|
||||
}
|
||||
|
||||
public IFebeStorage Storage { get; }
|
||||
|
||||
public IClock Clock { get; }
|
||||
|
||||
public IRandomSource Random { get; }
|
||||
|
||||
public EngineOptions Options { get; }
|
||||
|
||||
public EngineRuntime Runtime { get; }
|
||||
|
||||
public EngineEvents Events { get; }
|
||||
|
||||
public DayPlanner Planner { get; }
|
||||
|
||||
public CircuitBreaker Breaker { get; }
|
||||
|
||||
/// <summary>The Governor: <see cref="IGovernor"/> for recipes, <see cref="IDataBudget"/> for the download queue.</summary>
|
||||
public Governor Governor { get; }
|
||||
|
||||
/// <summary>Current engine and caution settings.</summary>
|
||||
public EngineSettings Settings { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Loads settings, pause flag, gradual-start origin and breaker state from the database. When the gradual start has
|
||||
/// no origin yet (first run after the first connection) it starts now.
|
||||
/// </summary>
|
||||
/// <param name="storage">The database.</param>
|
||||
/// <param name="clock">Time source (virtual in tests and simulation).</param>
|
||||
/// <param name="random">Random source (seeded in tests and simulation).</param>
|
||||
/// <param name="options">Tuning; <see cref="EngineOptions.Default"/> when null.</param>
|
||||
/// <param name="userIsDriving">True while Alberto uses the browser.</param>
|
||||
/// <param name="isConnected">Whether the browser holds an Instagram session; null when unknown.</param>
|
||||
/// <param name="ct">Cancellation.</param>
|
||||
public static async Task<EngineServices> CreateAsync(
|
||||
IFebeStorage storage,
|
||||
IClock clock,
|
||||
IRandomSource random,
|
||||
EngineOptions? options = null,
|
||||
Func<bool>? userIsDriving = null,
|
||||
Func<bool>? isConnected = null,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(storage);
|
||||
ArgumentNullException.ThrowIfNull(clock);
|
||||
ArgumentNullException.ThrowIfNull(random);
|
||||
options ??= EngineOptions.Default;
|
||||
|
||||
var runtime = new EngineRuntime(userIsDriving, isConnected);
|
||||
var settings = await EngineSettingsStore.LoadAsync(storage.Settings, ct).ConfigureAwait(false);
|
||||
runtime.Profile = settings.EffectiveProfile;
|
||||
runtime.IsPaused = await storage.Settings.GetBoolAsync(EngineSettingKeys.Paused, false, ct).ConfigureAwait(false);
|
||||
|
||||
var rampStart = await storage.Settings.GetInt64Async(EngineSettingKeys.RampStart, ct).ConfigureAwait(false);
|
||||
if (rampStart is null)
|
||||
{
|
||||
rampStart = clock.Now.ToUnixTimeMilliseconds();
|
||||
await storage.Settings.SetInt64Async(EngineSettingKeys.RampStart, rampStart, ct).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
runtime.RampStart = DateTimeOffset.FromUnixTimeMilliseconds(rampStart.Value);
|
||||
|
||||
var events = new EngineEvents(storage.Events, clock);
|
||||
var planner = new DayPlanner(storage.Settings, random, () => runtime.Profile, clock.LocalZone);
|
||||
|
||||
// Draws (or loads) the seed of the daily windows now, so that later readers never consume randomness.
|
||||
await planner.GetNextWindowAsync(clock.Now, ct).ConfigureAwait(false);
|
||||
var breaker = await CircuitBreaker.LoadAsync(storage, clock, runtime, events, ct).ConfigureAwait(false);
|
||||
var governor = new Governor(storage, breaker, runtime, planner, clock, options, events);
|
||||
return new EngineServices(storage, clock, random, options, runtime, events, planner, breaker, governor, settings);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates and stores new engine settings (mockup 07) and applies them at once. Throws
|
||||
/// <see cref="ArgumentException"/> with Italian messages when a value is outside the limits of spec §7.5.
|
||||
/// </summary>
|
||||
public async Task ApplySettingsAsync(EngineSettings settings, CancellationToken ct = default)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(settings);
|
||||
await EngineSettingsStore.SaveAsync(Storage.Settings, settings, ct).ConfigureAwait(false);
|
||||
Settings = settings;
|
||||
Runtime.Profile = settings.EffectiveProfile;
|
||||
|
||||
await Events.WriteAsync(
|
||||
EventSeverity.Info,
|
||||
EngineEventCodes.SettingsChanged,
|
||||
$"Impostazioni del motore cambiate: profilo «{ItalianText.Profile(settings.ProfileKind)}».",
|
||||
ct: ct).ConfigureAwait(false);
|
||||
Events.Publish(EngineNotificationKind.StateChanged);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The first connection to Instagram (spec §7.2): the gradual start begins now. Later connections do not restart it;
|
||||
/// the resume after a verification or a disconnection does, through <see cref="CircuitBreaker.ResumeAsync"/>.
|
||||
/// </summary>
|
||||
public async Task NotifyFirstConnectionAsync(CancellationToken ct = default)
|
||||
{
|
||||
var now = Clock.Now;
|
||||
Runtime.RampStart = now;
|
||||
await Storage.Settings.SetAsync(
|
||||
EngineSettingKeys.RampStart,
|
||||
now.ToUnixTimeMilliseconds().ToString(CultureInfo.InvariantCulture),
|
||||
ct).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user