Skip to content

Commit

Permalink
fix: don't hide error on LISTEN channel failure
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-chavez committed Mar 11, 2024
1 parent 9405a62 commit 34bb8f2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
13 changes: 7 additions & 6 deletions src/PostgREST/AppState.hs
Original file line number Diff line number Diff line change
Expand Up @@ -487,15 +487,16 @@ listener appState observer = do
putIsListenerOn appState True
SQL.listen db $ SQL.toPgIdentifier dbChannel
SQL.waitForNotifications handleNotification db
_ ->
die $ "Could not listen for notifications on the " <> dbChannel <> " channel"
Left err -> do
observer $ DBListenerFail dbChannel err
exitFailure
where
handleFinally _ False _ = do
observer DBListenerFailNoRecoverObs
handleFinally dbChannel False err = do
observer $ DBListenerFailNoRecoverObs dbChannel err
killThread (getMainThreadId appState)
handleFinally dbChannel True _ = do
handleFinally dbChannel True err = do
-- if the thread dies, we try to recover
observer $ DBListenerFailRecoverObs dbChannel
observer $ DBListenerFailRecoverObs dbChannel err
putIsListenerOn appState False
-- assume the pool connection was also lost, call the connection worker
connectionWorker appState
Expand Down
20 changes: 14 additions & 6 deletions src/PostgREST/Observation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module PostgREST.Observation
import qualified Data.ByteString.Lazy as LBS
import qualified Data.Text.Encoding as T
import qualified Hasql.Pool as SQL
import qualified Hasql.Connection as SQL
import qualified Network.Socket as NS
import Numeric (showFFloat)
import qualified PostgREST.Error as Error
Expand All @@ -36,8 +37,9 @@ data Observation
| ConnectionRetryObs Int
| ConnectionPgVersionErrorObs SQL.UsageError
| DBListenerStart Text
| DBListenerFailNoRecoverObs
| DBListenerFailRecoverObs Text
| DBListenerFail Text SQL.ConnectionError
| DBListenerFailNoRecoverObs Text (Either SomeException ())
| DBListenerFailRecoverObs Text (Either SomeException ())
| ConfigReadErrorObs
| ConfigReadErrorFatalObs SQL.UsageError Text
| ConfigReadErrorNotFatalObs SQL.UsageError
Expand Down Expand Up @@ -81,10 +83,12 @@ observationMessage = \case
jsonMessage usageErr
DBListenerStart channel -> do
"Listening for notifications on the " <> channel <> " channel"
DBListenerFailNoRecoverObs ->
"Automatic recovery disabled, exiting."
DBListenerFailRecoverObs channel ->
"Retrying listening for notifications on the " <> channel <> " channel.."
DBListenerFail channel err -> do
"Could not listen for notifications on the " <> channel <> " channel. " <> show err
DBListenerFailNoRecoverObs channel err ->
showListenerError err <> ". Automatic recovery disabled on the " <> channel <> " channel"
DBListenerFailRecoverObs channel err ->
showListenerError err <> ". Retrying listening for notifications on the " <> channel <> " channel.."
ConfigReadErrorObs ->
"An error ocurred when trying to query database settings for the config parameters"
ConfigReadErrorFatalObs usageErr hint ->
Expand All @@ -106,3 +110,7 @@ observationMessage = \case
showMillis x = toS $ showFFloat (Just 1) (x * 1000) ""

jsonMessage err = T.decodeUtf8 . LBS.toStrict . Error.errorPayload $ Error.PgError False err

showListenerError :: (Either SomeException ()) -> Text
showListenerError (Left e) = show e
showListenerError (Right _) = "Failed getting notifications" -- this should not happen as the listener will never finish with a Right result

0 comments on commit 34bb8f2

Please sign in to comment.