Supervision
An actor that fails stops taking messages, and its parent decides what becomes of it. Failures are values: a
Fault with a kind and a detail. A supervisor’s decider maps a fault’s kind to a directive, and its
strategy says which children the directive touches.
How an actor fails
A method fails by returning failWith kind detail state:
Crash r -> failWith "Glitch" "the worker tripped" w
The asker of that message gets Left (Fault "Glitch" "the worker tripped"). The actor’s parent is told, and its
strategy decides.
A refusal (refuse r fault) is not a failure: the actor answered, and goes on.
Directives
| Directive | What happens to the child |
|---|---|
Restart |
It starts over: a fresh state from its initial, its first methods. Messages waiting in its mailbox stay. |
Resume |
It keeps its state and goes on with the next message. |
Stop |
It stops (its own children first). |
Escalate |
The supervisor fails with the same fault, and its own parent decides. |
Propagate |
It goes on, as with Resume; the failure only reached the asker. |
The decider
A decider has a directive per failure kind and a default. The first rule for a kind wins:
import Actor.Supervision
decider Restart -- every failure restarts
onKind "Glitch" Resume (decider Restart) -- a Glitch resumes; anything else restarts
byDefault Stop (onKind "Timeout" Resume (decider Restart))
OneForOne and AllForOne
oneForOne :: Decider -> Strategy -- the directive touches the failed child alone
allForOne :: Decider -> Strategy -- the directive touches every child of the supervisor
Use allForOne when children only make sense together (a reader and a writer over one connection): one failing
restarts them all.
Declaring a strategy
A class sets how it supervises its children with @supervision:
-- A Glitch is resumed (the worker keeps its count); any other failure restarts it.
foremanStrategy :: Strategy
foremanStrategy = oneForOne (onKind "Glitch" Resume (decider Restart))
data Foreman = Foreman { hired :: Int }
data ForemanMsg = Hire (Reply (Address Worker))
@supervision(foremanStrategy)
instance Actor Foreman where
type Message Foreman = ForemanMsg
receive ctx f msg = case msg of
Hire r -> respond r (childActorOf ctx (parameters [])) >> continue f { hired = f.hired + 1 }
The worker it hires:
data Worker = Worker { done :: Int }
data WorkerMsg = Work (Reply Int) | Crash (Reply Int)
instance Actor Worker where
type Message Worker = WorkerMsg
receive ctx w msg = case msg of
Work r -> respond r (w.done + 1) >> continue w { done = w.done + 1 }
Crash r -> failWith "Glitch" "the worker tripped" w
Asking the worker Work, then Crash, then Work answers Right 1, Left (Fault "Glitch" ...), Right 2: the
glitch was resumed, so the worker kept its count. With decider Restart the last answer would be Right 1.
The default strategy
Without @supervision, an actor restarts each failing child on its own (oneForOne (decider Restart)). Actors
made with actorOf are children of the /user guardian, which restarts them the same way.
Supervision and asks
A failing child waits for its parent’s decision. If the parent is itself waiting on that child (it asked the child
from inside receive), neither moves. Let the parent hand out its children’s addresses, or tell them work and let
them answer with a message.
Ephemeral actors
An @ephemeral actor handles each message in a fiber of its own, from its first state. A message that fails tells
its asker; the actor itself never stops, so there is nothing to restart. See Ephemeral Actors.
Where next
- Lifecycle: what a restart calls (
onPreRestart,onPostRestart). - API: Supervision