[Haskell-cafe] how to implement daemon start and stop directives?

Luke Palmer 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:
>
> Hi!
>
> Could somebody please share some experience on how to implement daemon
> start
> and stop directives. In theory I need something like this:
> 1. "my_daemon start" - starts my app with an infinite loop of serving
> inside.
> 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
the loop
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)
    forkIO run
    return (atomically $ writeTVar stopvar True)

TVars are overkill here, actually, an IORef would be just fine, I think.

Luke
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090121/ab5e8c06/attachment.htm


More information about the Haskell-Cafe mailing list