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 f = do
44 (T.pack $ currentExchange args)
46 newMsg {msgBody = f, msgDeliveryMode = Just Persistent}
47 printparam "sent" $ fmap show r
51 inotify <- initINotify
57 (handleEvent publishOneMsg)
58 hr $ "watching " ++ (inputFile args)
59 _ <- forever $ threadDelay 1000000
62 hr $ "sending " ++ (inputFile args)
63 messageFile <- BL.readFile (inputFile args)
65 then mapM_ publishOneMsg (BL.lines messageFile)
66 else publishOneMsg messageFile)
67 (\exception -> printparam' "exception" $ show (exception :: X.SomeException))
68 -- all done. wait and close.
70 then waitForConfirms chan >>= return . show >> return ()
74 -- | The handler for publisher confirms
75 confirmCallback :: (Word64, Bool, AckType) -> IO ()
76 confirmCallback (deliveryTag, isAll, ackType) =
79 ((show deliveryTag) ++
85 -- | Hotfolder event handler
86 handleEvent :: (BL.ByteString -> IO ()) -> Event -> IO ()
87 -- just handle closewrite and movedin events
88 handleEvent f (Closed False (Just x) True) = handleFile f x
89 handleEvent f (MovedIn False x _) = handleFile f x
90 handleEvent _ _ = return ()
92 -- | Hotfolder file handler
93 handleFile :: (BL.ByteString -> IO ()) -> FilePath -> IO ()
94 handleFile _ ('.':_) = return () -- ignore hidden files
97 (hr ("sending " ++ x) >> BL.readFile x >>= f)
99 printparam' "exception in handleFile" $
100 show (exception :: X.SomeException))