{- |Module : PGQueuer.Core.MonadDescription : Abstract typeclass for database queue operations.Copyright : (c) Tushar Adhatrao, 2026License : MITMaintainer : tusharadhatrao@gmail.comStability : experimentalDefines the 'MonadPGQueuer' typeclass which serves as the contractfor any database engine backend. This abstraction enables swappingout low-level database clients without modifying the concurrencyor worker loop infrastructure.-}module PGQueuer.Core.Monad ( MonadPGQueuer (..) )whereimport Data.Aeson (Value)import qualified Data.ByteString.Lazy as BLimport Data.Int (Int32)import Data.Text (Text)import Data.Time (NominalDiffTime, UTCTime)import Data.UUID (UUID)import PGQueuer.Types{- | Abstract interface for database-backed queue operations.Any backend that implements this typeclass provides the fulllifecycle management for jobs: enqueueing, dequeueing withlock-free concurrency via @FOR UPDATE SKIP LOCKED@, statuslogging, heartbeat maintenance, retry logic, and transactionalboundaries.-}class (Monad m) => MonadPGQueuer m where {- | Enqueue a single job with optional payload, priority, delay, deduplication key, and headers. Returns the assigned 't:JobId's. -} enqueue :: Entrypoint -> Maybe BL.ByteString -> Int -> Maybe NominalDiffTime -> Maybe Text -> Maybe Value -> Maybe JobId -> Maybe Value -> JobStatus -> m [JobId] {- | Dequeue a batch of jobs matching the given entrypoint parameters. Uses @FOR UPDATE SKIP LOCKED@ internally to avoid contention. -} dequeue :: -- | Batch size Int -> -- | Per-entrypoint execution parameters [EntrypointExecutionParameter] -> -- | Queue manager identifier UUID -> -- | Global concurrency limit Maybe Int -> -- | Heartbeat timeout in seconds Int -> m [Job] {- | Atomically update job statuses and append entries to the audit log table. -} logJobs :: [(JobId, JobStatus, Maybe Value)] -> m () {- | Refresh heartbeat timestamps for the given active jobs. Only updates the @heartbeat@ column (HOT-friendly). -} updateHeartbeat :: [JobId] -> m () {- | Retry failed jobs in bulk. Provides a list of (JobId, newExecuteAfter, newAttempts). -} retryJobs :: [(JobId, UTCTime, Int32)] -> m () {- | Query completed children results for a parent job. Returns a list of 'Data.Aeson.Value' payloads or tracebacks. -} mergedChildResults :: JobId -> m [Value] {- | Execute an action within a database transaction boundary. Provides @BEGIN@, @COMMIT@ on success, and @ROLLBACK@ on exception. -} withTransaction :: m a -> m a {- | Insert a cron schedule if it does not already exist. Matches the Python implementation's @insert_schedule@ with ON CONFLICT DO NOTHING. -} insertSchedule :: CronExpression -> Entrypoint -> m () {- | Fetch due schedules safely using @FOR UPDATE SKIP LOCKED@. Atomically transitions them to 'Picked' status and returns the 't:Schedule' records. -} fetchSchedules :: m [Schedule] {- | Reset a schedule's status back to 'Queued' after its job has been dispatched. Sets last_run = NOW() and updates next_run to the provided 'UTCTime'. -} setScheduleQueued :: ScheduleId -> UTCTime -> m () {- | Get the earliest next_run among queued schedules. Used by the cron scheduler to determine how long to sleep. -} getEarliestNextRun :: m (Maybe UTCTime)