Skip to content

Commit

Permalink
Implemented logging from DI
Browse files Browse the repository at this point in the history
  • Loading branch information
andresgutierrez committed Oct 16, 2023
1 parent 9baf2fe commit e8f1ba5
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 11 deletions.
1 change: 0 additions & 1 deletion Nixie.Tests/Actors/LoggingActor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,3 @@ public async Task Receive(string message)
context.Logger?.LogInformation("Message: {Message}", message);
}
}

22 changes: 22 additions & 0 deletions Nixie.Tests/Actors/LoggingArgsActor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

using Microsoft.Extensions.Logging;

namespace Nixie.Tests.Actors;

public class LoggingArgsActor : IActor<string>
{
private readonly ILogger<TestLogging> logger;

public LoggingArgsActor(IActorContext<LoggingArgsActor, string> _, ILogger<TestLogging> logger)
{
this.logger = logger;
}

public async Task Receive(string message)
{
await Task.Yield();

logger.LogInformation("Message: {Message}", message);
}
}

23 changes: 21 additions & 2 deletions Nixie.Tests/TestLogging.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

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

namespace Nixie.Tests;

Expand Down Expand Up @@ -31,11 +32,29 @@ public async Task TestAddLogging()

Assert.IsAssignableFrom<LoggingActor>(actor.Runner.Actor);

actor.Send("message");
actor.Send("TestAddLogging");

await asx.Wait();
}

[Fact]
public async Task TestAddLoggingArgs()
{
IServiceCollection services = new ServiceCollection();

services.AddSingleton(typeof(ILogger<TestLogging>), logger);

ServiceProvider serviceProvider = services.BuildServiceProvider();

using ActorSystem asx = new(serviceProvider);

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

Assert.IsAssignableFrom<LoggingArgsActor>(actor.Runner.Actor);

actor.Send("TestAddLoggingArgs");

//Assert.Equal(5, ((LoggingActor)actor.Runner.Actor!).GetMessages());
await asx.Wait();
}
}

16 changes: 13 additions & 3 deletions Nixie/ActorRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ public ActorRepository(ActorSystem actorSystem, IServiceProvider? serviceProvide
/// <summary>
/// Check if any of the actors have pending messages
/// </summary>
/// <param name="actorName"></param>
/// <returns></returns>
public bool HasPendingMessages()
{
public bool HasPendingMessages(out string? actorName)
{
foreach (KeyValuePair<string, Lazy<(ActorRunner<TActor, TRequest> runner, ActorRef<TActor, TRequest> actorRef)>> actor in actors)
{
Lazy<(ActorRunner<TActor, TRequest> runner, ActorRef<TActor, TRequest> actorRef)> lazyValue = actor.Value;
Expand All @@ -48,18 +49,23 @@ public bool HasPendingMessages()
ActorRunner<TActor, TRequest> runner = lazyValue.Value.runner;

if (!runner.IsShutdown && !lazyValue.Value.runner.Inbox.IsEmpty)
{
actorName = runner.Name;
return true;
}
}
}

actorName = null;
return false;
}

/// <summary>
/// Check if any of the actors are processing messages
/// </summary>
/// <param name="actorName"></param>
/// <returns></returns>
public bool IsProcessing()
public bool IsProcessing(out string? actorName)
{
foreach (KeyValuePair<string, Lazy<(ActorRunner<TActor, TRequest> runner, ActorRef<TActor, TRequest> actorRef)>> actor in actors)
{
Expand All @@ -70,10 +76,14 @@ public bool IsProcessing()
ActorRunner<TActor, TRequest> runner = lazyValue.Value.runner;

if (!runner.IsShutdown && runner.IsProcessing)
{
actorName = runner.Name;
return true;
}
}
}

actorName = null;
return false;
}

Expand Down
12 changes: 10 additions & 2 deletions Nixie/ActorRepositoryReply.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public ActorRepository(ActorSystem actorSystem, IServiceProvider? serviceProvide
/// Check if any of the actors have pending messages
/// </summary>
/// <returns></returns>
public bool HasPendingMessages()
public bool HasPendingMessages(out string? actorName)
{
foreach (KeyValuePair<string, Lazy<(ActorRunner<TActor, TRequest, TResponse> runner, ActorRef<TActor, TRequest, TResponse> actorRef)>> actor in actors)
{
Expand All @@ -49,18 +49,22 @@ public bool HasPendingMessages()
ActorRunner<TActor, TRequest, TResponse> runner = lazyValue.Value.runner;

if (!runner.IsShutdown && !lazyValue.Value.runner.Inbox.IsEmpty)
{
actorName = runner.Name;
return true;
}
}
}

actorName = null;
return false;
}

/// <summary>
/// Check if any of the actors are processing messages
/// </summary>
/// <returns></returns>
public bool IsProcessing()
public bool IsProcessing(out string? actorName)
{
foreach (KeyValuePair<string, Lazy<(ActorRunner<TActor, TRequest, TResponse> runner, ActorRef<TActor, TRequest, TResponse> actorRef)>> actor in actors)
{
Expand All @@ -71,10 +75,14 @@ public bool IsProcessing()
ActorRunner<TActor, TRequest, TResponse> runner = lazyValue.Value.runner;

if (!runner.IsShutdown && runner.IsProcessing)
{
actorName = runner.Name;
return true;
}
}
}

actorName = null;
return false;
}

Expand Down
12 changes: 11 additions & 1 deletion Nixie/ActorSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Extensions.Logging;
using Nixie.Actors;
using System.Collections.Concurrent;
using System.Diagnostics;

namespace Nixie;

Expand Down Expand Up @@ -330,6 +331,9 @@ public void StopAllTimers<TActor, TRequest>(IActorRef<TActor, TRequest> actorRef
/// <returns></returns>
public async Task Wait()
{
Stopwatch stopWatch = new();
string? pendingActorName = null, processingName = null;

while (true)
{
bool completed = true;
Expand All @@ -341,7 +345,7 @@ public async Task Wait()
if (!lazyRepository.IsValueCreated)
continue;

if (lazyRepository.Value.HasPendingMessages() || lazyRepository.Value.IsProcessing())
if (lazyRepository.Value.HasPendingMessages(out pendingActorName) || lazyRepository.Value.IsProcessing(out processingName))
{
await Task.Yield();
completed = false;
Expand All @@ -351,6 +355,12 @@ public async Task Wait()

if (completed)
break;

if (stopWatch.ElapsedMilliseconds > 10000)
{
logger?.LogWarning("Timeout waiting for actor {PendingActorName}/{ProcessingName}", pendingActorName, processingName);
break;
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions Nixie/IActorRepositoryRunnable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ public interface IActorRepositoryRunnable
/// Returns true if there are pending messages to process.
/// </summary>
/// <returns></returns>
public bool HasPendingMessages();
public bool HasPendingMessages(out string? actorName);

/// <summary>
/// Returns true if the repository is processing messages.
/// </summary>
/// <returns></returns>
public bool IsProcessing();
public bool IsProcessing(out string? actorName);
}

0 comments on commit e8f1ba5

Please sign in to comment.