Add conf into IO and handle IO throwable

This commit is contained in:
Guntis Smaukstelis
2023-05-01 16:43:21 +03:00
parent 36f97a7448
commit 7e3d58bec0
4 changed files with 41 additions and 30 deletions
+14 -9
View File
@@ -9,16 +9,21 @@ 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)
val from = LocalDateTime.of(2023, 4, 28, 10, 0)
val to = LocalDateTime.of(2023, 4, 28, 13, 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 ()
// fetchResultEither <- FetchService.fetchFromDate(LocalDate.of(2023, 4, 28)).attempt
fetchResultEither <- FetchService.fetchInRange(from, to).attempt
fetchServiceError = fetchResultEither.left.toOption.map(e => s"FetchServiceError: ${e.getMessage}").toList
fetchResult = fetchResultEither.getOrElse(List.empty)
(fetchErrors, successDownloads) = fetchResult.partitionMap(identity)
saveResults <- successDownloads.traverse { case (name, content) => DBService.save(name, content) }
(saveErrors, successSaves) = saveResults.partitionMap(identity)
successes = successDownloads.map(s => s"fetched: ${s._1}") ++ successSaves.map(s => s"saved: $s")
errors = fetchServiceError ++ fetchErrors.map(e => s"FetchError: ${e.getMessage}") ++ saveErrors.map(e => s"SaveError: ${e.getMessage}")
_ <- IO.println(s"errors: $errors")
_ <- IO.println(s"successes: $successes")
} yield (successes, errors)
}
def main(args: Array[String]): Unit = {