Skip to content

Commit

Permalink
Add scheduled cleaning job
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed Nov 21, 2023
1 parent 5a524c1 commit 8a1de0a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
8 changes: 5 additions & 3 deletions app/src/main/scala/App.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ object App extends IOApp.Simple:
res <- AppResources.instance(config.redis)
lilaClient = LilaClient(res.redisPubsub)
executor <- Resource.eval(Executor.instance(lilaClient))
job = RedisSubscriberJob(executor, res.redisPubsub)
httpApi = HttpApi(executor, HealthCheck())
workListenerJob = RedisSubscriberJob(executor, res.redisPubsub)
cleanJob = CleanJob(executor)
httpApi = HttpApi(executor, HealthCheck())
server <- MkHttpServer.apply.newEmber(config.server, httpApi.httpApp)
_ <- job.run().background
_ <- workListenerJob.run().background
_ <- cleanJob.run().background
_ <- Resource.eval(Logger[IO].info(s"Starting server on ${config.server.host}:${config.server.port}"))
yield ()
13 changes: 13 additions & 0 deletions app/src/main/scala/RedisSubscriberJob.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import io.chrisdavenport.rediculous.RedisPubSub
import org.typelevel.log4cats.Logger
import Lila.Request

import scala.concurrent.duration.*

trait RedisSubscriberJob:
def run(): IO[Unit]

Expand All @@ -33,3 +35,14 @@ object RedisSubscriberJob:
case Some(request) => executor.add(request)
case None => Logger[IO].error(s"Failed to parse message: $msg"),
) *> pubsub.runMessages

trait CleanJob:
def run(): IO[Unit]

object CleanJob:
def apply(executor: Executor)(using Logger[IO]): CleanJob =
new CleanJob:
def run(): IO[Unit] =
Logger[IO].info("Start cleaning job") *>
IO.sleep(5.seconds) *>
IO.realTimeInstant.flatMap(executor.clean)
2 changes: 1 addition & 1 deletion app/src/test/scala/ExecutorTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ object ExecutorTest extends SimpleIOSuite:
moves <- ref.get
yield assert(moves.isEmpty)

test("after timeout should move can be acruired again"):
test("after timeout move should be able to acquired again"):
for
ref <- Ref.of[IO, List[Lila.Move]](Nil)
client = createLilaClient(ref)
Expand Down

0 comments on commit 8a1de0a

Please sign in to comment.