packages feed

pgqueuer-hs-0.1.0.0 / src / PGQueuer / Listener.hs

{-# LANGUAGE OverloadedStrings #-}{- |Module      : PGQueuer.ListenerDescription : PostgreSQL LISTEN/NOTIFY event listener and event publisher.Copyright   : (c) Tushar Adhatrao, 2026License     : MITMaintainer  : tusharadhatrao@gmail.comStability   : experimentalProvides functions for subscribing to PostgreSQL asynchronous notifications(@LISTEN@ / @UNLISTEN@) and publishing cancellation or health check signals.-}module PGQueuer.Listener  ( listen  , unlisten  , unlistenAll  , getNextNotification  , notifyJobCancellation  , notifyHealthCheck  , parseEventPayload  ) whereimport Control.Monad (void)import Data.Aeson ((.=))import qualified Data.Aeson as Aesonimport qualified Data.ByteString.Char8 as BSimport qualified Data.ByteString.Lazy as BLimport qualified Data.Text as Timport qualified Data.Text.Encoding as TEimport Data.Time (getCurrentTime)import Data.UUID.V4 (nextRandom)import Database.PostgreSQL.Simple (Connection, Only (..), execute, execute_)import Database.PostgreSQL.Simple.Notification (Notification, getNotification)import Database.PostgreSQL.Simple.Types (Query (..))import PGQueuer.Types (Channel (..), defaultChannel)-- | Listen for notifications on a specific channellisten :: Connection -> T.Text -> IO ()listen conn channel =  void $ execute_ conn (Query $ TE.encodeUtf8 ("LISTEN " <> channel))-- | Stop listening on a specific channelunlisten :: Connection -> T.Text -> IO ()unlisten conn channel =  void $ execute_ conn (Query $ TE.encodeUtf8 ("UNLISTEN " <> channel))-- | Stop listening on all channelsunlistenAll :: Connection -> IO ()unlistenAll conn =  void $ execute_ conn (Query "UNLISTEN *;")-- | Get the next notification, blocking until availablegetNextNotification :: Connection -> IO (Maybe Notification)getNextNotification conn = Just <$> getNotification connsendNotification :: Connection -> Aeson.Value -> IO ()sendNotification conn payload =  void $ execute conn "NOTIFY pgqueuer, ?" (Only payloadText)  where    payloadText = TE.decodeUtf8 . BL.toStrict $ Aeson.encode payloadnotificationChannel :: T.TextnotificationChannel = channelText  where    Channel channelText = defaultChannel-- | Send a job cancellation notificationnotifyJobCancellation :: Connection -> Int -> IO ()notifyJobCancellation conn jobId = do  sentAt <- getCurrentTime  sendNotification    conn    ( Aeson.object        [ "channel" .= notificationChannel        , "sent_at" .= sentAt        , "type" .= ("cancellation_event" :: T.Text)        , "ids" .= ([jobId] :: [Int])        ]    )-- | Send a health check notificationnotifyHealthCheck :: Connection -> IO ()notifyHealthCheck conn = do  sentAt <- getCurrentTime  healthCheckId <- nextRandom  sendNotification    conn    ( Aeson.object        [ "channel" .= notificationChannel        , "sent_at" .= sentAt        , "type" .= ("health_check_event" :: T.Text)        , "id" .= healthCheckId        ]    )-- | Parse event payload from notificationparseEventPayload :: BS.ByteString -> Either String StringparseEventPayload payload =  case Aeson.eitherDecodeStrict' payload :: Either String Aeson.Value of    Left err -> Left err    Right _ -> Right (BS.unpack payload)