From b46a904ca14d05104481f81f50bb2ba91bf46d7e Mon Sep 17 00:00:00 2001 From: Jake Lee Kennedy Date: Mon, 2 Sep 2024 16:05:55 +0100 Subject: [PATCH 1/5] WIP --- admin/app/AppLoader.scala | 2 + applications/app/AppLoader.scala | 2 + article/app/AppLoader.scala | 2 + commercial/app/AppLoader.scala | 2 + .../commercial/CommercialBundleUrlAgent.scala | 48 +++++++++++++++++++ .../CommercialBundleUrlLifecycle.scala | 32 +++++++++++++ common/app/common/configuration.scala | 5 +- common/app/model/meta.scala | 5 ++ common/app/views/support/JavaScriptPage.scala | 3 +- dev-build/app/AppLoader.scala | 2 + facia/app/AppLoader.scala | 2 + preview/app/AppLoader.scala | 2 + 12 files changed, 102 insertions(+), 5 deletions(-) create mode 100644 common/app/common/commercial/CommercialBundleUrlAgent.scala create mode 100644 common/app/common/commercial/CommercialBundleUrlLifecycle.scala diff --git a/admin/app/AppLoader.scala b/admin/app/AppLoader.scala index eef272c9585d..34908e144c63 100644 --- a/admin/app/AppLoader.scala +++ b/admin/app/AppLoader.scala @@ -6,6 +6,7 @@ import common._ import conf.switches.SwitchboardLifecycle import controllers.{AdminControllers, HealthCheck} import _root_.dfp.DfpDataCacheLifecycle +import _root_.commercial.CommercialBundleUrlLifecycle import org.apache.pekko.actor.{ActorSystem => PekkoActorSystem} import concurrent.BlockingOperations import contentapi.{CapiHttpClient, ContentApiClient, HttpClient} @@ -85,6 +86,7 @@ trait AppComponents extends FrontendComponents with AdminControllers with AdminS wire[DfpAgentLifecycle], wire[DfpDataCacheLifecycle], wire[CommercialDfpReportingLifecycle], + wire[CommercialBundleUrlLifecycle], ) lazy val router: Router = wire[Routes] diff --git a/applications/app/AppLoader.scala b/applications/app/AppLoader.scala index 4dae2fc8336a..e8747f7cb076 100644 --- a/applications/app/AppLoader.scala +++ b/applications/app/AppLoader.scala @@ -2,6 +2,7 @@ import org.apache.pekko.actor.{ActorSystem => PekkoActorSystem} import app.{FrontendApplicationLoader, FrontendBuildInfo, FrontendComponents} import com.softwaremill.macwire._ import common.dfp.DfpAgentLifecycle +import common.commercial.CommercialBundleUrlLifecycle import common.{ApplicationMetrics, CloudWatchMetricsLifecycle, ContentApiMetrics, DCRMetrics, EmailSubsciptionMetrics} import _root_.commercial.targeting.TargetingLifecycle import common.Assets.DiscussionExternalAssetsLifecycle @@ -55,6 +56,7 @@ trait AppComponents extends FrontendComponents with ApplicationsControllers with wire[ConfigAgentLifecycle], wire[CloudWatchMetricsLifecycle], wire[DfpAgentLifecycle], + wire[CommercialBundleUrlLifecycle], wire[SurgingContentAgentLifecycle], wire[IndexListingsLifecycle], wire[SectionsLookUpLifecycle], diff --git a/article/app/AppLoader.scala b/article/app/AppLoader.scala index c85eea8f120e..af7a822de36b 100644 --- a/article/app/AppLoader.scala +++ b/article/app/AppLoader.scala @@ -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 @@ -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], diff --git a/commercial/app/AppLoader.scala b/commercial/app/AppLoader.scala index 994dcd796627..891dccfe47c5 100644 --- a/commercial/app/AppLoader.scala +++ b/commercial/app/AppLoader.scala @@ -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} @@ -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], diff --git a/common/app/common/commercial/CommercialBundleUrlAgent.scala b/common/app/common/commercial/CommercialBundleUrlAgent.scala new file mode 100644 index 000000000000..958f8a942760 --- /dev/null +++ b/common/app/common/commercial/CommercialBundleUrlAgent.scala @@ -0,0 +1,48 @@ +package common.commercial + +import scala.concurrent.ExecutionContext +import common.Environment.{app, awsRegion, stage} +import services.ParameterStore +import services.S3 + import scala.concurrent.Future + import play.api.libs.json.Json +import conf.Configuration +import play.api.libs.ws.{WSClient, WSResponse} + +object CommercialBundleUrlAgent { + val wsClient: WSClient + + var bundlePath: String + + private val frontendAssetUrl = Configuration.assets.fullURL(common.Environment.stage) + + private val commercialAssetMap = s"$frontendAssetUrl/javascripts/commercial/assets.map" + + private def commercialStandaloneAssetUrl(assetMap: String): String = { + val assetMapJson = Json.parse(assetMap) + (assetMapJson \ "commercial-standalone.js").as[String] + } + + private def fetchAssetMap()(implicit executionContext: ExecutionContext): Future[String] = { + wsClient.url(commercialAssetMap).get().map { response => + response.status match { + case 200 => response.body + case _ => throw new Exception(s"Failed to fetch commercial asset map: ${response.status} ${response.statusText}") + } + } + } + + private def getCommercialBundleUrl(implicit executionContext: ExecutionContext): Future[String] = { + fetchAssetMap().map(commercialStandaloneAssetUrl) + } + + def updateBundlePath()(implicit executionContext: ExecutionContext): Unit = { + getCommercialBundleUrl.map { url => + bundlePath = url + } + } + + def refresh()(implicit executionContext: ExecutionContext): Unit = { + updateBundlePath() + } +} diff --git a/common/app/common/commercial/CommercialBundleUrlLifecycle.scala b/common/app/common/commercial/CommercialBundleUrlLifecycle.scala new file mode 100644 index 000000000000..5e93831a66fb --- /dev/null +++ b/common/app/common/commercial/CommercialBundleUrlLifecycle.scala @@ -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() + } + } +} diff --git a/common/app/common/configuration.scala b/common/app/common/configuration.scala index 208a1f33200b..88fd3d3b80ae 100644 --- a/common/app/common/configuration.scala +++ b/common/app/common/configuration.scala @@ -12,6 +12,7 @@ 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._ @@ -526,9 +527,7 @@ 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 commercialBundleUrl = configuration.getStringProperty("commercial.commercialBundleUrl").getOrElse(assetURL("javascripts/commercial/graun.standalone.commercial.js")) } object journalism { diff --git a/common/app/model/meta.scala b/common/app/model/meta.scala index 138898a81ea9..fc777229ad7c 100644 --- a/common/app/model/meta.scala +++ b/common/app/model/meta.scala @@ -22,12 +22,14 @@ import play.api.libs.json.JodaWrites.JodaDateTimeWrites import play.api.libs.functional.syntax._ import play.api.mvc.RequestHeader import navigation.GuardianFoundationHelper +import common.commercial.CommercialBundleUrlAgent import scala.util.matching.Regex import utils.ShortUrls import java.time.{OffsetDateTime, ZoneId, ZoneOffset} import model.ApiContent2Is +import model.dotcomrendering.DotcomRenderingUtils.assetURL object Commercial { @@ -356,6 +358,9 @@ case class MetaData( def hasLiveBlogTopAd(request: RequestHeader, content: Option[Content]): Boolean = DfpAgent.hasLiveBlogTopAd(this, content.map(_.tags.tags).getOrElse(Seq.empty), request) + def commercialBundleUrl: String = + assetURL(CommercialBundleUrlAgent.bundlePath) + def omitMPUsFromContainers(edition: Edition): Boolean = if (isPressedPage) { DfpAgent.omitMPUsFromContainers(id, edition) diff --git a/common/app/views/support/JavaScriptPage.scala b/common/app/views/support/JavaScriptPage.scala index aaac7739e866..3b7aa852a971 100644 --- a/common/app/views/support/JavaScriptPage.scala +++ b/common/app/views/support/JavaScriptPage.scala @@ -70,8 +70,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")), + page.metadata.commercialBundleUrl ) javascriptConfig ++ config ++ commercialMetaData ++ journalismMetaData ++ Map( diff --git a/dev-build/app/AppLoader.scala b/dev-build/app/AppLoader.scala index 540a7227d168..cbb66b5d6786 100644 --- a/dev-build/app/AppLoader.scala +++ b/dev-build/app/AppLoader.scala @@ -17,6 +17,7 @@ import cricket.conf.CricketLifecycle import cricket.controllers.CricketControllers import dev.DevAssetsController import dfp.DfpDataCacheLifecycle +import commercial.CommercialBundleUrlLifecycle import feed._ import football.controllers._ import http.{CorsHttpErrorHandler, DevBuildParametersHttpRequestHandler, DevFilters} @@ -91,6 +92,7 @@ trait AppComponents wire[OnwardJourneyLifecycle], wire[CommercialLifecycle], wire[DfpDataCacheLifecycle], + wire[CommercialBundleUrlLifecycle], wire[FaciaDfpAgentLifecycle], wire[ConfigAgentLifecycle], wire[SurgingContentAgentLifecycle], diff --git a/facia/app/AppLoader.scala b/facia/app/AppLoader.scala index e1677a9c5c29..5922c2bd6dfb 100644 --- a/facia/app/AppLoader.scala +++ b/facia/app/AppLoader.scala @@ -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 @@ -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], diff --git a/preview/app/AppLoader.scala b/preview/app/AppLoader.scala index 73b9df1b38c5..ae2c6a706c9c 100644 --- a/preview/app/AppLoader.scala +++ b/preview/app/AppLoader.scala @@ -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} @@ -53,6 +54,7 @@ trait PreviewLifecycleComponents wire[OnwardJourneyLifecycle], wire[ConfigAgentLifecycle], wire[FaciaDfpAgentLifecycle], + wire[CommercialBundleUrlLifecycle], wire[SwitchboardLifecycle], wire[FootballLifecycle], wire[CricketLifecycle], From 42b00d63011ad7703f1beea14f4de72dd146584f Mon Sep 17 00:00:00 2001 From: Jake Lee Kennedy Date: Fri, 1 Nov 2024 16:08:13 +0000 Subject: [PATCH 2/5] read assetmap from s3 --- admin/app/AppLoader.scala | 2 - applications/app/AppLoader.scala | 2 - .../commercial/CommercialBundleUrlAgent.scala | 54 +++++++++---------- common/app/common/configuration.scala | 4 +- .../DotcomRenderingUtils.scala | 8 +-- common/app/model/meta.scala | 5 -- common/app/views/support/JavaScriptPage.scala | 2 +- dev-build/app/AppLoader.scala | 2 +- 8 files changed, 36 insertions(+), 43 deletions(-) diff --git a/admin/app/AppLoader.scala b/admin/app/AppLoader.scala index 34908e144c63..eef272c9585d 100644 --- a/admin/app/AppLoader.scala +++ b/admin/app/AppLoader.scala @@ -6,7 +6,6 @@ import common._ import conf.switches.SwitchboardLifecycle import controllers.{AdminControllers, HealthCheck} import _root_.dfp.DfpDataCacheLifecycle -import _root_.commercial.CommercialBundleUrlLifecycle import org.apache.pekko.actor.{ActorSystem => PekkoActorSystem} import concurrent.BlockingOperations import contentapi.{CapiHttpClient, ContentApiClient, HttpClient} @@ -86,7 +85,6 @@ trait AppComponents extends FrontendComponents with AdminControllers with AdminS wire[DfpAgentLifecycle], wire[DfpDataCacheLifecycle], wire[CommercialDfpReportingLifecycle], - wire[CommercialBundleUrlLifecycle], ) lazy val router: Router = wire[Routes] diff --git a/applications/app/AppLoader.scala b/applications/app/AppLoader.scala index e8747f7cb076..4dae2fc8336a 100644 --- a/applications/app/AppLoader.scala +++ b/applications/app/AppLoader.scala @@ -2,7 +2,6 @@ import org.apache.pekko.actor.{ActorSystem => PekkoActorSystem} import app.{FrontendApplicationLoader, FrontendBuildInfo, FrontendComponents} import com.softwaremill.macwire._ import common.dfp.DfpAgentLifecycle -import common.commercial.CommercialBundleUrlLifecycle import common.{ApplicationMetrics, CloudWatchMetricsLifecycle, ContentApiMetrics, DCRMetrics, EmailSubsciptionMetrics} import _root_.commercial.targeting.TargetingLifecycle import common.Assets.DiscussionExternalAssetsLifecycle @@ -56,7 +55,6 @@ trait AppComponents extends FrontendComponents with ApplicationsControllers with wire[ConfigAgentLifecycle], wire[CloudWatchMetricsLifecycle], wire[DfpAgentLifecycle], - wire[CommercialBundleUrlLifecycle], wire[SurgingContentAgentLifecycle], wire[IndexListingsLifecycle], wire[SectionsLookUpLifecycle], diff --git a/common/app/common/commercial/CommercialBundleUrlAgent.scala b/common/app/common/commercial/CommercialBundleUrlAgent.scala index 958f8a942760..949c1e104b7e 100644 --- a/common/app/common/commercial/CommercialBundleUrlAgent.scala +++ b/common/app/common/commercial/CommercialBundleUrlAgent.scala @@ -2,47 +2,47 @@ package common.commercial import scala.concurrent.ExecutionContext import common.Environment.{app, awsRegion, stage} +import conf.Configuration.commercial._ import services.ParameterStore import services.S3 - import scala.concurrent.Future - import play.api.libs.json.Json +import scala.concurrent.Future +import play.api.libs.json.Json import conf.Configuration import play.api.libs.ws.{WSClient, WSResponse} +import common.GuLogging -object CommercialBundleUrlAgent { - val wsClient: WSClient +case class AssetMap( + commercialStandalone: Option[String], +) - var bundlePath: String - - private val frontendAssetUrl = Configuration.assets.fullURL(common.Environment.stage) - - private val commercialAssetMap = s"$frontendAssetUrl/javascripts/commercial/assets.map" - - private def commercialStandaloneAssetUrl(assetMap: String): String = { +object AssetMapParser { + def apply(assetMap: String): Option[AssetMap] = { val assetMapJson = Json.parse(assetMap) - (assetMapJson \ "commercial-standalone.js").as[String] + Some(AssetMap( + (assetMapJson \ "commercial-standalone.js").asOpt[String], + )) } +} - private def fetchAssetMap()(implicit executionContext: ExecutionContext): Future[String] = { - wsClient.url(commercialAssetMap).get().map { response => - response.status match { - case 200 => response.body - case _ => throw new Exception(s"Failed to fetch commercial asset map: ${response.status} ${response.statusText}") - } - } - } +object CommercialBundleUrlAgent extends GuLogging { + + var bundlePath: String = getBundlePath() - private def getCommercialBundleUrl(implicit executionContext: ExecutionContext): Future[String] = { - fetchAssetMap().map(commercialStandaloneAssetUrl) + def getAssetMap(): AssetMap = { + S3.get(commercialAssetMap) flatMap (AssetMapParser( + _, + )) getOrElse(throw new RuntimeException(s"Failed to load commercial bundle asset map from $commercialAssetMap")) } - def updateBundlePath()(implicit executionContext: ExecutionContext): Unit = { - getCommercialBundleUrl.map { url => - bundlePath = url + def getBundlePath(): String = { + getAssetMap().commercialStandalone match { + case Some(p) => "test_commercial_bundles/" + p + case None => "javascripts/commercial/graun.standalone.commercial.js" } } - def refresh()(implicit executionContext: ExecutionContext): Unit = { - updateBundlePath() + def refresh(): Unit = { + bundlePath = getBundlePath() + log.logger.info(s"Commercial bundle path is now $bundlePath") } } diff --git a/common/app/common/configuration.scala b/common/app/common/configuration.scala index 88398d6b257b..1b883c565904 100644 --- a/common/app/common/configuration.scala +++ b/common/app/common/configuration.scala @@ -17,6 +17,7 @@ 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) @@ -525,7 +526,8 @@ class GuardianConfiguration extends GuLogging { lazy val prebidServerUrl = configuration.getStringProperty("commercial.prebid.server.url") getOrElse "http://localhost:8000" - lazy val commercialBundleUrl = configuration.getStringProperty("commercial.commercialBundleUrl").getOrElse(assetURL("javascripts/commercial/graun.standalone.commercial.js")) + lazy val commercialAssetMap = s"$commercialRoot/assets-map/assets.map" + lazy val commercialBundleUrl = assetURL(CommercialBundleUrlAgent.bundlePath) } object journalism { diff --git a/common/app/model/dotcomrendering/DotcomRenderingUtils.scala b/common/app/model/dotcomrendering/DotcomRenderingUtils.scala index 10b70491d263..e0446dbb9eba 100644 --- a/common/app/model/dotcomrendering/DotcomRenderingUtils.scala +++ b/common/app/model/dotcomrendering/DotcomRenderingUtils.scala @@ -120,11 +120,11 @@ object DotcomRenderingUtils { def assetURL(bundlePath: String): String = { // This function exists because for some reasons `Static` behaves differently in { PROD and CODE } versus LOCAL - if (Configuration.environment.isProd || Configuration.environment.isCode) { + // if (Configuration.environment.isProd || Configuration.environment.isCode) { Static(bundlePath) - } else { - s"${Configuration.site.host}${Static(bundlePath)}" - } + // } else { + // s"${Configuration.site.host}${Static(bundlePath)}" + // } } // note: this is duplicated in the onward service (DotcomponentsOnwardsModels - if duplicating again consider moving to common! :() diff --git a/common/app/model/meta.scala b/common/app/model/meta.scala index 505e44136bc2..831aab3c618c 100644 --- a/common/app/model/meta.scala +++ b/common/app/model/meta.scala @@ -22,14 +22,12 @@ import play.api.libs.json.JodaWrites.JodaDateTimeWrites import play.api.libs.functional.syntax._ import play.api.mvc.RequestHeader import navigation.GuardianFoundationHelper -import common.commercial.CommercialBundleUrlAgent import scala.util.matching.Regex import utils.ShortUrls import java.time.{OffsetDateTime, ZoneId, ZoneOffset} import model.ApiContent2Is -import model.dotcomrendering.DotcomRenderingUtils.assetURL object Commercial { @@ -357,9 +355,6 @@ case class MetaData( def hasLiveBlogTopAd(request: RequestHeader, content: Option[Content]): Boolean = DfpAgent.hasLiveBlogTopAd(this, content.map(_.tags.tags).getOrElse(Seq.empty), request) - def commercialBundleUrl: String = - assetURL(CommercialBundleUrlAgent.bundlePath) - def hasSurveyAd(request: RequestHeader): Boolean = DfpAgent.hasSurveyAd(fullAdUnitPath, this, request) diff --git a/common/app/views/support/JavaScriptPage.scala b/common/app/views/support/JavaScriptPage.scala index dc694b99bfd8..9e3f9237d16a 100644 --- a/common/app/views/support/JavaScriptPage.scala +++ b/common/app/views/support/JavaScriptPage.scala @@ -71,7 +71,7 @@ object JavaScriptPage { val ipsos = if (page.metadata.isFront) getScriptTag(page.metadata.id) else getScriptTag(page.metadata.sectionId) val commercialBundleUrl = JsString( - page.metadata.commercialBundleUrl + Configuration.commercial.commercialBundleUrl ) javascriptConfig ++ config ++ commercialMetaData ++ journalismMetaData ++ Map( diff --git a/dev-build/app/AppLoader.scala b/dev-build/app/AppLoader.scala index 0dd39166653c..838c622fc54e 100644 --- a/dev-build/app/AppLoader.scala +++ b/dev-build/app/AppLoader.scala @@ -17,7 +17,7 @@ import cricket.conf.CricketLifecycle import cricket.controllers.CricketControllers import dev.DevAssetsController import dfp.DfpDataCacheLifecycle -import commercial.CommercialBundleUrlLifecycle +import common.commercial.CommercialBundleUrlLifecycle import feed._ import football.controllers._ import http.{CorsHttpErrorHandler, DevBuildParametersHttpRequestHandler, DevFilters} From afff1391d08b9f1ae82178d8a2d41ade0f19a332 Mon Sep 17 00:00:00 2001 From: Jake Lee Kennedy Date: Fri, 1 Nov 2024 16:10:25 +0000 Subject: [PATCH 3/5] format --- article/app/AppLoader.scala | 2 +- commercial/app/AppLoader.scala | 2 +- .../commercial/CommercialBundleUrlAgent.scala | 14 ++++++++------ .../commercial/CommercialBundleUrlLifecycle.scala | 4 ++-- .../dotcomrendering/DotcomRenderingUtils.scala | 8 ++++---- common/app/views/support/JavaScriptPage.scala | 2 +- facia/app/AppLoader.scala | 2 +- preview/app/AppLoader.scala | 2 +- 8 files changed, 19 insertions(+), 17 deletions(-) diff --git a/article/app/AppLoader.scala b/article/app/AppLoader.scala index af7a822de36b..b2e6c3e23fac 100644 --- a/article/app/AppLoader.scala +++ b/article/app/AppLoader.scala @@ -47,7 +47,7 @@ trait AppComponents extends FrontendComponents with ArticleControllers { override lazy val lifecycleComponents = List( wire[NewspaperBooksAndSectionsAutoRefresh], wire[DfpAgentLifecycle], - wire[CommercialBundleUrlLifecycle], + wire[CommercialBundleUrlLifecycle], wire[CloudWatchMetricsLifecycle], wire[SurgingContentAgentLifecycle], wire[SwitchboardLifecycle], diff --git a/commercial/app/AppLoader.scala b/commercial/app/AppLoader.scala index 891dccfe47c5..d283d87c2756 100644 --- a/commercial/app/AppLoader.scala +++ b/commercial/app/AppLoader.scala @@ -56,7 +56,7 @@ trait AppComponents extends FrontendComponents with CommercialControllers with C override lazy val lifecycleComponents = List( wire[CommercialLifecycle], wire[DfpAgentLifecycle], - wire[CommercialBundleUrlLifecycle], + wire[CommercialBundleUrlLifecycle], wire[SwitchboardLifecycle], wire[CloudWatchMetricsLifecycle], wire[CachedHealthCheckLifeCycle], diff --git a/common/app/common/commercial/CommercialBundleUrlAgent.scala b/common/app/common/commercial/CommercialBundleUrlAgent.scala index 949c1e104b7e..965179e62667 100644 --- a/common/app/common/commercial/CommercialBundleUrlAgent.scala +++ b/common/app/common/commercial/CommercialBundleUrlAgent.scala @@ -12,15 +12,17 @@ import play.api.libs.ws.{WSClient, WSResponse} import common.GuLogging case class AssetMap( - commercialStandalone: Option[String], + commercialStandalone: Option[String], ) object AssetMapParser { def apply(assetMap: String): Option[AssetMap] = { val assetMapJson = Json.parse(assetMap) - Some(AssetMap( - (assetMapJson \ "commercial-standalone.js").asOpt[String], - )) + Some( + AssetMap( + (assetMapJson \ "commercial-standalone.js").asOpt[String], + ), + ) } } @@ -31,13 +33,13 @@ object CommercialBundleUrlAgent extends GuLogging { def getAssetMap(): AssetMap = { S3.get(commercialAssetMap) flatMap (AssetMapParser( _, - )) getOrElse(throw new RuntimeException(s"Failed to load commercial bundle asset map from $commercialAssetMap")) + )) getOrElse (throw new RuntimeException(s"Failed to load commercial bundle asset map from $commercialAssetMap")) } def getBundlePath(): String = { getAssetMap().commercialStandalone match { case Some(p) => "test_commercial_bundles/" + p - case None => "javascripts/commercial/graun.standalone.commercial.js" + case None => "javascripts/commercial/graun.standalone.commercial.js" } } diff --git a/common/app/common/commercial/CommercialBundleUrlLifecycle.scala b/common/app/common/commercial/CommercialBundleUrlLifecycle.scala index 5e93831a66fb..118b26dc880c 100644 --- a/common/app/common/commercial/CommercialBundleUrlLifecycle.scala +++ b/common/app/common/commercial/CommercialBundleUrlLifecycle.scala @@ -6,8 +6,8 @@ import play.api.inject.ApplicationLifecycle import scala.concurrent.{ExecutionContext, Future} -class CommercialBundleUrlLifecycle(appLifeCycle: ApplicationLifecycle, jobs: JobScheduler, pekkoAsync: PekkoAsync)(implicit - ec: ExecutionContext, +class CommercialBundleUrlLifecycle(appLifeCycle: ApplicationLifecycle, jobs: JobScheduler, pekkoAsync: PekkoAsync)( + implicit ec: ExecutionContext, ) extends LifecycleComponent { appLifeCycle.addStopHook { () => diff --git a/common/app/model/dotcomrendering/DotcomRenderingUtils.scala b/common/app/model/dotcomrendering/DotcomRenderingUtils.scala index e0446dbb9eba..10b70491d263 100644 --- a/common/app/model/dotcomrendering/DotcomRenderingUtils.scala +++ b/common/app/model/dotcomrendering/DotcomRenderingUtils.scala @@ -120,11 +120,11 @@ object DotcomRenderingUtils { def assetURL(bundlePath: String): String = { // This function exists because for some reasons `Static` behaves differently in { PROD and CODE } versus LOCAL - // if (Configuration.environment.isProd || Configuration.environment.isCode) { + if (Configuration.environment.isProd || Configuration.environment.isCode) { Static(bundlePath) - // } else { - // s"${Configuration.site.host}${Static(bundlePath)}" - // } + } else { + s"${Configuration.site.host}${Static(bundlePath)}" + } } // note: this is duplicated in the onward service (DotcomponentsOnwardsModels - if duplicating again consider moving to common! :() diff --git a/common/app/views/support/JavaScriptPage.scala b/common/app/views/support/JavaScriptPage.scala index 9e3f9237d16a..745eb30722f7 100644 --- a/common/app/views/support/JavaScriptPage.scala +++ b/common/app/views/support/JavaScriptPage.scala @@ -71,7 +71,7 @@ object JavaScriptPage { val ipsos = if (page.metadata.isFront) getScriptTag(page.metadata.id) else getScriptTag(page.metadata.sectionId) val commercialBundleUrl = JsString( - Configuration.commercial.commercialBundleUrl + Configuration.commercial.commercialBundleUrl, ) javascriptConfig ++ config ++ commercialMetaData ++ journalismMetaData ++ Map( diff --git a/facia/app/AppLoader.scala b/facia/app/AppLoader.scala index 5922c2bd6dfb..d6bdad168182 100644 --- a/facia/app/AppLoader.scala +++ b/facia/app/AppLoader.scala @@ -55,7 +55,7 @@ trait AppComponents extends FrontendComponents with FaciaControllers with FapiSe wire[ConfigAgentLifecycle], wire[CloudWatchMetricsLifecycle], wire[FaciaDfpAgentLifecycle], - wire[CommercialBundleUrlLifecycle], + wire[CommercialBundleUrlLifecycle], wire[SurgingContentAgentLifecycle], wire[IndexListingsLifecycle], wire[SwitchboardLifecycle], diff --git a/preview/app/AppLoader.scala b/preview/app/AppLoader.scala index ae2c6a706c9c..3da928ee52fb 100644 --- a/preview/app/AppLoader.scala +++ b/preview/app/AppLoader.scala @@ -54,7 +54,7 @@ trait PreviewLifecycleComponents wire[OnwardJourneyLifecycle], wire[ConfigAgentLifecycle], wire[FaciaDfpAgentLifecycle], - wire[CommercialBundleUrlLifecycle], + wire[CommercialBundleUrlLifecycle], wire[SwitchboardLifecycle], wire[FootballLifecycle], wire[CricketLifecycle], From 17add266fc48422c904fbac88f3ab1ad11e7a125 Mon Sep 17 00:00:00 2001 From: Jake Lee Kennedy Date: Fri, 1 Nov 2024 16:13:48 +0000 Subject: [PATCH 4/5] dont throw an exception if asset map missing --- common/app/common/commercial/CommercialBundleUrlAgent.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/app/common/commercial/CommercialBundleUrlAgent.scala b/common/app/common/commercial/CommercialBundleUrlAgent.scala index 965179e62667..0ffc5787cd51 100644 --- a/common/app/common/commercial/CommercialBundleUrlAgent.scala +++ b/common/app/common/commercial/CommercialBundleUrlAgent.scala @@ -33,7 +33,7 @@ object CommercialBundleUrlAgent extends GuLogging { def getAssetMap(): AssetMap = { S3.get(commercialAssetMap) flatMap (AssetMapParser( _, - )) getOrElse (throw new RuntimeException(s"Failed to load commercial bundle asset map from $commercialAssetMap")) + )) getOrElse AssetMap(None) } def getBundlePath(): String = { From 3ccae418997c7844673e28d6ca176bdf9e3eec9c Mon Sep 17 00:00:00 2001 From: Jake Lee Kennedy Date: Fri, 1 Nov 2024 17:42:38 +0000 Subject: [PATCH 5/5] tidy --- common/app/common/commercial/CommercialBundleUrlAgent.scala | 6 ------ 1 file changed, 6 deletions(-) diff --git a/common/app/common/commercial/CommercialBundleUrlAgent.scala b/common/app/common/commercial/CommercialBundleUrlAgent.scala index 0ffc5787cd51..f36df3713ba9 100644 --- a/common/app/common/commercial/CommercialBundleUrlAgent.scala +++ b/common/app/common/commercial/CommercialBundleUrlAgent.scala @@ -1,14 +1,8 @@ package common.commercial -import scala.concurrent.ExecutionContext -import common.Environment.{app, awsRegion, stage} import conf.Configuration.commercial._ -import services.ParameterStore import services.S3 -import scala.concurrent.Future import play.api.libs.json.Json -import conf.Configuration -import play.api.libs.ws.{WSClient, WSResponse} import common.GuLogging case class AssetMap(