Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add template for ElectionResultsAgent #27193

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions facia-press/app/AppLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import conf.switches.SwitchboardLifecycle
import contentapi.{CapiHttpClient, ContentApiClient, HttpClient}
import controllers.{Application, HealthCheck}
import frontpress.{DraftFapiFrontPress, FrontPressCron, LiveFapiFrontPress, ToolPressQueueWorker}
import lifecycle.FaciaPressLifecycle
import lifecycle.{ElectionResultsAgentLifecycle, FaciaPressLifecycle}
import model.ApplicationIdentity
import play.api.ApplicationLoader.Context
import play.api.routing.Router
import play.api._
import play.api.mvc.EssentialFilter
import services.ConfigAgentLifecycle
import services.{ConfigAgentLifecycle, ElectionResultsAgent}
import router.Routes
import _root_.commercial.targeting.TargetingLifecycle
import org.apache.pekko.actor.{ActorSystem => PekkoActorSystem}
Expand All @@ -32,6 +32,8 @@ trait AppComponents extends FrontendComponents {
lazy val toolPressQueueWorker = wire[ToolPressQueueWorker]
lazy val frontPressCron = wire[FrontPressCron]

lazy val electionResultsAgent = wire[ElectionResultsAgent]

lazy val healthCheck = wire[HealthCheck]
lazy val applicationController: Application = wire[Application]
lazy val logbackOperationsPool = wire[LogbackOperationsPool]
Expand All @@ -43,6 +45,7 @@ trait AppComponents extends FrontendComponents {
wire[CloudWatchMetricsLifecycle],
wire[FaciaPressLifecycle],
wire[TargetingLifecycle],
wire[ElectionResultsAgentLifecycle],
)

lazy val router: Router = wire[Routes]
Expand Down
45 changes: 45 additions & 0 deletions facia-press/app/lifecycle/ElectionResultsAgentLifecycle.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package lifecycle

import app.LifecycleComponent
import common.{GuLogging, JobScheduler, PekkoAsync}
import play.api.inject.ApplicationLifecycle
import services.ElectionResultsAgent

import scala.concurrent.duration.DurationInt
import scala.concurrent.{ExecutionContext, Future}

class ElectionResultsAgentLifecycle(
applicationLifecycle: ApplicationLifecycle,
jobs: JobScheduler,
pekkoAsync: PekkoAsync,
electionResultsAgent: ElectionResultsAgent,
)(implicit ec: ExecutionContext)
extends LifecycleComponent
with GuLogging {

applicationLifecycle.addStopHook { () =>
Future {
descheduleJobs()
}
}

override def start(): Unit = {
descheduleJobs()
scheduleJobs()

pekkoAsync.after1s {
electionResultsAgent.refresh()
}
}

private def scheduleJobs(): Unit = {
// This job runs every 2 minutes
jobs.scheduleEvery("ElectionResultsJob", 2.minutes) {
electionResultsAgent.refresh()
}
}

private def descheduleJobs(): Unit = {
jobs.deschedule("ElectionResultsJob")
}
}
17 changes: 17 additions & 0 deletions facia-press/app/services/ElectionResultsAgent.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package services

import common.{Box, GuLogging}

import scala.concurrent.Future

class ElectionResultsAgent extends GuLogging {

private lazy val electionResults = Box[Map[String, String]](Map.empty)

def refresh(): Future[Unit] = {
log.info("Fetching election data")
Future.successful(())
}

def getResults: Map[String, String] = electionResults.get()
}
13 changes: 13 additions & 0 deletions facia-press/test/services/ElectionResultsAgentTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package services

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class ElectionResultsAgentTest extends AnyFlatSpec with Matchers {

it should "initialise with results being an empty Seq" in {
val electionResultsAgent = new ElectionResultsAgent()
electionResultsAgent.getResults shouldBe Map.empty
}

}