From 1abb12d92a0d9ab13a0e39145cfaa91bf2f90896 Mon Sep 17 00:00:00 2001 From: Guntis Smaukstelis Date: Thu, 25 May 2023 13:51:45 +0300 Subject: [PATCH] Moved to app/Main, scheduler helper methods moved to unfinished test --- build.sbt | 2 +- src/main/scala/{server => app}/Main.scala | 4 +++- src/main/scala/fetch/FetchService.scala | 1 - src/main/scala/fetch/Scheduler.scala | 22 +------------------- src/test/scala/fetch/SchedulerSpec.scala | 25 +++++++++++++++++++++++ 5 files changed, 30 insertions(+), 24 deletions(-) rename src/main/scala/{server => app}/Main.scala (95%) create mode 100644 src/test/scala/fetch/SchedulerSpec.scala diff --git a/build.sbt b/build.sbt index 2cbeb3d..0f1c1ef 100644 --- a/build.sbt +++ b/build.sbt @@ -5,7 +5,7 @@ ThisBuild / scalaVersion := "2.13.10" lazy val root = (project in file(".")) .settings( name := "WeatherTool", - Compile / mainClass := Some("server.Main") + Compile / mainClass := Some("app.Main") ) val http4sVersion = "0.23.18" diff --git a/src/main/scala/server/Main.scala b/src/main/scala/app/Main.scala similarity index 95% rename from src/main/scala/server/Main.scala rename to src/main/scala/app/Main.scala index f0f28bb..aa8253f 100644 --- a/src/main/scala/server/Main.scala +++ b/src/main/scala/app/Main.scala @@ -1,8 +1,10 @@ -package server +package app + import cats.effect._ import cats.implicits.catsSyntaxTuple2Parallel import db.{DBService, DataService} import fetch.{FetchService, FileFetchScheduler} +import server.Server object Main extends IOApp { def run(args: List[String]): IO[ExitCode] = { diff --git a/src/main/scala/fetch/FetchService.scala b/src/main/scala/fetch/FetchService.scala index 2ce3818..6166070 100644 --- a/src/main/scala/fetch/FetchService.scala +++ b/src/main/scala/fetch/FetchService.scala @@ -12,7 +12,6 @@ import pureconfig._ import pureconfig.generic.auto._ import java.time.{LocalDate, LocalDateTime} -import scala.concurrent.ExecutionContext.global final case class WeatherServerConfig( username: String, diff --git a/src/main/scala/fetch/Scheduler.scala b/src/main/scala/fetch/Scheduler.scala index c710a93..02b3af3 100644 --- a/src/main/scala/fetch/Scheduler.scala +++ b/src/main/scala/fetch/Scheduler.scala @@ -1,7 +1,6 @@ package fetch import cats.effect._ -import cats.effect.unsafe.implicits.global import cats.implicits.catsSyntaxApply import fs2.Stream import org.typelevel.log4cats.Logger @@ -22,10 +21,6 @@ object Scheduler { class Scheduler(log: Logger[IO]) { private val downloadMinute = 31 -// private def testTask: IO[Unit] = { -// log.info("Running task") -// } - def durationToNextHalfHour(implicit clock: Clock[IO]): IO[FiniteDuration] = { clock.realTime.map { duration => val now = LocalTime.ofSecondOfDay((duration.toMillis / 1000) % (24 * 60 * 60)) @@ -37,12 +32,6 @@ class Scheduler(log: Logger[IO]) { } } -// def run(implicit clock: Clock[IO]): Stream[IO, Nothing] = { -// Stream.eval(durationToNextHalfHour).flatMap { delay => -// (Stream.sleep[IO](delay) ++ Stream.awakeEvery[IO](1.hour)).evalMap(_ => testTask).drain -// } -// } - def scheduleTask(task: IO[Either[Throwable, (String, String)]]): Stream[IO, Either[Throwable, (String, String)]] = { Stream.eval(durationToNextHalfHour).flatMap { delay => { Stream.eval(log.info(s"Scheduler started with delay: ${delay.toMinutes} min")) *> @@ -50,13 +39,4 @@ class Scheduler(log: Logger[IO]) { .evalMap(_ => task) }} } - - // This is just for testing - def main(args: Array[String]): Unit = { -// run.compile.drain.unsafeRunSync() - for { - fetch <- FetchService.of - fetchTask = new FileNameService().generateCurrentHour.flatMap(fetch.fetchSingleFile) - } yield scheduleTask(fetchTask).compile.drain.unsafeRunSync() - } -} +} \ No newline at end of file diff --git a/src/test/scala/fetch/SchedulerSpec.scala b/src/test/scala/fetch/SchedulerSpec.scala new file mode 100644 index 0000000..c21e1f6 --- /dev/null +++ b/src/test/scala/fetch/SchedulerSpec.scala @@ -0,0 +1,25 @@ +package fetch + +import cats.effect.{Clock, IO} +import fs2.Stream + +class SchedulerSpec { +// private def testTask: IO[Unit] = { +// log.info("Running task") +// } +// +// def run(implicit clock: Clock[IO]): Stream[IO, Nothing] = { +// Stream.eval(durationToNextHalfHour).flatMap { delay => +// (Stream.sleep[IO](delay) ++ Stream.awakeEvery[IO](1.hour)).evalMap(_ => testTask).drain +// } +// } +// +// def main(args: Array[String]): Unit = { +// // run.compile.drain.unsafeRunSync() +// for { +// scheduler <- Scheduler.of +// fetch <- FetchService.of +// fetchTask = new FileNameService().generateCurrentHour.flatMap(fetch.fetchSingleFile) +// } yield scheduler.scheduleTask(fetchTask).compile.drain.unsafeRunSync() +// } +}