Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
- type SupervisorSpec0 q = Supervisor_ q Uninitialised
- type Supervisor0 q = Supervisor_ q Initialised
- class QueueLike q where
- newQueueIO :: Int -> IO (q a)
- readQueue :: q a -> STM a
- writeQueue :: q a -> a -> STM ()
- data Child_ q
- data DeadLetter
- type RestartAction = ThreadId -> IO ThreadId
- data SupervisionEvent
- data RestartStrategy = OneForOne
- newSupervisorSpec :: QueueLike q => RestartStrategy -> Int -> IO (SupervisorSpec0 q)
- newSupervisor :: QueueLike q => SupervisorSpec0 q -> IO (Supervisor0 q)
- fibonacciRetryPolicy :: RetryPolicyM IO
- shutdownSupervisor :: QueueLike q => Supervisor0 q -> IO ()
- eventStream :: QueueLike q => Supervisor0 q -> q SupervisionEvent
- activeChildren :: QueueLike q => Supervisor0 q -> IO Int
- forkSupervised :: QueueLike q => Supervisor0 q -> RetryPolicyM IO -> IO () -> IO ThreadId
- monitor :: QueueLike q => Supervisor0 q -> Supervisor0 q -> IO ()
Documentation
type SupervisorSpec0 q = Supervisor_ q Uninitialised Source
type Supervisor0 q = Supervisor_ q Initialised Source
class QueueLike q where Source
newQueueIO :: Int -> IO (q a) Source
readQueue :: q a -> STM a Source
writeQueue :: q a -> a -> STM () 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 :: QueueLike q => RestartStrategy -> Int -> IO (SupervisorSpec0 q) 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
newSupervisor :: QueueLike q => SupervisorSpec0 q -> IO (Supervisor0 q) Source
Restart Policies
fibonacciRetryPolicy :: RetryPolicyM IO Source
Smart constructor which offers a default throttling based on fibonacci numbers.
Stopping a supervisor
shutdownSupervisor :: QueueLike q => Supervisor0 q -> 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 :: QueueLike q => Supervisor0 q -> q 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 :: QueueLike q => Supervisor0 q -> IO Int Source
Returns the number of active threads at a given moment in time.
Supervise a forked thread
:: QueueLike q | |
=> Supervisor0 q | The |
-> RetryPolicyM IO | The retry policy to use |
-> IO () | The computation to run |
-> IO ThreadId |
Fork a thread in a supervised mode.
Monitor another supervisor
monitor :: QueueLike q => Supervisor0 q -> Supervisor0 q -> 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.