hgearman-client如何运作?

时间:2017-03-13 21:59:18

标签: haskell gearman

很遗憾,软件包hgearman没有提供任何测试或示例,我无法自行解决如何将connectGearmansubmitJob合并到一起gearman job server

connectGearman的结果是:

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

submitJob使用处理StateT的私有函数submit。所以我只能猜测connectGearman的结果应该包含在S.StateT GearmanClient IO中,而不知道如何做到这一点。

1 个答案:

答案 0 :(得分:0)

以下是我对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")