mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 08:11:10 +00:00
Completes the scheduled jobs module with default jobs, a themed scheduler page and aligned recovery semantics
This commit is contained in:
1 parent
99a6159f7f
commit
4b7cfbbe11
67 files changed
+3324
-745
No files matched your search
@@ -16,10 +16,28 @@ public sealed class ScheduledJobRuntimeRegistry : IScheduledJobRuntimeRegistry
|
||||
/// </summary>
|
||||
private sealed class ScheduledJobRuntimeState
|
||||
{
|
||||
/// <summary>
|
||||
/// Object used to synchronize the access to the runtime state of the scheduled job.
|
||||
/// </summary>
|
||||
public readonly object SyncRoot = new();
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the cancellation token source of the running execution cycle, or <see langword="null"/> when no cycle is running.
|
||||
/// </summary>
|
||||
public CancellationTokenSource? CycleCancellationTokenSource;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the slot that guards a scheduled job against overlapping executions.
|
||||
/// </summary>
|
||||
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<ScheduledJobId, ScheduledJobRuntimeState> _runtimeStates = [];
|
||||
|
||||
/// <summary>
|
||||
@@ -31,10 +49,31 @@ public sealed class ScheduledJobRuntimeRegistry : IScheduledJobRuntimeRegistry
|
||||
public bool TryStartCycle(ScheduledJobId scheduledJobId, CancellationTokenSource cycleCancellationTokenSource)
|
||||
{
|
||||
ScheduledJobRuntimeState state = _runtimeStates.GetOrAdd(scheduledJobId, static _ => new ScheduledJobRuntimeState());
|
||||
if (state.CycleCancellationTokenSource is not null)
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the execution cycle of the scheduled job identified by <paramref name="scheduledJobId"/> is currently running.
|
||||
/// </summary>
|
||||
/// <param name="scheduledJobId">The object representing the unique identifier of the scheduled job whose cycle is checked.</param>
|
||||
/// <returns><see langword="true"/> when a cycle is currently running, <see langword="false"/> otherwise.</returns>
|
||||
public bool HasActiveCycle(ScheduledJobId scheduledJobId)
|
||||
{
|
||||
if (!_runtimeStates.TryGetValue(scheduledJobId, out ScheduledJobRuntimeState? state))
|
||||
return false;
|
||||
state.CycleCancellationTokenSource = cycleCancellationTokenSource;
|
||||
return true;
|
||||
// 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -45,11 +84,43 @@ public sealed class ScheduledJobRuntimeRegistry : IScheduledJobRuntimeRegistry
|
||||
{
|
||||
if (!_runtimeStates.TryGetValue(scheduledJobId, out ScheduledJobRuntimeState? state))
|
||||
return;
|
||||
CancellationTokenSource? cycleCancellationTokenSource = state.CycleCancellationTokenSource;
|
||||
state.CycleCancellationTokenSource = null;
|
||||
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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ends the execution cycle of the scheduled job identified by <paramref name="scheduledJobId"/>, when it was started with <paramref name="cycleCancellationTokenSource"/>.
|
||||
/// </summary>
|
||||
/// <param name="scheduledJobId">The object representing the unique identifier of the scheduled job whose cycle is ended.</param>
|
||||
/// <param name="cycleCancellationTokenSource">The cancellation token source of the cycle that is ending.</param>
|
||||
/// <remarks>
|
||||
/// 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.
|
||||
/// </remarks>
|
||||
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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to reserve the execution slot of the scheduled job identified by <paramref name="scheduledJobId"/>.
|
||||
/// </summary>
|
||||
@@ -58,7 +129,10 @@ public sealed class ScheduledJobRuntimeRegistry : IScheduledJobRuntimeRegistry
|
||||
public bool TryAcquireRunSlot(ScheduledJobId scheduledJobId)
|
||||
{
|
||||
ScheduledJobRuntimeState state = _runtimeStates.GetOrAdd(scheduledJobId, static _ => new ScheduledJobRuntimeState());
|
||||
return state.RunSlot.Wait(0);
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -67,7 +141,12 @@ public sealed class ScheduledJobRuntimeRegistry : IScheduledJobRuntimeRegistry
|
||||
/// <param name="scheduledJobId">The object representing the unique identifier of the scheduled job whose execution slot is released.</param>
|
||||
public void ReleaseRunSlot(ScheduledJobId scheduledJobId)
|
||||
{
|
||||
if (_runtimeStates.TryGetValue(scheduledJobId, out ScheduledJobRuntimeState? state))
|
||||
state.RunSlot.Release();
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user