Delete old grib files more than 9hours old

This commit is contained in:
Guntis Smaukstelis
2025-02-11 11:33:27 +02:00
parent cf69372204
commit 09449b5675
2 changed files with 34 additions and 3 deletions
+17
View File
@@ -1,6 +1,7 @@
package data
import cats.effect.IO
import cats.implicits.toTraverseOps
import fs2.io.file.{Files, Path}
import grib.{Grib, GribParser}
@@ -48,6 +49,22 @@ object DataService {
}
}
def deleteOldForecasts(maxHours: Int = 9): IO[List[String]] = {
val nowUTC = ZonedDateTime.now(ZoneOffset.UTC)
val oldThreshold = nowUTC.minusHours(maxHours)
for {
fileList <- getFileList()
fileDateList = fileList.flatMap(fileName =>
getTimeFromName(fileName).map(extracted => (fileName, extracted._1))
)
deleteList = fileDateList.filter(_._2.isBefore(oldThreshold)).map(_._1)
_ <- deleteList.traverse(name => Files[IO].delete(Path(s"$FOLDER/${name}")))
// TODO change to log
_ <- deleteList.traverse(name => IO.println(s"delete: $name"))
} yield deleteList
}
private def getTimeFromName(filename: String): Option[(ZonedDateTime, ZonedDateTime)] = {
Try {
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HHmm'Z'").withZone(ZoneId.of("UTC"))