Skip to content

Commit

Permalink
Merge pull request #13444 from guardian/dev-build-lifecycle
Browse files Browse the repository at this point in the history
Dev build lifecycle / Autorefresh
  • Loading branch information
alexduf committed Jun 30, 2016
2 parents 1582691 + 3598717 commit e64538f
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 25 deletions.
10 changes: 6 additions & 4 deletions article/app/services/NewspaperBooksAndSectionsAutoRefresh.scala
@@ -1,7 +1,9 @@
package services

import akka.actor.ActorSystem
import common.{LifecycleComponent, AutoRefresh}
import model.{TagDefinition, TagIndexListings}
import play.libs.Akka

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
Expand All @@ -25,7 +27,7 @@ trait NewspaperTags {
}
}

class NewspaperBookTagAgent extends AutoRefresh[TagIndexListings](0 seconds, 5 minutes) with NewspaperTags {
class NewspaperBookTagAgent(actorSystem: => ActorSystem) extends AutoRefresh[TagIndexListings](0 seconds, 5 minutes, actorSystem) with NewspaperTags {
override val source = "newspaper_books"
override protected def refresh(): Future[TagIndexListings] = Future {
blocking {
Expand All @@ -34,9 +36,9 @@ class NewspaperBookTagAgent extends AutoRefresh[TagIndexListings](0 seconds, 5 m
}
}

object NewspaperBookTagAgent extends NewspaperBookTagAgent
object NewspaperBookTagAgent extends NewspaperBookTagAgent(Akka.system())

class NewspaperBookSectionTagAgent extends AutoRefresh[TagIndexListings](0 seconds, 5 minutes) with NewspaperTags {
class NewspaperBookSectionTagAgent(actorSystem: => ActorSystem) extends AutoRefresh[TagIndexListings](0 seconds, 5 minutes, actorSystem) with NewspaperTags {
override val source = "newspaper_book_sections"
override protected def refresh(): Future[TagIndexListings] = Future {
blocking {
Expand All @@ -45,4 +47,4 @@ class NewspaperBookSectionTagAgent extends AutoRefresh[TagIndexListings](0 secon
}
}

object NewspaperBookSectionTagAgent extends NewspaperBookSectionTagAgent
object NewspaperBookSectionTagAgent extends NewspaperBookSectionTagAgent(Akka.system())
6 changes: 3 additions & 3 deletions common/app/common/AutoRefresh.scala
Expand Up @@ -4,14 +4,14 @@ import play.api.{Application, GlobalSettings}

import scala.concurrent.duration.FiniteDuration
import akka.agent.Agent
import akka.actor.Cancellable
import akka.actor.{ActorSystem, Cancellable}
import scala.concurrent.Future
import play.libs.Akka
import scala.util.{Failure, Success}
import scala.concurrent.ExecutionContext.Implicits.global

/** Simple class for repeatedly updating a value on a schedule */
abstract class AutoRefresh[A](initialDelay: FiniteDuration, interval: FiniteDuration) extends Logging {
abstract class AutoRefresh[A](initialDelay: FiniteDuration, interval: FiniteDuration, actorSystem: => ActorSystem = Akka.system()) extends Logging {
private lazy val agent = Agent[Option[A]](None)

@volatile private var subscription: Option[Cancellable] = None
Expand All @@ -28,7 +28,7 @@ abstract class AutoRefresh[A](initialDelay: FiniteDuration, interval: FiniteDura
final def start() = {
log.info(s"Starting refresh cycle after $initialDelay repeatedly over $interval delay")

subscription = Some(Akka.system.scheduler.schedule(initialDelay, interval) {
subscription = Some(actorSystem.scheduler.schedule(initialDelay, interval) {
refresh() onComplete {
case Success(a) =>
log.debug(s"Updated AutoRefresh: $a")
Expand Down
14 changes: 9 additions & 5 deletions common/app/contentapi/SectionsLookUpLifecycle.scala
@@ -1,31 +1,35 @@
package contentapi

import common.{LifecycleComponent, AkkaAsync, Logging, Jobs}
import common._
import play.api.inject.ApplicationLifecycle

import scala.concurrent.{Future, ExecutionContext}

class SectionsLookUpLifecycle(appLifecycle: ApplicationLifecycle)(implicit ec: ExecutionContext) extends LifecycleComponent with Logging {
class SectionsLookUpLifecycle(
appLifecycle: ApplicationLifecycle,
jobs: JobScheduler = Jobs,
akkaAsync: AkkaAsync = AkkaAsync
)(implicit ec: ExecutionContext) extends LifecycleComponent with Logging {

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

private def scheduleJobs() {
Jobs.schedule("SectionsLookUpJob", "0 * * * * ?") {
jobs.schedule("SectionsLookUpJob", "0 * * * * ?") {
SectionsLookUp.refresh()
}
}

private def descheduleJobs() {
Jobs.deschedule("SectionsLookUpJob")
jobs.deschedule("SectionsLookUpJob")
}

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

AkkaAsync {
akkaAsync.after1s {
SectionsLookUp.refresh()
}
}
Expand Down
12 changes: 8 additions & 4 deletions common/app/headlines/ABHeadlines.scala
Expand Up @@ -84,20 +84,24 @@ object ABHeadlines extends ExecutionContexts with Logging {
private def isUsFront(req: RequestHeader) = req.path == "/us"
}

class ABHeadlinesLifecycle(appLifeCycle: ApplicationLifecycle)(implicit ec: ExecutionContext) extends LifecycleComponent {
class ABHeadlinesLifecycle(
appLifeCycle: ApplicationLifecycle,
jobs: JobScheduler = Jobs,
akkaAsync: AkkaAsync = AkkaAsync
)(implicit ec: ExecutionContext) extends LifecycleComponent {

private val ABHeadlinesJob = "ABHeadlinesJob"

appLifeCycle.addStopHook { () => Future {
Jobs.deschedule(ABHeadlinesJob)
jobs.deschedule(ABHeadlinesJob)
}}

override def start(): Unit = {
//runs once a minute
Jobs.schedule(ABHeadlinesJob, "0 * * * * ?") {
jobs.schedule(ABHeadlinesJob, "0 * * * * ?") {
ABHeadlines.refresh()
}
AkkaAsync{
akkaAsync.after1s {
ABHeadlines.refresh()
}
}
Expand Down
6 changes: 3 additions & 3 deletions diagnostics/app/common/DiagnosticsLifecycle.scala
Expand Up @@ -4,20 +4,20 @@ import play.api.inject.ApplicationLifecycle

import scala.concurrent.{ExecutionContext, Future}

class DiagnosticsLifecycle(appLifecycle: ApplicationLifecycle)(implicit ec: ExecutionContext) extends LifecycleComponent with Logging {
class DiagnosticsLifecycle(appLifecycle: ApplicationLifecycle, jobs: JobScheduler = Jobs)(implicit ec: ExecutionContext) extends LifecycleComponent with Logging {

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

private def scheduleJobs() {
Jobs.schedule("DiagnosticsLoadJob", "0 * * * * ?") {
jobs.schedule("DiagnosticsLoadJob", "0 * * * * ?") {
model.diagnostics.analytics.UploadJob.run()
}
}

private def descheduleJobs() {
Jobs.deschedule("DiagnosticsLoadJob")
jobs.deschedule("DiagnosticsLoadJob")
}

override def start(): Unit = {
Expand Down
16 changes: 10 additions & 6 deletions onward/app/feed/MostReadLifecycle.scala
@@ -1,25 +1,29 @@
package feed

import common.{LifecycleComponent, AkkaAsync, Jobs}
import common.{JobScheduler, LifecycleComponent, AkkaAsync, Jobs}
import play.api.inject.ApplicationLifecycle

import scala.concurrent.{Future, ExecutionContext}

class MostReadLifecycle(appLifecycle: ApplicationLifecycle)(implicit ec: ExecutionContext) extends LifecycleComponent {
class MostReadLifecycle(
appLifecycle: ApplicationLifecycle,
jobs: JobScheduler = Jobs,
akkaAsync: AkkaAsync = AkkaAsync
)(implicit ec: ExecutionContext) extends LifecycleComponent {

appLifecycle.addStopHook { () => Future {
Jobs.deschedule("MostReadAgentRefreshJob")
jobs.deschedule("MostReadAgentRefreshJob")
}}

override def start(): Unit = {
Jobs.deschedule("MostReadAgentRefreshJob")
jobs.deschedule("MostReadAgentRefreshJob")

// update every 30 min
Jobs.schedule("MostReadAgentRefreshJob", "0 0/30 * * * ?") {
jobs.schedule("MostReadAgentRefreshJob", "0 0/30 * * * ?") {
MostReadAgent.update()
}

AkkaAsync {
akkaAsync.after1s {
MostReadAgent.update()
}
}
Expand Down

0 comments on commit e64538f

Please sign in to comment.