Daemons in Haskell made fun and easy https://github.com/scvalex/daemons
|Latest on Hackage:||0.2.1|
This package is not currently in any snapshots. If you're interested in using it, we recommend adding it to Stackage Nightly. Doing so will make builds more reliable, and allow stackage.org to host generated Haddocks.
Daemons in Haskell made fun and easy
Here's AddOne, a simple daemon that waits for a number and responds with the incremented number.
import Data.Default ( def ) import System.Environment ( getArgs ) import System.Daemon addOne :: Int -> IO Int addOne n = return (n + 1) main :: IO () main = do ensureDaemonRunning "addOne" def addOne [n] <- getArgs res <- runClient "localhost" 5000 ((read n) :: Int) print (res :: Maybe Int)
Running it, we see:
% addone 22 Daemon started on port 5000 Just 23 % addone 41 Just 42
The two important functions above are
checks if a daemon named
addOne is already running, and starts it if
runClient which connects to the daemon running on
localhost:5000, passes it a number, and waits for the response.
What would I use this for?
You can use the
System.Posix.Daemonto turn your program into a daemon for Unix-like systems. You'd want to do this for practically every program that's meant to run as a server.
You can use the functions from
Serializeto communicate with running Haskell program. At the simplest, you could query the program for its status, or instruct it to shutdown cleanly. A more complex use would be adding a full REPL into a running Haskell process (think
You can use the helpers from
System.Daemonto trivially do the above. Check out the following tutorials and examples for details.
Tutorials and examples
Memo - in which we write an in-memory key-value store,
Queue - a task queue using the streaming interface of
This package is on Hackage. To install it, run:
cabal update cabal install daemons
Control.Pipe.C3provides simple RPC-like wrappers for pipes.
Control.Pipe.Serializeprovides pipes to serialize and deserialize streams of strict
ByteStrings using cereal.
Control.Pipe.Socketprovides functions to setup strict
ByteStringpipes around sockets.
System.Daemonprovides a high-level interface to starting daemonized programs that are controlled through sockets.
System.Posix.Daemonprovides a low-level interface to starting, and controlling detached jobs.