Skip to content

Enhancement: create ActorTaskScheduler overload that schedules Task to dispatcher without blocking current actor #4363

Open
@Aaronontheweb

Description

Version: 1.4.3 and later

An advanced feature - right now we support the ability to schedule a Task onto the current actor's dispatcher using the ActorTaskScheduler:

public static void RunTask(Func<Task> asyncAction)
{
var context = ActorCell.Current;
if (context == null)
throw new InvalidOperationException("RunTask must be called from an actor context.");
var dispatcher = context.Dispatcher;
//suspend the mailbox
dispatcher.Suspend(context);
ActorTaskScheduler actorScheduler = context.TaskScheduler;
actorScheduler.CurrentMessage = context.CurrentMessage;
Task<Task>.Factory.StartNew(asyncAction, CancellationToken.None, TaskCreationOptions.None, actorScheduler)
.Unwrap()
.ContinueWith(parent =>
{
Exception exception = GetTaskException(parent);
if (exception == null)
{
dispatcher.Resume(context);
context.CheckReceiveTimeout();
}
else
{
context.Self.AsInstanceOf<IInternalActorRef>().SendSystemMessage(new ActorTaskSchedulerMessage(exception, actorScheduler.CurrentMessage));
}
//clear the current message field of the scheduler
actorScheduler.CurrentMessage = null;
}, actorScheduler);
}

One of the side effects of this method is that it blocks the current actor in order to run that Task.

I think it'd be worthwhile to add an overload that allows a task to be scheduled onto the current dispatcher, without impacting the current actor's ability to process its mailbox. That could make for some interesting scenarios in areas like IOT, where users often need to run actors on pinned or dedicated thread dispatchers.

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions