Skip to content

BirdseyeSoftware/dp-system-process

Repository files navigation

WARNING! This is a work in progress, and it is still experimental, use it at your own risk

dp-system-process

This library allows the creation of system processes using the distributed-process Actor's library.

Installation and usage

This library requires using cabal sandbox with the following sub-projects (mind the 'development' branch part):

In order to run tests, you'll also need network-transport-inmemory

There is a Makefile that would facilitate running the test suite, just do make test.

Documentation

Currently you can read the documentation by doing cabal haddock and accessing it through dist/doc folder

Example

The API tries to replicate as much as it can from System.Process, there are bits that change specifically on the std_out and std_err. There is also a new exit_code field.

You may send ByteStrings to the process std_in by using the writeStdin function.

An example to better explain how it works:

import Control.Monad (forever)
import Control.Monad.Trans (liftIO)

import Control.Concurrent (threadDelay)

import Network.Transport.Chan (createTransport)
import Control.Distributed.Process.Node (runProcess, newLocalNode, initRemoteTable)
import qualified Control.Distributed.Process as Process

import Control.Distributed.Process.SystemProcess as Proc

main :: IO ()
main = do
  -- Cloud Haskell boilerplate
  transport <- createTransport
  node <- newLocalNode transport initRemoteTable
  runProcess node $ do

    -- Actual example

    -- Listener is going to receive stdout and exit code from the system process actor
    listenerPid <- Process.spawnLocal $ do
      forever $
        Process.receiveWait [ Process.matchIf Proc.isExitCode (liftIO . print)
                            , Process.matchIf Proc.isStdOut (liftIO . print) ]

    -- current actor is going to become the process actor
    Proc.runSystemProcess
      -- Process.send std_out and exit_code to listenerPid
      -- There are different transports, check the haddock documentation
      (Proc.proc "ls" ["-l", "-a"]) { Proc.std_out = Proc.sendToPid listenerPid
                                    , Proc.exit_code = Proc.sendToPid listenerPid }

    liftIO $ threadDelay 10000
    Process.kill listenerPid "time to die..."

About

Handle system processes in Cloud Haskell

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published