1 {-# LANGUAGE OverloadedStrings #-}
3 import Control.Concurrent (threadDelay)
4 import qualified Control.Exception as X
5 import Control.Monad (forever)
6 import qualified Data.ByteString.Lazy.Char8 as BL
7 import qualified Data.Text as T
8 import Data.Version (showVersion)
9 import Data.Word (Word64)
11 import Network.AMQP.Utils.Connection
12 import Network.AMQP.Utils.Helpers
13 import Network.AMQP.Utils.Options
14 import Paths_amqp_utils (version)
15 import System.Environment
17 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 = publishOneMsg' chan args
44 inotify <- initINotify
50 (handleEvent publishOneMsg)
51 hr $ "watching " ++ (inputFile args)
52 _ <- forever $ threadDelay 1000000
55 hr $ "sending " ++ (inputFile args)
56 messageFile <- BL.readFile (inputFile args)
58 then mapM_ publishOneMsg (BL.lines messageFile)
59 else publishOneMsg messageFile)
60 (\exception -> printparam' "exception" $ show (exception :: X.SomeException))
61 -- all done. wait and close.
63 then waitForConfirms chan >>= return . show >> return ()
67 -- | The handler for publisher confirms
68 confirmCallback :: (Word64, Bool, AckType) -> IO ()
69 confirmCallback (deliveryTag, isAll, ackType) =
72 ((show deliveryTag) ++
78 -- | Hotfolder event handler
79 handleEvent :: (BL.ByteString -> IO ()) -> Event -> IO ()
80 -- just handle closewrite and movedin events
81 handleEvent f (Closed False (Just x) True) = handleFile f x
82 handleEvent f (MovedIn False x _) = handleFile f x
83 handleEvent _ _ = return ()
85 -- | Hotfolder file handler
86 handleFile :: (BL.ByteString -> IO ()) -> FilePath -> IO ()
87 handleFile _ ('.':_) = return () -- ignore hidden files
90 (hr ("sending " ++ x) >> BL.readFile x >>= f)
92 printparam' "exception in handleFile" $
93 show (exception :: X.SomeException))
95 -- | Publish one message with our settings
96 publishOneMsg' :: Channel -> Args -> BL.ByteString -> IO ()
97 publishOneMsg' c a f = do
101 (T.pack $ currentExchange a)
104 , msgDeliveryMode = Just Persistent
105 , msgTimestamp = msgtimestamp a
107 , msgType = msgtype a
108 , msgUserID = msguserid a
109 , msgApplicationID = msgappid a
110 , msgClusterID = msgclusterid a
111 , msgContentType = msgcontenttype a
112 , msgContentEncoding = msgcontentencoding a
113 , msgReplyTo = msgreplyto a
114 , msgPriority = msgprio a
115 , msgCorrelationID = msgcorrid a
116 , msgExpiration = msgexp a
119 printparam "sent" $ fmap show r