39 lines
1.9 KiB
Haskell
39 lines
1.9 KiB
Haskell
module Jobs.Handler.PruneFiles
|
|
( dispatchJobPruneSessionFiles
|
|
, dispatchJobPruneUnreferencedFiles
|
|
) where
|
|
|
|
import Import hiding (matching)
|
|
|
|
import Database.Persist.Sql (deleteWhereCount)
|
|
|
|
import qualified Database.Esqueleto as E
|
|
import qualified Database.Esqueleto.Utils as E
|
|
|
|
|
|
dispatchJobPruneSessionFiles :: JobHandler UniWorX
|
|
dispatchJobPruneSessionFiles = JobHandlerAtomic . hoist lift $ do
|
|
now <- liftIO getCurrentTime
|
|
expires <- getsYesod $ view _appSessionFilesExpire
|
|
n <- deleteWhereCount [ SessionFileTouched <. addUTCTime (- expires) now ]
|
|
$logInfoS "PruneSessionFiles" [st|Deleted #{n} expired session files|]
|
|
|
|
|
|
dispatchJobPruneUnreferencedFiles :: JobHandler UniWorX
|
|
dispatchJobPruneUnreferencedFiles = JobHandlerAtomic . hoist lift $ do
|
|
n <- E.deleteCount . E.from $ \fileContent ->
|
|
E.where_ . E.not_ . E.any E.exists $ references fileContent
|
|
$logInfoS "PruneUnreferencedFiles" [st|Deleted #{n} unreferenced files|]
|
|
where
|
|
references :: E.SqlExpr (Entity FileContent) -> [E.SqlQuery ()]
|
|
references (E.just . (E.^. FileContentHash) -> fHash) =
|
|
[ E.from $ \appFile -> E.where_ $ appFile E.^. CourseApplicationFileContent E.==. fHash
|
|
, E.from $ \matFile -> E.where_ $ matFile E.^. MaterialFileContent E.==. fHash
|
|
, E.from $ \newsFile -> E.where_ $ newsFile E.^. CourseNewsFileContent E.==. fHash
|
|
, E.from $ \sheetFile -> E.where_ $ sheetFile E.^. SheetFileContent E.==. fHash
|
|
, E.from $ \appInstr -> E.where_ $ appInstr E.^. CourseAppInstructionFileContent E.==. fHash
|
|
, E.from $ \matching -> E.where_ $ E.just (matching E.^. AllocationMatchingLog) E.==. fHash
|
|
, E.from $ \subFile -> E.where_ $ subFile E.^. SubmissionFileContent E.==. fHash
|
|
, E.from $ \sessFile -> E.where_ $ sessFile E.^. SessionFileContent E.==. fHash
|
|
]
|