Skip to content

Commit

Permalink
Log exceptions in main loop of PGListener
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Jan 30, 2023
1 parent e31d7b8 commit 69ebc36
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions IHP/PGListener.hs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,15 @@ subscribe channel callback pgListener = do
-- A naive implementation might be just kicking of an async for each message. But in that case
-- the messages might be delivered to the final consumer out of order.
(inChan, outChan) <- Queue.newChan
reader <- async (forever (Queue.readChan outChan >>= callback))

let
-- We need to log any exception, otherwise there might be silent errors
logException :: SomeException -> IO ()
logException exception = logError pgListener ("Error in pg_notify handler: " <> cs (displayException exception))

reader <- async $ forever do
message <- Queue.readChan outChan
callback message `Exception.catch` logException
let subscription = Subscription { .. }

modifyIORef' (get #subscriptions pgListener) (HashMap.insertWith mappend channel [subscription] )
Expand All @@ -148,7 +156,7 @@ subscribeJSON channel callback pgListener = subscribe channel callback' pgListen
let payload = (get #notificationData notification)
case Aeson.decodeStrict' payload of
Just payload -> callback payload
Nothing -> pure ()
Nothing -> logError pgListener ("PGListener.subscribeJSON: Failed to parse " <> tshow payload)

-- | Stops the callback of a subscription from receiving further notifications
--
Expand Down Expand Up @@ -239,4 +247,7 @@ notifyLoop listeningToVar listenToVar subscriptions = do
listenToChannel :: PG.Connection -> Channel -> IO ()
listenToChannel databaseConnection channel = do
PG.execute databaseConnection "LISTEN ?" [PG.Identifier (cs channel)]
pure ()
pure ()

logError :: PGListener -> Text -> IO ()
logError pgListener message = let ?context = pgListener.modelContext in Log.error message

0 comments on commit 69ebc36

Please sign in to comment.