Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

haskell - how does hgearman-client work?

Unfortunately the package 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.

The result of connectGearman is:

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

but submitJob uses private function submit which deals with StateT. So I can only guess the result of connectGearman should be wrapped into S.StateT GearmanClient IO without faintest idea how to do that.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Below is my implementation of Gearman Client:

{-# 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")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...