-
-
Save jmmk/17c85f4d4e68aa3e03e5dfdd527ec61c to your computer and use it in GitHub Desktop.
Tailing MongoDB OpLog in Haskell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
module Mongolito where | |
import Control.Exception | |
import Control.Monad.IO.Class (liftIO) | |
import Database.MongoDB | |
import System.Log.Logger | |
localDb :: Database | |
localDb = "local" | |
opLogColl :: Collection | |
opLogColl = "oplog.rs" | |
tailOpLog :: Pipe -> ([Document] -> IO ()) -> IO () | |
tailOpLog pipe f = bracket acquire release (run . loop) | |
where | |
acquire = run . find $ (select [] opLogColl) { options = [TailableCursor, AwaitData, NoCursorTimeout] } | |
release x = do | |
infoM "mongodb" "Closing opLog cursor..." | |
run $ closeCursor x | |
loop cr = do | |
xs <- nextBatch cr | |
liftIO $ do | |
debugM "mongodb" (concat ["nextBatch.length=", show $ length xs]) | |
f xs | |
loop cr | |
run = access pipe master localDb | |
mainService :: IO () | |
mainService = do | |
infoM "main" "Starting..." | |
bracket (connect (host "127.0.0.1")) close $ \pipe -> tailOpLog pipe print |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment