[Akka 2.0] Limited actor lifetime

163 views
Skip to first unread message

Alex

unread,
May 10, 2012, 10:44:42 AM5/10/12
to akka...@googlegroups.com
Hi,

I have some worker actors, which do some work. When they finish their work they should stop. No problem so far. But I have an additional requirement. The worker should also stop if it hasn't finish the work after defined duration.

My current approach is to stop the worker in 2 places:

def preStart() {
  context.system.scheduler.scheduleOnce(...) { context.stop(self) }
}

def receive = {
  case ... =>
    ... // do something
    context.stop(self)
}

I get an "[TaskInvocation] null" error. I suppose if a worker finishes its work early enough, self becomes null and the scheduled function fails.

Is the approach ok? How to avoid the described error?

Alex

Derek Wyatt

unread,
May 10, 2012, 10:49:27 AM5/10/12
to akka...@googlegroups.com
I suspect that closing over context the way you're doing in the scheduler is just plain wrong.

Can't you break the algorithm up?  For example:

    case class Message(workToDo, workAlreadyDone, timestamp)

Then send that message to yourself over and over.  The workToDo is a list of things to do, and the workAlreadyDone is a list of results.  One shrinks as the other grows.  If the timestamp indicates that you've taken "too long" then die.

If your actor is busy, then it's busy.  You can't stop it the way you want to stop it.  It needs to be responsive in order to be stoppable.

--
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To view this discussion on the web visit https://groups.google.com/d/msg/akka-user/-/hVdDFyLBrv0J.
To post to this group, send email to akka...@googlegroups.com.
To unsubscribe from this group, send email to akka-user+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/akka-user?hl=en.

signature.asc

√iktor Ҡlang

unread,
May 10, 2012, 11:06:52 AM5/10/12
to akka...@googlegroups.com
Hi Alex,

Just as Derek says, you're closing over context and publish it to another thread. This is not allowed.

So you can either:

system stop self

or

self ! PoisonPill

Cheers,
 

Alex

--
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To view this discussion on the web visit https://groups.google.com/d/msg/akka-user/-/hVdDFyLBrv0J.
To post to this group, send email to akka...@googlegroups.com.
To unsubscribe from this group, send email to akka-user+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/akka-user?hl=en.



--
Viktor Klang

Akka Tech Lead
Typesafe - The software stack for applications that scale

Twitter: @viktorklang

√iktor Ҡlang

unread,
May 10, 2012, 11:11:48 AM5/10/12
to akka...@googlegroups.com
Added a clarification in the ScalaDoc:


Cheers,

Roland Kuhn

unread,
May 10, 2012, 11:12:11 AM5/10/12
to akka...@googlegroups.com
On May 10, 2012, at 17:06 , √iktor Ҡlang wrote:

Hi Alex,

On Thu, May 10, 2012 at 4:44 PM, Alex <bertra...@gmail.com> wrote:
Hi,

I have some worker actors, which do some work. When they finish their work they should stop. No problem so far. But I have an additional requirement. The worker should also stop if it hasn't finish the work after defined duration.

My current approach is to stop the worker in 2 places:

def preStart() {
  context.system.scheduler.scheduleOnce(...) { context.stop(self) }
}

def receive = {
  case ... =>
    ... // do something
    context.stop(self)
}

I get an "[TaskInvocation] null" error. I suppose if a worker finishes its work early enough, self becomes null and the scheduled function fails.

Is the approach ok? How to avoid the described error?

Just as Derek says, you're closing over context and publish it to another thread. This is not allowed.

So you can either:

system stop self

or

self ! PoisonPill


But as Derek—spot on—pointed out, this won’t solve the problem of trying to stop the actor while it keeps processing its message. It will get rid of the exception, though ;-)

Regards,

Roland

Cheers,
 

Alex

--
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To view this discussion on the web visit https://groups.google.com/d/msg/akka-user/-/hVdDFyLBrv0J.
To post to this group, send email to akka...@googlegroups.com.
To unsubscribe from this group, send email to akka-user+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/akka-user?hl=en.



--
Viktor Klang

Akka Tech Lead
Typesafe - The software stack for applications that scale

Twitter: @viktorklang


--
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To post to this group, send email to akka...@googlegroups.com.
To unsubscribe from this group, send email to akka-user+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/akka-user?hl=en.

Roland Kuhn
Typesafe – The software stack for applications that scale.
twitter: @rolandkuhn


√iktor Ҡlang

unread,
May 10, 2012, 11:19:28 AM5/10/12
to akka...@googlegroups.com
On Thu, May 10, 2012 at 5:12 PM, Roland Kuhn <goo...@rkuhn.info> wrote:

On May 10, 2012, at 17:06 , √iktor Ҡlang wrote:

Hi Alex,

On Thu, May 10, 2012 at 4:44 PM, Alex <bertra...@gmail.com> wrote:
Hi,

I have some worker actors, which do some work. When they finish their work they should stop. No problem so far. But I have an additional requirement. The worker should also stop if it hasn't finish the work after defined duration.

My current approach is to stop the worker in 2 places:

def preStart() {
  context.system.scheduler.scheduleOnce(...) { context.stop(self) }
}

def receive = {
  case ... =>
    ... // do something
    context.stop(self)
}

I get an "[TaskInvocation] null" error. I suppose if a worker finishes its work early enough, self becomes null and the scheduled function fails.

Is the approach ok? How to avoid the described error?

Just as Derek says, you're closing over context and publish it to another thread. This is not allowed.

So you can either:

system stop self

or

self ! PoisonPill


But as Derek—spot on—pointed out, this won’t solve the problem of trying to stop the actor while it keeps processing its message. It will get rid of the exception, though ;-)

Well of course the actor is implemented in a way so it sends itself continuation-messages as not to hog its thread, right? I thought that was already the case ;-)

Alex

unread,
May 10, 2012, 2:31:52 PM5/10/12
to akka...@googlegroups.com
Thank you, guys!

Alex
To unsubscribe from this group, send email to akka-user+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages