Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
- type SupervisorSpec = Supervisor_ Uninitialised
- type Supervisor = Supervisor_ Initialised
- data DeadLetter
- type RestartAction = ThreadId -> IO ThreadId
- data SupervisionEvent
- data RestartStrategy = OneForOne !Int RetryPolicy
- newSupervisorSpec :: IO SupervisorSpec
- newSupervisor :: SupervisorSpec -> IO Supervisor
- oneForOne :: RestartStrategy
- shutdownSupervisor :: Supervisor -> IO ()
- eventStream :: Supervisor -> TBQueue SupervisionEvent
- activeChildren :: Supervisor -> IO Int
- forkSupervised :: Supervisor -> RestartStrategy -> IO () -> IO ThreadId
- monitor :: Supervisor -> Supervisor -> IO ()
Documentation
type SupervisorSpec = Supervisor_ Uninitialised Source
type Supervisor = Supervisor_ Initialised Source
data DeadLetter Source
type RestartAction = ThreadId -> IO ThreadId Source
data SupervisionEvent Source
data RestartStrategy Source
Erlang inspired strategies. At the moment only the OneForOne
is
implemented.
Creating a new supervisor spec
In order to create a new supervisor, you need a SupervisorSpec
,
which can be acquired by a call to newSupervisor
:
newSupervisorSpec :: IO SupervisorSpec Source
Creates a new SupervisorSpec
. The reason it doesn't return a
Supervisor
is to force you to call supervise
explicitly, in order to start the
supervisor thread.
Creating a new supervisor
Restart Strategies
oneForOne :: RestartStrategy Source
Smart constructor which offers a default throttling based on fibonacci numbers.
Stopping a supervisor
shutdownSupervisor :: Supervisor -> IO () Source
Shutdown the given supervisor. This will cause the supervised children to
be killed as well. To do so, we explore the children tree, killing workers as we go,
and recursively calling shutdownSupervisor
in case we hit a monitored Supervisor
.
Accessing Supervisor event log
eventStream :: Supervisor -> TBQueue SupervisionEvent Source
Gives you access to the event this supervisor is generating, allowing you to react. It's using a bounded queue to explicitly avoid memory leaks in case you do not want to drain the queue to listen to incoming events.
activeChildren :: Supervisor -> IO Int Source
Returns the number of active threads at a given moment in time.
Supervise a forked thread
:: Supervisor | The |
-> RestartStrategy | The |
-> IO () | The computation to run |
-> IO ThreadId |
Fork a thread in a supervised mode.
Monitor another supervisor
monitor :: Supervisor -> Supervisor -> IO () Source
Monitor another supervisor. To achieve these, we simulate a new DeadLetter
,
so that the first supervisor will effectively restart the monitored one.
Thanks to the fact that for the supervisor the restart means we just copy over
its internal state, it should be perfectly fine to do so.