hgearman-client 是如何工作的?

how does hgearman-client work?

不幸的是包裹hgearman does not provide any test or example and I can't work it out for myself how should be combined connectGearman and submitJob to put a job to the gearman job server

connectGearman的结果是:

ghci> conn <- connectGearman (B.pack "x") ("localhost"::HostName) (4730::Port)
ghci> :t conn
conn :: Either GearmanError GearmanClient

但是 submitJob 使用私有函数 submit which deals with StateT. So I can only guess the result of connectGearman should be wrapped into S.StateT GearmanClient IO 完全不知道该怎么做。

下面是我对 Gearman 客户端的实现:

{-# LANGUAGE DeriveDataTypeable #-}
import Control.Exception (Exception, IOException, catch, throwIO)
import qualified Data.ByteString.Char8 as B
import Data.Typeable     (Typeable)
import qualified Network.Gearman.Client as C
import Network.Gearman.Internal (Function, GearmanClient, GearmanError, Port, withGearman)
import Network.Socket (HostName)

data ConnectException = ConnectException HostName Port IOException
    deriving (Show, Typeable)
instance Exception ConnectException

main :: IO ()
main = do
  c <- C.connectGearman (B.pack "client-id") host port `catch` \e -> throwIO (ConnectException host port e)
  either (error . B.unpack) return c
    >>= submitFooJob
    >>= either(error . B.unpack) (putStrLn . B.unpack)
  return ()
    where
      host = "localhost"::HostName
      port =  4730::Port
      submitFooJob :: GearmanClient -> IO (Either GearmanError B.ByteString)
      submitFooJob gc = withGearman gc $ C.submitJob (B.pack "foo"::Function) (B.pack "bar")