Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make deleting successful jobs configurable #84

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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