Safe Haskell | None |
---|
Control.Immortal
Description
This module is designed to be imported qualified, e.g.
import qualified Control.Immortal as Immortal
- data Thread
- create :: MonadBaseControl IO m => m () -> m Thread
- createWithLabel :: MonadBaseControl IO m => String -> m () -> m Thread
- stop :: Thread -> IO ()
- threadId :: Thread -> ThreadId
- onFinish :: MonadBaseControl IO m => (Either SomeException () -> m ()) -> m () -> m ()
Documentation
create :: MonadBaseControl IO m => m () -> m ThreadSource
Spawn a new immortal thread running the given computation.
If the computation ever finishes (either normally or due to an exception), it will be restarted (in the same thread).
The monadic «state» (captured by thMonadBaseControl
instance) will
be preserved if the computation terminates normally, and reset when the
exception is thrown, so be cautious when m
is stateful.
It is completely safe, however, to instantiate m
with
something like ReaderT conf IO
to pass configuration to the new
thread.
createWithLabel :: MonadBaseControl IO m => String -> m () -> m ThreadSource
Like create
, but also apply the given label to the thread
(using labelThread
).
Stop (kill) an immortal thread.
This is the only way to really stop an immortal thread.
onFinish :: MonadBaseControl IO m => (Either SomeException () -> m ()) -> m () -> m ()Source
Run a callback every time the action finishes. This can be used e.g. to log exceptions or attempts to exit when such attempts are not expected. Example usage:
Immortal.create $ Immortal.onFinish print myAction
This is nothing more than a simple wrapper around try
.