Server fetches and saves files. Also proper error handling

This commit is contained in:
Guntis Smaukstelis
2023-04-23 22:41:38 +03:00
parent 0622177572
commit 31a56bc0d4
6 changed files with 159 additions and 64 deletions
+27
View File
@@ -0,0 +1,27 @@
package fetch
import cats.effect.IO
import cats.effect.unsafe.implicits.global
import cats.implicits.toTraverseOps
import db.DBService
import java.time.{LocalDate, LocalDateTime}
object Main {
def run: IO[Unit] = {
val from = LocalDateTime.of(2023, 4, 23, 20, 0)
val to = LocalDateTime.of(2023, 4, 23, 23, 30)
for {
fetched <- FetchService.fetchInRange(from, to)
// fetched <- FetchService.fetchFromDate(LocalDate.of(2023, 4, 23))
(errors, successfulDownloads) = fetched.partitionMap(identity)
_ <- IO.println(s"Fetch errors: $errors")
saveResults <- successfulDownloads.traverse { case (name, content) => DBService.save(name, content) }
_ <- IO.println(s"Save results: $saveResults")
} yield ()
}
def main(args: Array[String]): Unit = {
run.unsafeRunSync()
}
}