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 "shown body chars" $ anRiss args
38 printparam "temp dir" $ tempDir args
39 printparam "callback" $ fileProcess args
40 printparam "callback args" $ addiArgs
41 printparam "cleanup temp file" $
42 maybe Nothing (\_ -> Just (cleanupTmpFile args)) (fileProcess args)
43 -- subscribe to the queue
51 (myCallback args addiArgs tid)
52 printparam "consumer tag" ctag
53 printparam "send acks" $ ack args
54 printparam "requeue if rejected" $ (ack args) && (requeuenack args)
55 hr "entering main loop"
56 sleepingBeauty >>= printparam "exception"
58 hr "connection closed"
60 -- | exclusive temp queue
61 tempQueue :: Channel -> String -> [(String, String)] -> IO T.Text
62 tempQueue chan tmpqname bindlist = do
66 newQueue {queueExclusive = True, queueName = T.pack tmpqname}
69 bindQueue chan q (T.pack xchange) (T.pack bkey) >>
70 printparam "binding" [xchange, bkey])
74 -- | process received message
75 myCallback :: Args -> [String] -> ThreadId -> (Message, Envelope) -> IO ()
76 myCallback a addi tid m@(_, envi) = do
77 let numstring = show $ envDeliveryTag envi
78 hr $ "BEGIN " ++ numstring
80 (callbackoptions, callbackenv) <-
82 (printmsg Nothing m (anRiss a) now)
83 (\x -> X.throwTo tid (x :: X.IOException) >> return ([], []))
84 either (\e -> printparam "ERROR" (e :: X.IOException) >> reje envi a) return =<<
95 hr $ "END " ++ numstring