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

Commercial bundle updater #27578

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions article/app/AppLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.softwaremill.macwire._
import common.Assets.DiscussionExternalAssetsLifecycle
import common._
import common.dfp.DfpAgentLifecycle
import common.commercial.CommercialBundleUrlLifecycle
import concurrent.BlockingOperations
import conf.{CachedHealthCheckLifeCycle, Configuration}
import conf.switches.SwitchboardLifecycle
Expand Down Expand Up @@ -46,6 +47,7 @@ trait AppComponents extends FrontendComponents with ArticleControllers {
override lazy val lifecycleComponents = List(
wire[NewspaperBooksAndSectionsAutoRefresh],
wire[DfpAgentLifecycle],
wire[CommercialBundleUrlLifecycle],
wire[CloudWatchMetricsLifecycle],
wire[SurgingContentAgentLifecycle],
wire[SwitchboardLifecycle],
Expand Down
2 changes: 2 additions & 0 deletions commercial/app/AppLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import commercial.model.merchandise.jobs.{Industries, JobsAgent}
import commercial.model.merchandise.travel.TravelOffersAgent
import common.CloudWatchMetricsLifecycle
import common.dfp.DfpAgentLifecycle
import common.commercial.CommercialBundleUrlLifecycle
import conf.switches.SwitchboardLifecycle
import conf.CachedHealthCheckLifeCycle
import contentapi.{CapiHttpClient, ContentApiClient, HttpClient}
Expand Down Expand Up @@ -55,6 +56,7 @@ trait AppComponents extends FrontendComponents with CommercialControllers with C
override lazy val lifecycleComponents = List(
wire[CommercialLifecycle],
wire[DfpAgentLifecycle],
wire[CommercialBundleUrlLifecycle],
wire[SwitchboardLifecycle],
wire[CloudWatchMetricsLifecycle],
wire[CachedHealthCheckLifeCycle],
Expand Down
44 changes: 44 additions & 0 deletions common/app/common/commercial/CommercialBundleUrlAgent.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package common.commercial

import conf.Configuration.commercial._
import services.S3
import play.api.libs.json.Json
import common.GuLogging

case class AssetMap(
commercialStandalone: Option[String],
)

object AssetMapParser {
def apply(assetMap: String): Option[AssetMap] = {
val assetMapJson = Json.parse(assetMap)
Some(
AssetMap(
(assetMapJson \ "commercial-standalone.js").asOpt[String],
),
)
}
}

object CommercialBundleUrlAgent extends GuLogging {

var bundlePath: String = getBundlePath()

def getAssetMap(): AssetMap = {
S3.get(commercialAssetMap) flatMap (AssetMapParser(
_,
)) getOrElse AssetMap(None)
}

def getBundlePath(): String = {
getAssetMap().commercialStandalone match {
case Some(p) => "test_commercial_bundles/" + p
case None => "javascripts/commercial/graun.standalone.commercial.js"
}
}

def refresh(): Unit = {
bundlePath = getBundlePath()
log.logger.info(s"Commercial bundle path is now $bundlePath")
}
}
32 changes: 32 additions & 0 deletions common/app/common/commercial/CommercialBundleUrlLifecycle.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package common.commercial

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

import scala.concurrent.{ExecutionContext, Future}

class CommercialBundleUrlLifecycle(appLifeCycle: ApplicationLifecycle, jobs: JobScheduler, pekkoAsync: PekkoAsync)(
implicit ec: ExecutionContext,
) extends LifecycleComponent {

appLifeCycle.addStopHook { () =>
Future {
jobs.deschedule("CommercialBundleUrlRefreshJob")
}
}

def refreshCommercialBundleUrl(): Unit = CommercialBundleUrlAgent.refresh()

override def start(): Unit = {
jobs.deschedule("CommercialBundleUrlRefreshJob")
jobs.scheduleEveryNMinutes("CommercialBundleUrlRefreshJob", 1) {
refreshCommercialBundleUrl()
Future.successful(())
}

pekkoAsync.after1s {
refreshCommercialBundleUrl()
}
}
}
7 changes: 4 additions & 3 deletions common/app/common/configuration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import common.Environment.{app, awsRegion, stage}
import conf.{Configuration, Static}
import org.apache.commons.io.IOUtils
import services.ParameterStore
import model.dotcomrendering.DotcomRenderingUtils.assetURL

import scala.jdk.CollectionConverters._
import scala.concurrent.duration._
import scala.util.{Failure, Success, Try}
import common.commercial.CommercialBundleUrlAgent

class BadConfigurationException(msg: String) extends RuntimeException(msg)

Expand Down Expand Up @@ -524,9 +526,8 @@ class GuardianConfiguration extends GuLogging {
lazy val prebidServerUrl =
configuration.getStringProperty("commercial.prebid.server.url") getOrElse "http://localhost:8000"

lazy val overrideCommercialBundleUrl: Option[String] =
if (environment.isDev) configuration.getStringProperty("commercial.overrideCommercialBundleUrl")
else None
lazy val commercialAssetMap = s"$commercialRoot/assets-map/assets.map"
lazy val commercialBundleUrl = assetURL(CommercialBundleUrlAgent.bundlePath)
}

object journalism {
Expand Down
3 changes: 1 addition & 2 deletions common/app/views/support/JavaScriptPage.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ object JavaScriptPage {
val ipsos = if (page.metadata.isFront) getScriptTag(page.metadata.id) else getScriptTag(page.metadata.sectionId)

val commercialBundleUrl = JsString(
Configuration.commercial.overrideCommercialBundleUrl
.getOrElse(assetURL("javascripts/commercial/graun.standalone.commercial.js")),
Configuration.commercial.commercialBundleUrl,
)

javascriptConfig ++ config ++ commercialMetaData ++ journalismMetaData ++ Map(
Expand Down
2 changes: 2 additions & 0 deletions dev-build/app/AppLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import cricket.conf.CricketLifecycle
import cricket.controllers.CricketControllers
import dev.DevAssetsController
import dfp.DfpDataCacheLifecycle
import common.commercial.CommercialBundleUrlLifecycle
import feed._
import football.controllers._
import http.{CorsHttpErrorHandler, DevBuildParametersHttpRequestHandler, DevFilters}
Expand Down Expand Up @@ -91,6 +92,7 @@ trait AppComponents
wire[OnwardJourneyLifecycle],
wire[CommercialLifecycle],
wire[DfpDataCacheLifecycle],
wire[CommercialBundleUrlLifecycle],
wire[FaciaDfpAgentLifecycle],
wire[ConfigAgentLifecycle],
wire[SurgingContentAgentLifecycle],
Expand Down
2 changes: 2 additions & 0 deletions facia/app/AppLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import app.{FrontendApplicationLoader, FrontendBuildInfo, FrontendComponents}
import com.softwaremill.macwire._
import common._
import common.dfp.FaciaDfpAgentLifecycle
import common.commercial.CommercialBundleUrlLifecycle
import concurrent.BlockingOperations
import conf.switches.SwitchboardLifecycle
import conf.CachedHealthCheckLifeCycle
Expand Down Expand Up @@ -54,6 +55,7 @@ trait AppComponents extends FrontendComponents with FaciaControllers with FapiSe
wire[ConfigAgentLifecycle],
wire[CloudWatchMetricsLifecycle],
wire[FaciaDfpAgentLifecycle],
wire[CommercialBundleUrlLifecycle],
wire[SurgingContentAgentLifecycle],
wire[IndexListingsLifecycle],
wire[SwitchboardLifecycle],
Expand Down
2 changes: 2 additions & 0 deletions preview/app/AppLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import commercial.CommercialLifecycle
import commercial.controllers.CommercialControllers
import commercial.targeting.TargetingLifecycle
import common.dfp.FaciaDfpAgentLifecycle
import common.commercial.CommercialBundleUrlLifecycle
import common.{ApplicationMetrics, CloudWatchMetricsLifecycle, ContentApiMetrics, DCRMetrics}
import conf.switches.SwitchboardLifecycle
import conf.{CachedHealthCheckLifeCycle, FootballLifecycle}
Expand Down Expand Up @@ -53,6 +54,7 @@ trait PreviewLifecycleComponents
wire[OnwardJourneyLifecycle],
wire[ConfigAgentLifecycle],
wire[FaciaDfpAgentLifecycle],
wire[CommercialBundleUrlLifecycle],
wire[SwitchboardLifecycle],
wire[FootballLifecycle],
wire[CricketLifecycle],
Expand Down
Loading