forked from rabbitmq/rabbitmq-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
receiveLogs.hs
48 lines (39 loc) · 1.35 KB
/
receiveLogs.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env stack
{- stack --install-ghc
runghc
--package amqp
--package bytestring
-}
{-# LANGUAGE OverloadedStrings #-}
import Network.AMQP
import qualified Data.ByteString.Lazy.Char8 as BL
import Data.Monoid ((<>))
import Control.Concurrent (threadDelay)
logsExchange = "logs"
main :: IO ()
main = do
conn <- openConnection "127.0.0.1" "/" "guest" "guest"
ch <- openChannel conn
declareExchange ch newExchange {exchangeName = logsExchange,
exchangeType = "fanout",
exchangeDurable = False}
(q, _, _) <- declareQueue ch newQueue {queueName = "",
queueAutoDelete = True,
queueDurable = False}
bindQueue ch q logsExchange ""
BL.putStrLn " [*] Waiting for messages. To exit press CTRL+C"
consumeMsgs ch q Ack deliveryHandler
-- waits for keypresses
getLine
closeConnection conn
deliveryHandler :: (Message, Envelope) -> IO ()
deliveryHandler (msg, metadata) = do
BL.putStrLn $ " [x] Received " <> body
threadDelay (1000000 * n)
BL.putStrLn " [x] Done"
ackEnv metadata
where
body = msgBody msg
n = countDots body
countDots :: BL.ByteString -> Int
countDots = fromIntegral . BL.count '.'