import Network.AMQP.Utils.Connection
import qualified Data.ByteString.Lazy.Char8 as BL
import Data.Word ( Word64 )
+import qualified System.Posix.Files as F
+import System.INotify
+import Control.Monad ( forever )
+import Control.Concurrent ( threadDelay )
main :: IO ()
main = do
args <- getArgs >>= parseargs "agitprop"
printparam' "client version" $ "amqp-utils " ++ (showVersion version)
printparam' "routing key" $ rKey args
- printparam' "input file" $
- (inputFile args) ++ if (lineMode args) then " (line-by-line)" else ""
- messageFile <- BL.readFile (inputFile args)
+ isDir <- F.getFileStatus (inputFile args) >>= return . F.isDirectory
+ if isDir
+ then printparam' "hotfolder" $ inputFile args
+ else printparam' "input file" $ (inputFile args) ++ if (lineMode args) then " (line-by-line)" else ""
(conn, chan) <- connect args
printparam' "confirm mode" $ show $ confirm args
if (confirm args)
, msgDeliveryMode = Just Persistent
}
printparam "sent" $ fmap show r
- if (lineMode args)
- then mapM_ publishOneMsg (BL.lines messageFile)
- else publishOneMsg messageFile
+ if isDir
+ then do
+ inotify <- initINotify
+ wd <- addWatch inotify [ Close ] (inputFile args) (handleEvent publishOneMsg)
+ hr (inputFile args)
+ _ <- forever $ threadDelay 1000000
+ removeWatch wd
+ else do
+ hr (inputFile args)
+ messageFile <- BL.readFile (inputFile args)
+ if (lineMode args)
+ then mapM_ publishOneMsg (BL.lines messageFile)
+ else publishOneMsg messageFile
+ -- all done. wait and close.
if (confirm args)
then waitForConfirms chan >>= return . show >> return ()
else return ()
printparam' "confirmed"
((show deliveryTag) ++
(if isAll then " all " else " this ") ++ (show ackType))
+
+-- | hotfolder event handler
+handleEvent :: (BL.ByteString -> IO ()) -> Event -> IO ()
+handleEvent f (Closed False (Just x) True) = hr x >> BL.readFile x >>= f
+handleEvent _ _ = return ()
{ mkDerivation, amqp, base, bytestring, connection, containers
, data-default-class, process, stdenv, text, time, tls, x509-system
+, unix, hinotify
}:
mkDerivation {
pname = "amqp-utils";
enableSharedExecutables = false;
executableHaskellDepends = [
amqp base bytestring connection containers data-default-class
- process text time tls x509-system
+ process text time tls x509-system unix hinotify
];
description = "Generic Haskell AMQP Consumer";
license = stdenv.lib.licenses.gpl3;