Created
October 25, 2016 13:06
-
-
Save AlbertoMonteiro/9a4a383b880a5810daac4257725144fb to your computer and use it in GitHub Desktop.
IMonitorActor.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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