2017-03-13 15 views
0

不幸的是,包hgearman沒有提供任何測試或示例,我不能自己解決這個問題,應該如何結合connectGearmansubmitJob來將作業放到gearman job serverhgearman-client如何工作?

connectGearman結果是:

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

submitJob使用私有函數submitStateT交易。所以我只能猜測connectGearman的結果應該被包裝成S.StateT GearmanClient IO,而沒有想到如何做到這一點。

+0

我得到了一些幫助[哈斯克爾初學者郵件列表(https://mail.haskell.org/pipermail/beginners/2017-March/017435.html )。我會很快回答我自己的問題。 – palik

+0

[這裏](https://github.com/p-alik/hgearman-client/blob/upstream/demos/submit-job.hs)是一個提交作業的例子 – palik

回答

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