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
21 args <- getArgs >>= parseargs "agitprop"
22 printparam' "client version" $ "amqp-utils " ++ (showVersion version)
23 printparam' "routing key" $ rKey args
24 isDir <- F.getFileStatus (inputFile args) >>= return . F.isDirectory
26 then printparam' "hotfolder" $ inputFile args
27 else printparam' "input file" $
30 then " (line-by-line)"
32 (conn, chan) <- connect args
33 printparam' "confirm mode" $ show $ confirm args
36 confirmSelect chan False
37 addConfirmationListener chan confirmCallback
39 let publishOneMsg f = do
43 (T.pack $ currentExchange args)
45 newMsg {msgBody = f, msgDeliveryMode = Just Persistent}
46 printparam "sent" $ fmap show r
49 inotify <- initINotify
55 (handleEvent publishOneMsg)
57 _ <- forever $ threadDelay 1000000
61 messageFile <- BL.readFile (inputFile args)
63 then mapM_ publishOneMsg (BL.lines messageFile)
64 else publishOneMsg messageFile
65 -- all done. wait and close.
67 then waitForConfirms chan >>= return . show >> return ()
71 -- | The handler for publisher confirms
72 confirmCallback :: (Word64, Bool, AckType) -> IO ()
73 confirmCallback (deliveryTag, isAll, ackType) =
76 ((show deliveryTag) ++
82 -- | hotfolder event handler
83 handleEvent :: (BL.ByteString -> IO ()) -> Event -> IO ()
84 handleEvent f (Closed False (Just x) True) = handleFile f x
85 handleEvent f (MovedIn False x _) = handleFile f x
86 handleEvent _ _ = return ()
88 -- | hotfolder file handler
89 handleFile :: (BL.ByteString -> IO ()) -> FilePath -> IO ()
90 handleFile _ ('.':_) = return ()
91 handleFile f x = hr x >> BL.readFile x >>= f