daemons-0.2.1: Daemons in Haskell made fun and easy

Safe HaskellNone

System.Daemon

Contents

Description

An RPC-like interface for daemons is provided by ensureDaemonRunning and runClient.

A more versatile interface that lets you supply your own Handler is provided by ensureDaemonWithHandlerRunning and runClientWithHandler. These are useful if, for instance, you need streaming requests or replies, or if you need to change your event handler at runtime.

The event handling loop is provided by runInForeground. You may want to use this for debugging purposes or if you want to handle daemonization manually.

Synopsis

Daemons

ensureDaemonRunningSource

Arguments

:: (Serialize a, Serialize b) 
=> String

name

-> DaemonOptions

options

-> (a -> IO b)

handler

-> IO () 

Simple wrapper around ensureDaemonWithHandlerRunning which uses a simple function to respond to commands and doesn't deal with pipes.

The handler is just a function that takes a command and returns a response.

ensureDaemonWithHandlerRunningSource

Arguments

:: String

name

-> DaemonOptions

options

-> Handler ()

handler

-> IO () 

Start a daemon running on the given port, using the given handler to respond to events. If the daemon is already running, don't do anything. Returns immediately.

The pidfile PidFile options will be created and locked. This function checks the pidfile to see if the daemon is already running.

The daemon will listen for incoming connections on all interfaces on daemonPort options.

The handler is a function that takes the reader and writer ByteString pipes and does something with them. See commandReceiver for an example handler.

Clients,

runClientSource

Arguments

:: (Serialize a, Serialize b) 
=> HostName

hostname

-> Port

port

-> a

command

-> IO (Maybe b) 

Send a command to the daemon running at the given network address and wait for a response.

This is a simple wrapper around runClientWithHandler that sends a single command and waits for a single response.

If the connection is closed before receiving a response, return Nothing.

runClientWithHandlerSource

Arguments

:: HostName

hostname

-> Port

port

-> Handler a

command

-> IO a 

Connect to the given network address and run the handler on the reader and wrier pipes for the socket.

The handler is a function that takes the reader and writer ByteString pipes and does something with them. For an example handler, see commandSender, which sends a command and waits for a response.

Types

data DaemonOptions Source

The configuration options of a daemon. See ensureDaemonRunning for a description of each.

data PidFile Source

The location of the daemon's pidfile.

Constructors

InHome 
PidFile FilePath 

Helpers

runInForeground :: Port -> Handler () -> IO ()Source

Start the given handler in the foreground. It will listen and respond to events on the given port.

This is the function that ensureDaemonWithHandlerRunning runs on the daemon thread.

bindPort :: Port -> IO SocketSource

Create a socket and bind it to the given port.

getSocket :: HostName -> Port -> IO SocketSource

Create a socket connected to the given network address.