Skip to content

Instantly share code, notes, and snippets.

@asik
Last active January 11, 2022 15:22
Show Gist options
  • Save asik/68d8a25d23304eee71c11256388671da to your computer and use it in GitHub Desktop.
Save asik/68d8a25d23304eee71c11256388671da to your computer and use it in GitHub Desktop.
Akka async continuations
using Akka.Actor;
using Akka.Dispatch;
using System.Diagnostics;
var actorSystem = ActorSystem.Create("ActorSystem");
actorSystem.ActorOf(Props.Create(() => new SupervisorActor()));
Console.ReadKey();
class SupervisorActor : ReceiveActor
{
private IActorRef _childActor;
public SupervisorActor()
{
_childActor = Context.ActorOf(Props.Create(() => new ChildActor()));
Context.Watch(_childActor);
Receive<Terminated>(
c => c.ActorRef == _childActor, t => {
Debug.WriteLine("Child actor terminated");
});
Task.Run(async () =>
{
await Task.Delay(2000);
_childActor.Tell(PoisonPill.Instance);
//await _childActor.GracefulStop(TimeSpan.FromSeconds(10));
Debug.WriteLine("GracefulStop returned");
});
}
}
class ChildActor: ReceiveActor
{
protected override void PostStop()
{
async Task DisposeThing()
{
Debug.WriteLine("Before Delay");
await Task.Delay(1); // This would be the actual call to dispose the thing
Debug.WriteLine("After Delay");
};
ActorTaskScheduler.RunTask(async () =>
{
try
{
Debug.WriteLine("Before Wait");
await DisposeThing();
Debug.WriteLine("After Wait");
}
catch (Exception ex)
{
Debug.WriteLine($"An exception occured: {ex}");
}
finally
{
Debug.WriteLine("actor done disposing.");
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment