@@ -245,8 +245,8 @@ concatJobDbColumns = concatJobDbColumns_ jobDbColumns ""
245245 concatJobDbColumns_ (col:cols) x = concatJobDbColumns_ cols (x <> col <> ", ")
246246
247247
248−findJobByIdQuery :: TableName -> PGS.Query
249−findJobByIdQuery tname = "SELECT " <> concatJobDbColumns <> " FROM " <> tname <> " WHERE id = ?"
248+findJobByIdQuery :: PGS.Query
249+findJobByIdQuery = "SELECT " <> concatJobDbColumns <> " FROM ? WHERE id = ?"
250250
251251withDbConnection :: (HasJobRunner m)
252252 => (Connection -> m a)
@@ -275,17 +275,17 @@ findJobById jid = do
275275 withDbConnection $ \conn -> liftIO $ findJobByIdIO conn tname jid
276276
277277findJobByIdIO :: Connection -> TableName -> JobId -> IO (Maybe Job)
278−findJobByIdIO conn tname jid = PGS.query conn (findJobByIdQuery tname) (Only jid) >>= \case
278+findJobByIdIO conn tname jid = PGS.query conn findJobByIdQuery (tname, jid) >>= \case
279279 [] -> pure Nothing
280280 [j] -> pure (Just j)
281281 js -> Prelude.error $ "Not expecting to find multiple jobs by id=" <> (show jid)
282282
283283
284−saveJobQuery :: TableName -> PGS.Query
285−saveJobQuery tname = "UPDATE " <> tname <> " set run_at = ?, status = ?, payload = ?, last_error = ?, attempts = ?, locked_at = ?, locked_by = ? WHERE id = ? RETURNING " <> concatJobDbColumns
284+saveJobQuery :: PGS.Query
285+saveJobQuery = "UPDATE ? set run_at = ?, status = ?, payload = ?, last_error = ?, attempts = ?, locked_at = ?, locked_by = ? WHERE id = ? RETURNING " <> concatJobDbColumns
286286
287−deleteJobQuery :: TableName -> PGS.Query
288−deleteJobQuery tname = "DELETE FROM " <> tname <> " WHERE id = ?"
287+deleteJobQuery :: PGS.Query
288+deleteJobQuery = "DELETE FROM ? WHERE id = ?"
289289
290290saveJob :: (HasJobRunner m) => Job -> m Job
291291saveJob j = do
@@ -294,8 +294,9 @@ saveJob j = do
294294
295295saveJobIO :: Connection -> TableName -> Job -> IO Job
296296saveJobIO conn tname Job{jobRunAt, jobStatus, jobPayload, jobLastError, jobAttempts, jobLockedBy, jobLockedAt, jobId} = do
297− rs <- PGS.query conn (saveJobQuery tname)
298− ( jobRunAt
297+ rs <- PGS.query conn saveJobQuery
298+ ( tname
299+ , jobRunAt
299300 , jobStatus
300301 , jobPayload
301302 , jobLastError
@@ -316,7 +317,7 @@ deleteJob jid = do
316317
317318deleteJobIO :: Connection -> TableName -> JobId -> IO ()
318319deleteJobIO conn tname jid = do
319− void $ PGS.execute conn (deleteJobQuery tname) (Only jid)
320+ void $ PGS.execute conn deleteJobQuery (tname, jid)
320321
321322runJobNowIO :: Connection -> TableName -> JobId -> IO (Maybe Job)
322323runJobNowIO conn tname jid = do
@@ -327,9 +328,9 @@ runJobNowIO conn tname jid = do
327328-- and somehow send an uninterruptibleCancel to that thread.
328329unlockJobIO :: Connection -> TableName -> JobId -> IO (Maybe Job)
329330unlockJobIO conn tname jid = do
330− fmap listToMaybe $ PGS.query conn q (Retry, jid, In [Locked])
331+ fmap listToMaybe $ PGS.query conn q (tname, Retry, jid, In [Locked])
331332 where
332− q = "update " <> tname <> " set status=?, run_at=now(), locked_at=null, locked_by=null where id=? and status in ? returning " <> concatJobDbColumns
333+ q = "update ? set status=?, run_at=now(), locked_at=null, locked_by=null where id=? and status in ? returning " <> concatJobDbColumns
333334
334335cancelJobIO :: Connection -> TableName -> JobId -> IO (Maybe Job)
335336cancelJobIO conn tname jid =
@@ -340,9 +341,9 @@ updateJobHelper :: TableName
340341 -> (Status, [Status], Maybe UTCTime, JobId)
341342 -> IO (Maybe Job)
342343updateJobHelper tname conn (newStatus, existingStates, mRunAt, jid) =
343− fmap listToMaybe $ PGS.query conn q (newStatus, runAt, jid, PGS.In existingStates)
344+ fmap listToMaybe $ PGS.query conn q (tname, newStatus, runAt, jid, PGS.In existingStates)
344345 where
345− q = "update " <> tname <> " set attempts=0, status=?, run_at=? where id=? and status in ? returning " <> concatJobDbColumns
346+ q = "update ? set attempts=0, status=?, run_at=? where id=? and status in ? returning " <> concatJobDbColumns
346347 runAt = case mRunAt of
347348 Nothing -> PGS.toField $ PGS.Identifier "run_at"
348349 Just t -> PGS.toField t
@@ -453,8 +454,8 @@ jobMonitor = do
453454 liftIO $ Dir.removePathForcibly f
454455
455456-- | Ref: 'jobPoller'
456−jobPollingSql :: TableName -> Query
457−jobPollingSql tname = "update " <> tname <> " set status = ?, locked_at = ?, locked_by = ?, attempts=attempts+1 WHERE id in (select id from " <> tname <> " where (run_at<=? AND ((status in ?) OR (status = ? and locked_at<?))) ORDER BY run_at ASC LIMIT 1 FOR UPDATE) RETURNING id"
457+jobPollingSql :: Query
458+jobPollingSql = "update ? set status = ?, locked_at = ?, locked_by = ?, attempts=attempts+1 WHERE id in (select id from ? where (run_at<=? AND ((status in ?) OR (status = ? and locked_at<?))) ORDER BY run_at ASC LIMIT 1 FOR UPDATE) RETURNING id"
458459
459460waitForJobs :: (HasJobRunner m)
460461 => m ()
@@ -508,8 +509,16 @@ jobPoller = do
508509 log LevelDebug $ LogText $ toS $ "[" <> processName <> "] Polling the job queue.."
509510 t <- liftIO getCurrentTime
510511 r <- liftIO $
511− PGS.query pollerDbConn (jobPollingSql tname)
512− (Locked, t, processName, t, (In [Queued, Retry]), Locked, (addUTCTime (fromIntegral $ negate $ unSeconds lockTimeout) t))
512+ PGS.query pollerDbConn jobPollingSql
513+ ( tname
514+ , Locked
515+ , t
516+ , processName
517+ , tname
518+ , t
519+ , (In [Queued, Retry])
520+ , Locked
521+ , (addUTCTime (fromIntegral $ negate $ unSeconds lockTimeout) t))
513522 case r of
514523 -- When we don't have any jobs to run, we can relax a bit...
515524 [] -> pure delayAction
@@ -537,8 +546,8 @@ jobEventListener = do
537546 concurrencyControlFn <- getConcurrencyControlFn
538547
539548 let tryLockingJob jid = do
540− let q = "UPDATE " <> tname <> " SET status=?, locked_at=now(), locked_by=?, attempts=attempts+1 WHERE id=? AND status in ? RETURNING id"
541− (withDbConnection $ \conn -> (liftIO $ PGS.query conn q (Locked, jwName, jid, In [Queued, Retry]))) >>= \case
549+ let q = "UPDATE ? SET status=?, locked_at=now(), locked_by=?, attempts=attempts+1 WHERE id=? AND status in ? RETURNING id"
550+ (withDbConnection $ \conn -> (liftIO $ PGS.query conn q (tname, Locked, jwName, jid, In [Queued, Retry]))) >>= \case
542551 [] -> do
543552 log LevelDebug $ LogText $ toS $ "Job was locked by someone else before I could start. Skipping it. JobId=" <> show jid
544553 pure Nothing
@@ -546,7 +555,7 @@ jobEventListener = do
546555 x -> error $ "WTF just happned? Was expecting a single row to be returned, received " ++ (show x)
547556
548557 withResource pool $ \monitorDbConn -> do
549− void $ liftIO $ PGS.execute monitorDbConn ("LISTEN " <> pgEventName tname) ()
558+ void $ liftIO $ PGS.execute monitorDbConn ("LISTEN ?") (Only $ pgEventName tname)
550559 forever $ do
551560 log LevelDebug $ LogText "[LISTEN/NOFIFY] Event loop"
552561 notif <- liftIO $ getNotification monitorDbConn
@@ -583,8 +592,8 @@ jobEventListener = do
583592
584593
585594
586−createJobQuery :: TableName -> PGS.Query
587−createJobQuery tname = "INSERT INTO " <> tname <> "(run_at, status, payload, last_error, attempts, locked_at, locked_by) VALUES (?, ?, ?, ?, ?, ?, ?) RETURNING " <> concatJobDbColumns
595+createJobQuery :: PGS.Query
596+createJobQuery = "INSERT INTO ? (run_at, status, payload, last_error, attempts, locked_at, locked_by) VALUES (?, ?, ?, ?, ?, ?, ?) RETURNING " <> concatJobDbColumns
588597
589598-- $createJobs
590599--
@@ -623,9 +632,9 @@ scheduleJob :: ToJSON p
623632 -> UTCTime -- ^ when should the job be executed
624633 -> IO Job
625634scheduleJob conn tname payload runAt = do
626− let args = ( runAt, Queued, toJSON payload, Nothing :: Maybe Value, 0 :: Int, Nothing :: Maybe Text, Nothing :: Maybe Text )
627− queryFormatter = toS <$> (PGS.formatQuery conn (createJobQuery tname) args)
628− rs <- PGS.query conn (createJobQuery tname) args
635+ let args = ( tname, runAt, Queued, toJSON payload, Nothing :: Maybe Value, 0 :: Int, Nothing :: Maybe Text, Nothing :: Maybe Text )
636+ queryFormatter = toS <$> (PGS.formatQuery conn createJobQuery args)
637+ rs <- PGS.query conn createJobQuery args
629638 case rs of
630639 [] -> (Prelude.error . (<> "Not expecting a blank result set when creating a job. Query=")) <$> queryFormatter
631640 [r] -> pure r
@@ -684,5 +693,5 @@ fetchAllJobRunners :: (MonadIO m)
684693 => Config
685694 -> m [JobRunnerName]
686695fetchAllJobRunners Config{cfgTableName, cfgDbPool} = liftIO $ withResource cfgDbPool $ \conn -> do
687− fmap (mapMaybe fromOnly) $ PGS.query_ conn $ "select distinct locked_by from " <> cfgTableName
696+ fmap (mapMaybe fromOnly) $ PGS.query conn "select distinct locked_by from ?" (Only cfgTableName)
688697