Skip to content

Commit

Permalink
Make deleting successful jobs configurable (#84)
Browse files Browse the repository at this point in the history
* Make deleting successful jobs configurable

* Fix newjob not being saved

Co-authored-by: Jappie Klooster <[email protected]>
  • Loading branch information
ivb-supercede and jappeace authored May 12, 2022
1 parent 33039f3 commit 72e942d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/OddJobs/ConfigBuilder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ mkConfig logger tname dbpool ccControl jrunner configOverridesFn =
, cfgConcurrencyControl = ccControl
, cfgJobType = defaultJobType
, cfgDefaultJobTimeout = Seconds 600
, cfgDeleteSuccessfulJobs = True
}
in cfg

Expand Down
11 changes: 9 additions & 2 deletions src/OddJobs/Job.hs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ import Database.PostgreSQL.Simple.ToField as PGS (toField)
class (MonadUnliftIO m, MonadBaseControl IO m) => HasJobRunner m where
getPollingInterval :: m Seconds
onJobSuccess :: Job -> m ()
deleteSuccessfulJobs :: m Bool
onJobFailed :: m [JobErrHandler]
getJobRunner :: m (Job -> IO ())
getDbPool :: m (Pool Connection)
Expand Down Expand Up @@ -169,9 +170,12 @@ logCallbackErrors jid msg action = catchAny action $ \e -> log LevelError $ LogT
instance HasJobRunner RunnerM where
getPollingInterval = cfgPollingInterval . envConfig <$> ask
onJobFailed = cfgOnJobFailed . envConfig <$> ask

onJobSuccess job = do
fn <- cfgOnJobSuccess . envConfig <$> ask
logCallbackErrors (jobId job) "onJobSuccess" $ liftIO $ fn job
deleteSuccessfulJobs = cfgDeleteSuccessfulJobs . envConfig <$> ask

getJobRunner = cfgJobRunner . envConfig <$> ask
getDbPool = cfgDbPool . envConfig <$> ask
getTableName = cfgTableName . envConfig <$> ask
Expand Down Expand Up @@ -387,8 +391,11 @@ runJob jid = do
(flip catches) [Handler $ timeoutHandler job startTime, Handler $ exceptionHandler job startTime] $ do
runJobWithTimeout lockTimeout job
endTime <- liftIO getCurrentTime
deleteJob jid
let newJob = job{jobStatus=Success, jobLockedBy=Nothing, jobLockedAt=Nothing}
shouldDeleteJob <- deleteSuccessfulJobs
let newJob = job{jobStatus=Success, jobLockedBy=Nothing, jobLockedAt=Nothing, jobUpdatedAt = endTime}
if shouldDeleteJob
then deleteJob jid
else void $ saveJob newJob
log LevelInfo $ LogJobSuccess newJob (diffUTCTime endTime startTime)
onJobSuccess newJob
pure ()
Expand Down
3 changes: 3 additions & 0 deletions src/OddJobs/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,9 @@ data Config = Config
-- | How long can a job run after which it is considered to be "crashed" and
-- picked up for execution again
, cfgDefaultJobTimeout :: Seconds

-- | Should successful jobs be deleted from the queue to save on table space?
, cfgDeleteSuccessfulJobs :: Bool
}


Expand Down

0 comments on commit 72e942d

Please sign in to comment.