[Haskell-cafe] how to implement daemon start and stop directives?
lrpalmer at gmail.com
Thu Jan 22 01:46:01 EST 2009
On Wed, Jan 21, 2009 at 11:36 PM, Belka <lambda-belka at yandex.ru> wrote:
> Could somebody please share some experience on how to implement daemon
> and stop directives. In theory I need something like this:
> 1. "my_daemon start" - starts my app with an infinite loop of serving
> 2. "my_daemon stop" - puts in some TVar a value signalizing, that stop is
> given - infinite loop brakes.
You can abstract this pattern:
-- runs its argument in an infinite loop, and returns an action that stops
daemon :: IO () -> IO (IO ())
daemon action = do
stopvar <- atomically $ newTVar False
let run = do
stop <- atomically $ readTVar stopvar
if stop then return () else (action >> run)
return (atomically $ writeTVar stopvar True)
TVars are overkill here, actually, an IORef would be just fine, I think.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe