1 -- SPDX-FileCopyrightText: 2022 Frank Doepper
3 -- SPDX-License-Identifier: GPL-3.0-only
5 -- generic amqp consumer
6 import Control.Concurrent
7 import qualified Control.Exception as X
8 import qualified Data.Text as T
10 import Data.Version (showVersion)
12 import Network.AMQP.Utils.Connection
13 import Network.AMQP.Utils.Helpers
14 import Network.AMQP.Utils.Options
15 import Paths_amqp_utils (version)
16 import System.Environment
22 args <- getArgs >>= parseargs 'k'
23 let addiArgs = reverse $ additionalArgs args
24 printparam "client version" ["amqp-utils", showVersion version]
25 (conn, chan) <- connect args
26 addChannelExceptionHandler chan (X.throwTo tid)
28 printparam "prefetch" $ preFetch args
29 qos chan 0 (preFetch args) False
30 -- attach to given queue? or build exclusive queue and bind it?
33 (tempQueue chan (tmpQName args) (bindings args))
35 (fmap T.pack (qName args))
36 printparam "queue name" queue
37 printparam "consumer args" $ formatheaders fieldshow $ streamoffset args
38 printparam "shown body chars" $ anRiss args
39 printparam "temp dir" $ tempDir args
40 printparam "callback" $ fileProcess args
41 printparam "callback args" $ addiArgs
42 printparam "cleanup temp file" $
43 maybe Nothing (\_ -> Just (cleanupTmpFile args)) (fileProcess args)
44 -- subscribe to the queue
52 (myCallback args addiArgs tid)
55 printparam "consumer tag" ctag
56 printparam "send acks" $ ack args
57 printparam "requeue if rejected" $ (ack args) && (requeuenack args)
58 hr "entering main loop"
62 hr "connection closed"
65 -- | exclusive temp queue
66 tempQueue :: Channel -> String -> [(String, String)] -> IO T.Text
67 tempQueue chan tmpqname bindlist = do
71 newQueue {queueExclusive = True, queueName = T.pack tmpqname}
74 bindQueue chan q (T.pack xchange) (T.pack bkey) >>
75 printparam "binding" [xchange, bkey])
79 -- | process received message
80 myCallback :: Args -> [String] -> ThreadId -> (Message, Envelope) -> IO ()
81 myCallback a addi tid m@(_, envi) = do
82 let numstring = show $ envDeliveryTag envi
83 hr $ "BEGIN " ++ numstring
85 (callbackoptions, callbackenv) <-
87 (printmsg Nothing m (anRiss a) now)
88 (\x -> X.throwTo tid (x :: X.IOException) >> return ([], []))
89 either (\e -> printparam "ERROR" (e :: X.IOException) >> reje envi a) return =<<
100 hr $ "END " ++ numstring