Robert Petermeier
Robert Petermeier

Reputation: 4152

Akka: How to schedule retries on failure with growing delay intervals?

What is a good way of having an actor try something again on failure, but with growing time intervals between the retries? Let's say I want the actor to try again after 15 seconds, then 30 seconds, then every minute for a limited number of times.

Here's what I've come up with:

Is this a good solution or is there a better approach?

Upvotes: 10

Views: 5955

Answers (2)

jxstanford
jxstanford

Reputation: 3387

You could have a supervisor that starts the worker actor. The tip from the docs is to declare a router of size one for the worker. The supervisor would keep track of the number of retries, then schedule the message send to the worker as appropriate.

Even though you would be creating another layer of actors, this seems cleaner to me since you would be keeping the supervisory functionality out of the worker. Ideally you could make this 1 supervisor to n workers, but I think you would have to use Lifecycle Monitoring to get a Failure from a child actor. In that case, you could just keep a map of [ActorRef, Int] to keep track of the number of retries for all supervised workers. The supervision policy would Resume, but if you reached your max retries, you could send a PoisonPill to the offending ActorRef.

Upvotes: 8

Latrine
Latrine

Reputation: 101

In such cases I use standard supervision. A parent/supervising actor defines retries within a time window. The retrying worker child simply re-schedules the message which caused the failure with a delay in preRestart().

If the retrying child is rather complex, you might consider to interconnect an intermediate actor. That actor simply escalates supervision. On preRestart the intermediate actor schedules a (delayed) restart-message. As the intermediate actor preserved its state it can simply restart the worker actor (with delay).

As you can see the delaying part might be in preRestart or on start of the worker.

Upvotes: 9

Related Questions