]> woffs.de Git - fd/haskell-amqp-utils.git/blob - agitprop.hs
agitprop: handle exceptions
[fd/haskell-amqp-utils.git] / agitprop.hs
1 {-# LANGUAGE OverloadedStrings #-}
2
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)
10 import Network.AMQP
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
16 import System.INotify
17 import qualified System.Posix.Files as F
18
19 main :: IO ()
20 main = do
21   hr "starting"
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
26   if isDir
27     then printparam' "hotfolder" $ inputFile args
28     else printparam' "input file" $
29          (inputFile args) ++
30          if (lineMode args)
31            then " (line-by-line)"
32            else ""
33   (conn, chan) <- connect args
34   printparam' "confirm mode" $ show $ confirm args
35   if (confirm args)
36     then do
37       confirmSelect chan False
38       addConfirmationListener chan confirmCallback
39     else return ()
40   let publishOneMsg f = do
41         r <-
42           publishMsg
43             chan
44             (T.pack $ currentExchange args)
45             (T.pack $ rKey args)
46             newMsg {msgBody = f, msgDeliveryMode = Just Persistent}
47         printparam "sent" $ fmap show r
48   X.catch
49     (if isDir
50        then do
51          inotify <- initINotify
52          wd <-
53            addWatch
54              inotify
55              [CloseWrite, MoveIn]
56              (inputFile args)
57              (handleEvent publishOneMsg)
58          hr $ "watching " ++ (inputFile args)
59          _ <- forever $ threadDelay 1000000
60          removeWatch wd
61        else do
62          hr $ "sending " ++ (inputFile args)
63          messageFile <- BL.readFile (inputFile args)
64          if (lineMode 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.
69   if (confirm args)
70     then waitForConfirms chan >>= return . show >> return ()
71     else return ()
72   closeConnection conn
73
74 -- | The handler for publisher confirms
75 confirmCallback :: (Word64, Bool, AckType) -> IO ()
76 confirmCallback (deliveryTag, isAll, ackType) =
77   printparam'
78     "confirmed"
79     ((show deliveryTag) ++
80      (if isAll
81         then " all "
82         else " this ") ++
83      (show ackType))
84
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 ()
91
92 -- | Hotfolder file handler
93 handleFile :: (BL.ByteString -> IO ()) -> FilePath -> IO ()
94 handleFile _ ('.':_) = return () -- ignore hidden files
95 handleFile f x =
96   X.catch
97     (hr ("sending " ++ x) >> BL.readFile x >>= f)
98     (\exception ->
99        printparam' "exception in handleFile" $
100        show (exception :: X.SomeException))