1 {-# LANGUAGE OverloadedStrings #-}
3 import Control.Concurrent (threadDelay)
4 import Control.Monad (forever)
5 import qualified Data.ByteString.Lazy.Char8 as BL
6 import qualified Data.Text as T
7 import Data.Version (showVersion)
8 import Data.Word (Word64)
10 import Network.AMQP.Utils.Connection
11 import Network.AMQP.Utils.Helpers
12 import Network.AMQP.Utils.Options
13 import Paths_amqp_utils (version)
14 import System.Environment
16 import qualified System.Posix.Files as F
22 args <- getArgs >>= parseargs "agitprop"
23 printparam' "client version" $ "amqp-utils " ++ (showVersion version)
24 printparam' "routing key" $ rKey args
25 isDir <- F.getFileStatus (inputFile args) >>= return . F.isDirectory
27 then printparam' "hotfolder" $ inputFile args
28 else printparam' "input file" $
31 then " (line-by-line)"
33 (conn, chan) <- connect args
34 printparam' "confirm mode" $ show $ confirm args
37 confirmSelect chan False
38 addConfirmationListener chan confirmCallback
40 let publishOneMsg f = do
44 (T.pack $ currentExchange args)
46 newMsg {msgBody = f, msgDeliveryMode = Just Persistent}
47 printparam "sent" $ fmap show r
50 inotify <- initINotify
52 addWatch inotify [Close] (inputFile args) (handleEvent publishOneMsg)
54 _ <- forever $ threadDelay 1000000
58 messageFile <- BL.readFile (inputFile args)
60 then mapM_ publishOneMsg (BL.lines messageFile)
61 else publishOneMsg messageFile
62 -- all done. wait and close.
64 then waitForConfirms chan >>= return . show >> return ()
68 -- | The handler for publisher confirms
69 confirmCallback :: (Word64, Bool, AckType) -> IO ()
70 confirmCallback (deliveryTag, isAll, ackType) =
73 ((show deliveryTag) ++
79 -- | hotfolder event handler
80 handleEvent :: (BL.ByteString -> IO ()) -> Event -> IO ()
81 handleEvent f (Closed False (Just x) True) = hr x >> BL.readFile x >>= f
82 handleEvent _ _ = return ()