#region ========================================================================= USING ===================================================================================== using Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate.ValueObjects; using System.Collections.Concurrent; using System.Threading; #endregion namespace Lumina.Infrastructure.Core.Scheduling.Execution; /// /// Registry that holds the live runtime state of the scheduled jobs. /// public sealed class ScheduledJobRuntimeRegistry : IScheduledJobRuntimeRegistry { /// /// Class that holds the live runtime state of the execution cycle of a scheduled job. /// private sealed class ScheduledJobRuntimeState { /// /// Object used to synchronize the access to the runtime state of the scheduled job. /// public readonly object SyncRoot = new(); /// /// Gets or sets the cancellation token source of the running execution cycle, or when no cycle is running. /// public CancellationTokenSource? CycleCancellationTokenSource; /// /// Gets the slot that guards a scheduled job against overlapping executions. /// public readonly SemaphoreSlim RunSlot = new(1, 1); } // One runtime state instance exists per scheduled job, and it is shared by every thread that works on that job: the // cycle worker of the job, the request handlers that stop or remove the job, and the one time (fired) executions. The // ConcurrentDictionary only makes the lookup of the state instance itself thread safe; the fields of the instance are // still plain mutable fields, so every read or write of them is guarded by the per-state lock. The lock is only ever // held for the few instructions that mutate or read the state, never while a cancellation callback runs, and the lock // is reentrant for the thread that owns it, which keeps the registry free of deadlocks. private readonly ConcurrentDictionary _runtimeStates = []; /// /// Starts the execution cycle of the scheduled job identified by . /// /// The object representing the unique identifier of the scheduled job whose cycle is started. /// The cancellation token source used to stop the execution cycle. /// when the cycle was started, when a cycle was already running. public bool TryStartCycle(ScheduledJobId scheduledJobId, CancellationTokenSource cycleCancellationTokenSource) { ScheduledJobRuntimeState state = _runtimeStates.GetOrAdd(scheduledJobId, static _ => new ScheduledJobRuntimeState()); lock (state.SyncRoot) { // The check and the assignment must be one atomic step: without the lock, two concurrent starts of the same job could both observe that // no cycle is running and both store their own cancellation token source, so a job would end up with two live cycles at once. // The lock serializes the starts, so exactly one wins. if (state.CycleCancellationTokenSource is not null) return false; state.CycleCancellationTokenSource = cycleCancellationTokenSource; return true; } } /// /// Gets a value indicating whether the execution cycle of the scheduled job identified by is currently running. /// /// The object representing the unique identifier of the scheduled job whose cycle is checked. /// when a cycle is currently running, otherwise. public bool HasActiveCycle(ScheduledJobId scheduledJobId) { if (!_runtimeStates.TryGetValue(scheduledJobId, out ScheduledJobRuntimeState? state)) return false; // The read takes the same lock the writers use, so it never observes a partially updated state, and the lock also acts as the memory barrier // that guarantees the read sees the latest value written by another thread. lock (state.SyncRoot) return state.CycleCancellationTokenSource is not null; } /// /// Stops the execution cycle of the scheduled job identified by . /// /// The object representing the unique identifier of the scheduled job whose cycle is stopped. public void StopCycle(ScheduledJobId scheduledJobId) { if (!_runtimeStates.TryGetValue(scheduledJobId, out ScheduledJobRuntimeState? state)) return; CancellationTokenSource? cycleCancellationTokenSource; lock (state.SyncRoot) { // The stored token source is read and cleared while the lock is held, so that a start that races with this stop either sees the cycle and loses, // or sees no cycle and starts a fresh one whose token source is not the one cancelled here. cycleCancellationTokenSource = state.CycleCancellationTokenSource; state.CycleCancellationTokenSource = null; } // Cancelling a token source synchronously invokes every callback registered on its token, and those callbacks can reach back into this registry // (for example the cycle worker that unwinds and ends its own cycle). The lock is therefore released before the cancellation runs, // so no callback ever executes while the lock is held, and the lock is never held longer than the few instructions that mutate the state. cycleCancellationTokenSource?.Cancel(); } /// /// Ends the execution cycle of the scheduled job identified by , when it was started with . /// /// The object representing the unique identifier of the scheduled job whose cycle is ended. /// The cancellation token source of the cycle that is ending. /// /// The cycle worker calls this method when its cycle finishes, so that a cycle that was replaced by a newer one /// after being stopped can never cancel the cycle that replaced it. /// public void EndCycle(ScheduledJobId scheduledJobId, CancellationTokenSource cycleCancellationTokenSource) { if (_runtimeStates.TryGetValue(scheduledJobId, out ScheduledJobRuntimeState? state)) lock (state.SyncRoot) // The identity check matters for the stop-then-start race: when a worker is stopped and a newer cycle is started before the old worker reaches // its cleanup, the stored token source already belongs to the new cycle. The old worker must only clear the field when it still owns it, // so it can never cancel the cycle that replaced it. if (ReferenceEquals(state.CycleCancellationTokenSource, cycleCancellationTokenSource)) state.CycleCancellationTokenSource = null; // Cancelling the worker's own token source is always safe and idempotent, and runs outside the lock for the same reason as in StopCycle: // the cancellation callbacks must not execute while the lock is held. cycleCancellationTokenSource.Cancel(); } /// /// Attempts to reserve the execution slot of the scheduled job identified by . /// /// The object representing the unique identifier of the scheduled job whose execution slot is reserved. /// when the slot was reserved, when another execution is already running. public bool TryAcquireRunSlot(ScheduledJobId scheduledJobId) { ScheduledJobRuntimeState state = _runtimeStates.GetOrAdd(scheduledJobId, static _ => new ScheduledJobRuntimeState()); lock (state.SyncRoot) // The slot is acquired under the same lock that guards the rest of the state, so the acquire is serialized // with the cycle start and end transitions of the job; Wait(0) never blocks, so the lock is held only for a single non blocking instruction. return state.RunSlot.Wait(0); } /// /// Releases the execution slot of the scheduled job identified by . /// /// The object representing the unique identifier of the scheduled job whose execution slot is released. public void ReleaseRunSlot(ScheduledJobId scheduledJobId) { if (!_runtimeStates.TryGetValue(scheduledJobId, out ScheduledJobRuntimeState? state)) return; lock (state.SyncRoot) // The semaphore is only released when a slot is actually taken, which is checked under the same lock as the acquire; // without that check, releasing a slot that was never acquired, or releasing it twice, would throw a SemaphoreFullException. if (state.RunSlot.CurrentCount is 0) state.RunSlot.Release(); } }