From a6d17160445c1d770ec45c2a6eab382cfb9d0592 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=99=E8=85=BE=E7=8C=AB=E8=B7=83?= <1043137532@qq.com> Date: Fri, 26 Jun 2026 05:41:58 +0800 Subject: [PATCH] RedoableWorker --- Shared/General/Worker.cs | 136 ++++++++++++++++++++++++++++++++++++ Tests/General/WorkerTest.cs | 52 ++++++++++++++ 2 files changed, 188 insertions(+) create mode 100644 Shared/General/Worker.cs create mode 100644 Tests/General/WorkerTest.cs diff --git a/Shared/General/Worker.cs b/Shared/General/Worker.cs new file mode 100644 index 0000000..86bef24 --- /dev/null +++ b/Shared/General/Worker.cs @@ -0,0 +1,136 @@ +namespace MeloongCore; + +/// +/// 可合并和重启的工作器。 +/// 在工作负载运行期间多次调用 会取消当前负载并使用最新令牌重新执行;多次调用也只重新执行一次。 +/// 若即将重启,则忽略当前负载的异常。 +/// +public interface IRedoableWorker { + /// 当前是否正在运行。 + bool Running { get; } + /// 是否曾经有过未被取消且未失败的运行。 + bool HasSucceeded { get; } + /// 标识上次进入空闲时,并非因为取消或失败,而是正常运行到结束。 + bool LastSucceeded { get; } + /// 在工作线程运行工作负载。若当前已在运行,则取消当前负载并使用最新令牌重启。 + void Run(CancellationToken cancellationToken = default); + /// 取消当前运行。 + void Cancel(); + /// 仅在当前处于运行状态时,等待其完成。 + /// 若未超时则返回 true。 + bool WaitIfRunning(int millisecondsTimeout = -1); +} + +/// +public abstract class RedoableWorkerBase(Func workload) : IRedoableWorker { + + // ============================================ 状态 ============================================ + + /// + public bool Running { get { lock (this) return running; } } + private bool running; + + /// + public bool HasSucceeded { get { lock (this) return hasSucceeded; } } + private bool hasSucceeded; + + /// + public bool LastSucceeded { get { lock (this) return lastSucceeded; } } + private bool lastSucceeded; + + /// 上次未被取消且未失败的运行中,工作负载的返回值。 + public TOut LastResult { + get { + lock (this) { + if (!hasSucceeded) throw new InvalidOperationException("从未成功完成过。"); + return lastResult == null ? default! : (TOut) lastResult; + } + } + } + private object? lastResult; + + private bool pendingRedo; + + // =========================================== 运行与取消 =========================================== + + private CancellationTokenSource? realCts; + private CancellationToken lastToken; + private readonly ManualResetEventSlim idleEvent = new(initialState: true); + + /// + public void Run(CancellationToken cancellationToken = default) { + // 接取运行状态 + lock (this) { + lastToken = cancellationToken; + if (running) { + pendingRedo = true; + realCts?.Cancel(); + return; + } + running = true; idleEvent.Reset(); + realCts = CancellationTokenSource.CreateLinkedTokenSource(lastToken); + } + var th = new Thread(_Run); + th.Start(); + } + private void _Run() { + try { + TOut result = workload(realCts!.Token); // 实际的执行 + realCts.Token.ThrowIfCancellationRequested(); + lock (this) { + realCts?.Dispose(); + if (pendingRedo) { // 接取重启请求 + pendingRedo = false; + realCts = CancellationTokenSource.CreateLinkedTokenSource(lastToken); + _Run(); return; + } else { + realCts = null; + } + running = false; idleEvent.Set(); + lastSucceeded = true; + lastResult = result; + hasSucceeded = true; + } + } catch (Exception ex) { + lock (this) { + realCts?.Dispose(); + if (pendingRedo) { // 接取重启请求 + pendingRedo = false; + realCts = CancellationTokenSource.CreateLinkedTokenSource(lastToken); + if (!ex.IsCanceled()) Logger.Info(ex, "在重启前出现了异常"); + _Run(); return; + } else { + realCts = null; + } + running = false; idleEvent.Set(); + lastSucceeded = false; + } + if (!ex.IsCanceled()) Logger.Error(ex, "工作线程执行失败", LogBehavior.None); + } + } + + /// + public void Cancel() { + lock (this) { + pendingRedo = false; + realCts?.Cancel(); + } + } + + /// + public bool WaitIfRunning(int millisecondsTimeout = -1) + => idleEvent.Wait(millisecondsTimeout); + +} + +/// +public sealed class RedoableWorker : RedoableWorkerBase { + public RedoableWorker(Func workload) : base(workload) { } + public RedoableWorker(Func workload) : base(_ => workload()) { } +} + +/// +public sealed class RedoableWorker : RedoableWorkerBase { + public RedoableWorker(Action workload) : base(ct => { workload(ct); return null; }) { } + public RedoableWorker(Action workload) : base(_ => { workload(); return null; }) { } +} diff --git a/Tests/General/WorkerTest.cs b/Tests/General/WorkerTest.cs new file mode 100644 index 0000000..af62435 --- /dev/null +++ b/Tests/General/WorkerTest.cs @@ -0,0 +1,52 @@ +namespace MeloongCore.Tests; + +public class WorkerTest : TestBase { + + [Test] + public async Task Run_在工作线程执行工作负载() { + int callerThreadId = Thread.CurrentThread.ManagedThreadId; + int workloadThreadId = callerThreadId; + using var started = new ManualResetEventSlim(false); + var worker = new RedoableWorker(() => { + workloadThreadId = Thread.CurrentThread.ManagedThreadId; + started.Set(); + }); + + worker.Run(); + + await Assert.That(started.Wait(3000)).IsTrue(); + await Assert.That(worker.WaitIfRunning(3000)).IsTrue(); + await Assert.That(workloadThreadId != callerThreadId).IsTrue(); + await Assert.That(worker.LastSucceeded).IsTrue(); + } + + [Test] + public async Task Run_运行中再次调用只重跑一次() { + int callerThreadId = Thread.CurrentThread.ManagedThreadId; + int runCount = 0; + var workloadThreadIds = new ConcurrentQueue(); + using var firstStarted = new ManualResetEventSlim(false); + var worker = new RedoableWorker(ct => { + int currentRun = Interlocked.Increment(ref runCount); + workloadThreadIds.Enqueue(Thread.CurrentThread.ManagedThreadId); + if (currentRun == 1) { + firstStarted.Set(); + while (true) { + ct.ThrowIfCancellationRequested(); + Thread.Sleep(10); + } + } + }); + + worker.Run(); + await Assert.That(firstStarted.Wait(3000)).IsTrue(); + worker.Run(); + worker.Run(); + + await Assert.That(worker.WaitIfRunning(3000)).IsTrue(); + await Assert.That(runCount).IsEqualTo(2); + await Assert.That(workloadThreadIds.All(id => id != callerThreadId)).IsTrue(); + await Assert.That(worker.LastSucceeded).IsTrue(); + } + +}