From 2869a2fea1234ec62d766a259c36612a910f1e46 Mon Sep 17 00:00:00 2001 From: Andres Gutierrez Date: Sat, 28 Sep 2024 13:09:59 -0500 Subject: [PATCH] Stop all timers when shutting down actors --- .github/workflows/run-tests.yml | 2 +- Nixie/ActorRepository.cs | 3 ++- Nixie/ActorRepositoryReply.cs | 2 +- Nixie/ActorRepositoryStruct.cs | 4 ++-- Nixie/ActorRepositoryStructReply.cs | 4 +++- Nixie/ActorScheduler.cs | 3 +-- Nixie/ActorSystem.cs | 9 +++------ README.md | 6 +++--- 8 files changed, 16 insertions(+), 17 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 7682e46..3fd3f48 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -25,7 +25,7 @@ jobs: - name: Run Tests run: (cd Nixie.Tests && dotnet dotcover test --dcOutput=NixieCoverageReport.html --dcReportType=HTML) - name: Archive code coverage results - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: code-coverage-report path: | diff --git a/Nixie/ActorRepository.cs b/Nixie/ActorRepository.cs index 912f449..e0f4fd4 100644 --- a/Nixie/ActorRepository.cs +++ b/Nixie/ActorRepository.cs @@ -232,6 +232,7 @@ public async Task GracefulShutdown(string name, TimeSpan maxWait) if (actors.TryGetValue(name, out Lazy<(ActorRunner runner, ActorRef actorRef)>? actor)) { bool result = await actor.Value.runner.GracefulShutdown(maxWait); + actorSystem.StopAllTimers(actor.Value.actorRef); actors.TryRemove(name, out _); return result; } @@ -252,8 +253,8 @@ public async Task GracefulShutdown(IActorRef actorRef, T if (actors.TryGetValue(name, out Lazy<(ActorRunner runner, ActorRef actorRef)>? actor)) { bool success = await actor.Value.runner.GracefulShutdown(maxWait); - actors.TryRemove(name, out _); actorSystem.StopAllTimers(actor.Value.actorRef); + actors.TryRemove(name, out _); return success; } diff --git a/Nixie/ActorRepositoryReply.cs b/Nixie/ActorRepositoryReply.cs index 4319296..fdb7859 100644 --- a/Nixie/ActorRepositoryReply.cs +++ b/Nixie/ActorRepositoryReply.cs @@ -188,8 +188,8 @@ public bool Shutdown(string name) { if (actor.Value.runner.Shutdown()) { - actors.TryRemove(name, out _); actorSystem.StopAllTimers(actor.Value.actorRef); + actors.TryRemove(name, out _); return true; } } diff --git a/Nixie/ActorRepositoryStruct.cs b/Nixie/ActorRepositoryStruct.cs index f3a9d37..589bb52 100644 --- a/Nixie/ActorRepositoryStruct.cs +++ b/Nixie/ActorRepositoryStruct.cs @@ -75,7 +75,7 @@ public bool IsProcessing(out string? actorName) { ActorRunnerStruct runner = lazyValue.Value.runner; - if (!runner.IsShutdown && runner.IsProcessing) + if (runner is { IsShutdown: false, IsProcessing: true }) { actorName = runner.Name; return true; @@ -110,7 +110,7 @@ public IActorRefStruct Spawn(string? name = null, params objec Lazy<(ActorRunnerStruct runner, ActorRefStruct actorRef)> actor = actors.GetOrAdd( name, - (string name) => new Lazy<(ActorRunnerStruct, ActorRefStruct)>(() => CreateInternal(name, args)) + (string name) => new(() => CreateInternal(name, args)) ); return actor.Value.actorRef; diff --git a/Nixie/ActorRepositoryStructReply.cs b/Nixie/ActorRepositoryStructReply.cs index 39bbf97..2e64fab 100644 --- a/Nixie/ActorRepositoryStructReply.cs +++ b/Nixie/ActorRepositoryStructReply.cs @@ -110,7 +110,7 @@ public IActorRefStruct Spawn(string? name = null, p Lazy<(ActorRunnerStruct runner, ActorRefStruct actorRef)> actor = actors.GetOrAdd( name, - (string name) => new Lazy<(ActorRunnerStruct, ActorRefStruct)>(() => CreateInternal(name, args)) + (string name) => new(() => CreateInternal(name, args)) ); return actor.Value.actorRef; @@ -188,6 +188,7 @@ public bool Shutdown(string name) { if (actor.Value.runner.Shutdown()) { + actorSystem.StopAllTimers(actor.Value.actorRef); actors.TryRemove(name, out _); return true; } @@ -209,6 +210,7 @@ public bool Shutdown(IActorRefStruct actorRef) { if (actor.Value.runner.Shutdown()) { + actorSystem.StopAllTimers(actor.Value.actorRef); actors.TryRemove(name, out _); return true; } diff --git a/Nixie/ActorScheduler.cs b/Nixie/ActorScheduler.cs index 4a980af..9c13763 100644 --- a/Nixie/ActorScheduler.cs +++ b/Nixie/ActorScheduler.cs @@ -224,8 +224,7 @@ public Timer ScheduleShutdown(IActorRef acto /// /// /// - public void StopPeriodicTimer(IActorRef actorRef, string name) - where TActor : IActor where TRequest : class + public void StopPeriodicTimer(IActorRef actorRef, string name) where TActor : IActor where TRequest : class { if (periodicTimers.TryGetValue(actorRef, out Lazy>>? timers)) { diff --git a/Nixie/ActorSystem.cs b/Nixie/ActorSystem.cs index 78db0a7..1956430 100644 --- a/Nixie/ActorSystem.cs +++ b/Nixie/ActorSystem.cs @@ -652,8 +652,7 @@ public void ScheduleShutdown(IActorRef actor /// /// /// - public void StopPeriodicTimer(IActorRef actorRef, string name) - where TActor : IActor where TRequest : class + public void StopPeriodicTimer(IActorRef actorRef, string name) where TActor : IActor where TRequest : class { scheduler.StopPeriodicTimer(actorRef, name); } @@ -663,8 +662,7 @@ public void StopPeriodicTimer(IActorRef acto /// /// /// - public void StopPeriodicTimer(IActorRef actorRef, string name) - where TActor : IActor where TRequest : class where TResponse : class? + public void StopPeriodicTimer(IActorRef actorRef, string name) where TActor : IActor where TRequest : class where TResponse : class? { scheduler.StopPeriodicTimer(actorRef, name); } @@ -676,8 +674,7 @@ public void StopPeriodicTimer(IActorRef /// /// - public void StopAllTimers(IActorRef actorRef) - where TActor : IActor where TRequest : class where TResponse : class? + public void StopAllTimers(IActorRef actorRef) where TActor : IActor where TRequest : class where TResponse : class? { scheduler.StopAllTimers(actorRef); } diff --git a/README.md b/README.md index ce65ba3..1e4f073 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ Nixie is a lightweight, high-performance implementation of the [actor model](htt ### Prerequisites -- .NET SDK 6.0 or later +- .NET SDK 8.0 or later - A suitable IDE (e.g., Visual Studio, Visual Studio Code, or Rider) ### Installation @@ -48,7 +48,7 @@ To install Nixie into your C#/.NET project, you can use the .NET CLI or the NuGe #### Using .NET CLI ```shell -dotnet add package Nixie --version 1.0.8 +dotnet add package Nixie --version 1.0.9 ``` ### Using NuGet Package Manager @@ -56,7 +56,7 @@ dotnet add package Nixie --version 1.0.8 Search for Nixie and install it from the NuGet package manager UI, or use the Package Manager Console: ```shell -Install-Package Nixie -Version 1.0.8 +Install-Package Nixie -Version 1.0.9 ``` ## Usage