Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Akka scheduler stops on exception; is it expected?

Tags:

scala

akka

We run this code:

scheduler.schedule(1 minute, 1 minute) { triggerOperations.tick() }

when starting our application, where scheduler is an Akka actorSystem.scheduler. If tick() throws an exception, then it is never called again!

I checked the documentation, but cannot find any statement that this is expected. Mostly the description is "Schedules a function to be run repeatedly with an initial delay and a frequency", with no mention that if the function throws an excpetion the task will stop firing.

Our akka version is 2.3.2.

http://doc.akka.io/docs/akka/2.3.4/scala/scheduler.html http://doc.akka.io/api/akka/2.0/akka/actor/Scheduler.html

Is this behavior expected? Is it documented anywhere?

like image 830
Jonathan Crosmer Avatar asked Aug 27 '15 17:08

Jonathan Crosmer


1 Answers

When in doubt, go to source. The code is a bit terse, but this fragment:

 override def run(): Unit = {
   try {
     runnable.run()
     val driftNanos = clock() - getAndAdd(delay.toNanos)
     if (self.get != null)
       swap(schedule(executor, this, Duration.fromNanos(Math.max(delay.toNanos - driftNanos, 1))))
   } catch {
      case _: SchedulerException ⇒ // ignore failure to enqueue or terminated target actor
   }

}

shows that if your runnable throws, scheduler does not reschedule the next execution (which happens inside swap as far as I understand).

like image 85
Tim Avatar answered Oct 21 '22 23:10

Tim