Search code examples
scalaakkaakka-supervision

Schedule restart of Akka actor


Is anyone aware of a way to create a SupervisorStrategy that executes a Restart of an Actor after a delay? To provide some context I have an actor that queries a DB on startup. If the DB connection is down the Actor will spin until it hits the max retried. It would be nice to delay the restart.

A brute force but unacceptable solution is to do something like the following:

override val supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 minute)    
  {
    case initEx: ActorInitializationException => {
      Thread.sleep(1000)
      Restart
    }
    case t =>
      super.supervisorStrategy.decider.applyOrElse(t, (_: Any) => Escalate)
  }

However, this seems untenable to me as I want to avoid any blocking code.

Is this just not a concept that should be supported by actors? Should the delay or retry be moved into the Actor implementation itself? This seems counter to the idea of 'let it crash'.


Solution

  • Delay on restart hasn't been implemented yet. Thread.sleep is out of the question performance wise.

    I see two choices :

    • Have your main actor create the querying actor on a message. When it blows up because of a database outage, you can just stop the actor and re-schedule a message to recreate it. Might be a problem if database operations must occur before anything else.
    • Move the fault tolerance logic inside the querying actor. Have your database logic inside a partial function and use a circuit breaker to handle retries. You can then use supervision to Restart the actor on needed exceptions and through preRestart hook re-schedule the message instructing to do database operations. If you don't want to restart, just surround your circuit breaker with a try-catch to re-schedule the message here.