【问题标题】:how does hgearman-client work?hgearman-client 是如何工作的?
【发布时间】:2017-03-13 21:59:18
【问题描述】:

不幸的是,hgearman 包没有提供任何测试或示例,我无法为自己解决如何将connectGearmansubmitJob 组合到gearman job server 的工作。

connectGearman 的结果是:

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

但是submitJob 使用私有函数submit 处理StateT。所以我只能猜测connectGearman 的结果应该被包装到S.StateT GearmanClient IO 中,而不知道该怎么做。

【问题讨论】:

标签: haskell gearman


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-14
    • 2021-08-02
    • 1970-01-01
    • 2021-07-03
    • 2014-05-16
    • 2022-11-24
    • 1970-01-01
    • 2012-10-14
    相关资源
    最近更新 更多