Skip to content

Commit

Permalink
Debug scheduler issues
Browse files Browse the repository at this point in the history
  • Loading branch information
andresgutierrez committed Oct 16, 2023
1 parent d3efff8 commit 13f56ed
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
34 changes: 31 additions & 3 deletions Nixie.Tests/TestScheduler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

using Microsoft.Extensions.Logging;
using Nixie.Tests.Actors;

namespace Nixie.Tests;
Expand All @@ -9,7 +10,16 @@ public sealed class TestScheduler
[Fact]
public async void TestCreatePeriodicTimer()
{
using ActorSystem asx = new();
using ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
{
builder
.AddFilter("Nixie", LogLevel.Debug)
.AddConsole();
});

ILogger logger = loggerFactory.CreateLogger<TestSendMessages>();

using ActorSystem asx = new(logger: logger);

IActorRef<PeriodicTimerActor, string> actor = asx.Spawn<PeriodicTimerActor, string>();

Expand All @@ -25,7 +35,16 @@ public async void TestCreatePeriodicTimer()
[Fact]
public async void TestCreatePeriodicTimerExternalStop()
{
using ActorSystem asx = new();
using ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
{
builder
.AddFilter("Nixie", LogLevel.Debug)
.AddConsole();
});

ILogger logger = loggerFactory.CreateLogger<TestSendMessages>();

using ActorSystem asx = new(logger: logger);

IActorRef<PeriodicTimerActor, string> actor = asx.Spawn<PeriodicTimerActor, string>();

Expand All @@ -49,7 +68,16 @@ public async void TestCreatePeriodicTimerExternalStop()
[Fact]
public void TestCreatePeriodicTimerExternalStopTwice()
{
using ActorSystem asx = new();
using ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
{
builder
.AddFilter("Nixie", LogLevel.Debug)
.AddConsole();
});

ILogger logger = loggerFactory.CreateLogger<TestSendMessages>();

using ActorSystem asx = new(logger: logger);

IActorRef<PeriodicTimerActor, string> actor = asx.Spawn<PeriodicTimerActor, string>();

Expand Down
26 changes: 24 additions & 2 deletions Nixie/ActorScheduler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

using System.Collections.Concurrent;
using Microsoft.Extensions.Logging;

namespace Nixie;

Expand All @@ -8,10 +9,17 @@ namespace Nixie;
/// </summary>
public class ActorScheduler : IDisposable
{
private readonly ILogger? logger;

private readonly ConcurrentDictionary<object, Lazy<ConcurrentBag<Timer>>> onceTimers = new();

private readonly ConcurrentDictionary<object, Lazy<ConcurrentDictionary<string, Lazy<Timer>>>> periodicTimers = new();

public ActorScheduler(ILogger? logger)
{
this.logger = logger;
}

/// <summary>
/// Schedules a message to be sent to an actor once after a specified delay at a specified interval.
/// </summary>
Expand Down Expand Up @@ -184,13 +192,27 @@ private Timer ScheduleOnceTimer<TActor, TRequest, TResponse>(IActorRef<TActor, T
private void SendScheduledMessage<TActor, TRequest>(IActorRef<TActor, TRequest> actorRef, TRequest request)
where TActor : IActor<TRequest> where TRequest : class
{
actorRef.Send(request);
try
{
actorRef.Send(request);
}
catch (Exception ex)
{
logger?.LogError("{Ex}", ex.Message);
}
}

private void SendScheduledMessage<TActor, TRequest, TResponse>(IActorRef<TActor, TRequest, TResponse> actorRef, TRequest request)
where TActor : IActor<TRequest, TResponse> where TRequest : class where TResponse : class
{
actorRef.Send(request);
try
{
actorRef.Send(request);
}
catch (Exception ex)
{
logger?.LogError("{Ex}", ex.Message);
}
}

private void StopAllTimersInternal(object actorRef)
Expand Down
3 changes: 2 additions & 1 deletion Nixie/ActorSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Nixie;
/// </summary>
public sealed class ActorSystem : IDisposable
{
private readonly ActorScheduler scheduler = new();
private readonly ActorScheduler scheduler;

private readonly IServiceProvider? serviceProvider;

Expand Down Expand Up @@ -40,6 +40,7 @@ public ActorSystem(IServiceProvider? serviceProvider = null, ILogger? logger = n
{
this.serviceProvider = serviceProvider;
this.logger = logger;
this.scheduler = new(logger);

nobody = Spawn<NobodyActor, object>();
}
Expand Down

0 comments on commit 13f56ed

Please sign in to comment.