1 {-# LANGUAGE OverloadedStrings #-}
3 -- generic AMQP rpc server
4 import Control.Concurrent
5 import qualified Control.Exception as X
7 import qualified Data.ByteString.Char8 as BS
8 import Data.Map (singleton)
10 import qualified Data.Text as T
12 import Data.Version (showVersion)
14 import Network.AMQP.Types
15 import Network.AMQP.Utils.Connection
16 import Network.AMQP.Utils.Helpers
17 import Network.AMQP.Utils.Options
18 import Paths_amqp_utils (version)
19 import System.Environment
25 args <- getArgs >>= parseargs 'r'
27 (printparam "worker" $ fromJust $ fileProcess args)
28 (error "-X option required")
29 printparam "cleanup temp file" $ cleanupTmpFile args
30 let addiArgs = reverse $ additionalArgs args
31 printparam "client version" ["amqp-utils", showVersion version]
32 (conn, chan) <- connect args
33 addChannelExceptionHandler chan (X.throwTo tid)
38 newQueue {queueExclusive = True, queueName = (T.pack $ tmpQName args)} >>=
39 (\(x, _, _) -> return x))
41 (fmap T.pack (qName args))
42 printparam "queue name" queue
43 if (currentExchange args /= "")
45 printparam "exchange" $ currentExchange args
46 bindQueue chan queue (T.pack $ currentExchange args) queue
55 (rpcServerCallback tid args addiArgs chan)
56 printparam "consumer tag" ctag
57 printparam "send acks" $ ack args
58 printparam "requeue if rejected" $
60 then Just (requeuenack args)
62 hr "entering main loop"
64 (forever $ threadDelay 5000000)
65 (\e -> printparam "exception" (e :: X.SomeException))
67 hr "connection closed"
70 ThreadId -> Args -> [String] -> Channel -> (Message, Envelope) -> IO ()
71 rpcServerCallback tid a addi c m@(msg, env) = do
72 let numstring = show $ envDeliveryTag env
73 hr $ "BEGIN " ++ numstring
75 (callbackoptions, callbackenv) <-
77 (printmsg Nothing m (anRiss a) now)
78 (\x -> X.throwTo tid (x :: X.SomeException) >> return ([], []))
79 either (\e -> printparam "ERROR" (e :: X.SomeException)) return =<<
90 hr $ "END " ++ numstring
97 (fromJust $ msgReplyTo msg)
100 , msgCorrelationID = msgCorrelationID msg
101 , msgTimestamp = msgTimestamp msg
102 , msgExpiration = msgExpiration msg
105 FieldTable $ singleton "exitcode" $ FVString $ BS.pack $ show e