Skip to content

Commit

Permalink
using withTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
saurabhnanda committed Oct 6, 2023
1 parent cd62c6b commit 4d894ee
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions src/OddJobs/Job.hs
Original file line number Diff line number Diff line change
Expand Up @@ -836,14 +836,14 @@ scheduleJob conn tname payload runAt = do

type ResourceList = [(ResourceId, Int)]

createJobWithResources
:: ToJSON p
=> Connection
-> TableName
-> ResourceCfg
-> p
-> ResourceList
-> IO Job
createJobWithResources ::
ToJSON p =>
Connection ->
TableName ->
ResourceCfg ->
p ->
ResourceList ->
IO Job
createJobWithResources conn tname resCfg payload resources = do
t <- getCurrentTime
scheduleJobWithResources conn tname resCfg payload resources t
Expand All @@ -857,10 +857,9 @@ scheduleJobWithResources
-> ResourceList
-> UTCTime
-> IO Job
scheduleJobWithResources conn tname ResourceCfg{..} payload resources runAt = do
scheduleJobWithResources conn tname ResourceCfg{..} payload resources runAt = PGS.withTransaction conn $ do
-- We insert everything in a single transaction to delay @NOTIFY@ calls,
-- so a job isn't picked up before its resources are inserted.
PGS.begin conn
let args = ( tname, runAt, Queued, toJSON payload, Nothing :: Maybe Value, 0 :: Int, Nothing :: Maybe Text, Nothing :: Maybe Text )
queryFormatter = toS <$> PGS.formatQuery conn createJobQuery args
rs <- PGS.query conn createJobQuery args
Expand All @@ -874,8 +873,6 @@ scheduleJobWithResources conn tname ResourceCfg{..} payload resources runAt = do
void $ PGS.execute conn ensureResource (resCfgResourceTable, rawResourceId resourceId, resCfgDefaultLimit)
void $ PGS.execute conn registerResourceUsage (resCfgUsageTable, jobId job, rawResourceId resourceId, usage)

PGS.commit conn

pure job

-- getRunnerEnv :: (HasJobRunner m) => m RunnerEnv
Expand Down

0 comments on commit 4d894ee

Please sign in to comment.