1. badi
  2. hs-cctools-workqueue

Source

hs-cctools-workqueue / Example.hs

-- -*- indent-tabs-mode: nil -*- --

module Main where

import Control.Distributed.CCTools.WorkQueue

import Control.Applicative ((<$>))
import qualified Data.ByteString.Char8 as BS (pack, putStrLn)
import Foreign.C.String (newCStringLen)
import Control.Monad (forM_)



mktask :: Show a => a -> IO Task
mktask v = do
  let script = BS.pack . unlines $ [
              "t=$(echo $RANDOM % 10 | bc)"
            , "sleep $t"
            , "echo " ++ show v
            ]
  t <- task $ cmd "bash script.sh"
  specifyBuffer t script (remote "script.sh") False
  specifyTag    t $ show v
  return t

printStats :: WorkQueue -> IO ()
printStats q = do
  s <- getStats q
  print $ map ($ s) [tasksRunning, tasksWaiting, tasksComplete]

processResult :: WorkQueue -> Task -> IO ()
processResult q r = do
  putStrLn $ "Got: " ++ show (tag r)
  BS.putStrLn . output $ r
  delete r

main = do
  setDebugFlags [All]
  q  <- workqueue $ defaultQParams { _qport   = Just $ port 1024
                                   , _mode    = Just Catalog
                                   , _name    = Just "hswq"
                                   , _logfile = Just "/tmp/wq.log"
                                   }
  ts <- mapM mktask [1..10]
  forM_ ts (submit q)

  eventLoop q (seconds 1) printStats processResult

  putStrLn "Done!"