Skip to content

Instantly share code, notes, and snippets.

@AlbertoMonteiro
Created October 25, 2016 13:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlbertoMonteiro/9a4a383b880a5810daac4257725144fb to your computer and use it in GitHub Desktop.
Save AlbertoMonteiro/9a4a383b880a5810daac4257725144fb to your computer and use it in GitHub Desktop.
IMonitorActor.cs
using System;
using System.Diagnostics;
using Akka.Actor;
namespace TestMonitoring
{
public interface IMonitorActor
{
ActorTask OnStartReceive(string actorTypeName, string actorPath, object message);
void OnFinishReceive(ActorTask actorTask);
}
public class ConsoleMonitorActor : IMonitorActor
{
public ActorTask OnStartReceive(string actorTypeName, string actorPath, object message)
{
var actorTask = new ActorTask(Guid.NewGuid().ToString("N"), Stopwatch.StartNew());
Console.WriteLine($"MonitorActor => {actorTypeName} - OnStartReceive: {message} - TaskId: {actorTask.TaskId}");
return actorTask;
}
public void OnFinishReceive(ActorTask actorTask)
{
actorTask.Stopwatch.Stop();
Console.WriteLine($"MonitorActor => OnFinishReceive: {actorTask.Stopwatch.Elapsed}");
}
}
public sealed class ActorTask
{
public ActorTask(string taskId, Stopwatch stopwatch)
{
TaskId = taskId;
Stopwatch = stopwatch;
}
public string TaskId { get; }
public Stopwatch Stopwatch { get; }
}
public abstract class MonitoredReceiveActor : ReceiveActor
{
private readonly IMonitorActor _monitorActor;
protected MonitoredReceiveActor(IMonitorActor monitorActor)
{
_monitorActor = monitorActor;
}
protected void MonitoredReceive<T>(Action<T> action)
{
Receive<T>(value =>
{
var actorTask = _monitorActor.OnStartReceive(nameof(MonitoredReceiveActor), Self.Path.ToString(), value);
action(value);
_monitorActor.OnFinishReceive(actorTask);
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment