2017-03-13 3 views
0

Malheureusement, le paquet hgearman ne fournit pas de test ou d'exemple et je ne peux pas résoudre par moi-même comment combiner connectGearman et submitJob pour mettre un travail au gearman job server.Comment fonctionne hgearman-client?

Le résultat de connectGearman est:

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

mais submitJob utilise la fonction privée submit qui traite StateT. Donc, je ne peux que deviner le résultat de connectGearman devrait être enveloppé dans S.StateT GearmanClient IO sans la moindre idée de comment faire cela.

+0

Je suis un peu d'aide par [débutants haskell liste de diffusion] (https://mail.haskell.org/pipermail/beginners/2017-March/017435.html). Je vais fournir une réponse à ma propre question bientôt. – palik

+0

[ici] (https://github.com/p-alik/hgearman-client/blob/upstream/demos/submit-job.hs) est un exemple de soumission – palik

Répondre

0

ci-dessous est ma mise en œuvre de 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")