diff --git a/article/app/AppLoader.scala b/article/app/AppLoader.scala index 51a892cd833..fd8418a3eb2 100644 --- a/article/app/AppLoader.scala +++ b/article/app/AppLoader.scala @@ -4,7 +4,6 @@ import app.{FrontendApplicationLoader, FrontendBuildInfo, FrontendComponents} import com.softwaremill.macwire._ import common._ import common.dfp.DfpAgentLifecycle -import common.commercial.CommercialBundleUrlLifecycle import concurrent.BlockingOperations import conf.{CachedHealthCheckLifeCycle, Configuration} import conf.switches.SwitchboardLifecycle @@ -46,7 +45,6 @@ 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 d283d87c275..994dcd79662 100644 --- a/commercial/app/AppLoader.scala +++ b/commercial/app/AppLoader.scala @@ -9,7 +9,6 @@ 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} @@ -56,7 +55,6 @@ 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/CommercialBundle.scala b/common/app/common/CommercialBundle.scala new file mode 100644 index 00000000000..38ae0d0480d --- /dev/null +++ b/common/app/common/CommercialBundle.scala @@ -0,0 +1,29 @@ +package common + +import scala.concurrent.duration._ +import org.joda.time.Instant +import conf.Configuration + +object CommercialBundle { + import GuardianConfiguration._ + + private var cachedBundlePath: String = + "test_commercial_bundles/" + configuration.getMandatoryStringProperty("commercial.bundlePath") + private var cachedTimestamp: Instant = Instant.now() + private val cacheDuration: FiniteDuration = 1.minute + + private val assetHost = Configuration.assets.path + + private def updateBundlePath(): Unit = { + cachedBundlePath = "test_commercial_bundles/" + configuration.getMandatoryStringProperty("commercial.bundlePath") + cachedTimestamp = Instant.now() + } + + private def bundlePath: String = { + if (Instant.now().isAfter(cachedTimestamp.plus(cacheDuration.toMillis))) updateBundlePath() + + cachedBundlePath + } + + def getBundleUrl: String = s"$assetHost$bundlePath" +} diff --git a/common/app/common/commercial/CommercialBundleUrlAgent.scala b/common/app/common/commercial/CommercialBundleUrlAgent.scala deleted file mode 100644 index 56d5f70e35f..00000000000 --- a/common/app/common/commercial/CommercialBundleUrlAgent.scala +++ /dev/null @@ -1,26 +0,0 @@ -package common.commercial - -import conf.Configuration.commercial._ -import services.S3 -import play.api.libs.json.Json -import common.{GuLogging, Box} -import conf.Configuration - -object CommercialBundleUrlAgent extends GuLogging { - - private lazy val bundlePathAgent = Box[String](getBundlePath()) - - private val assetHost = Configuration.assets.path - - private def bundlePath: String = bundlePathAgent.get() - - def bundleUrl: String = s"$assetHost$bundlePath" - - private def getBundlePath(): String = { - "test_commercial_bundles/" + Configuration.commercial.getBundlePath - } - - def refresh(): Unit = { - bundlePathAgent send getBundlePath() - } -} diff --git a/common/app/common/commercial/CommercialBundleUrlLifecycle.scala b/common/app/common/commercial/CommercialBundleUrlLifecycle.scala deleted file mode 100644 index 993d32d30c1..00000000000 --- a/common/app/common/commercial/CommercialBundleUrlLifecycle.scala +++ /dev/null @@ -1,32 +0,0 @@ -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", 5) { - refreshCommercialBundleUrl() - Future.successful(()) - } - - pekkoAsync.after1s { - refreshCommercialBundleUrl() - } - } -} diff --git a/common/app/views/support/JavaScriptPage.scala b/common/app/views/support/JavaScriptPage.scala index 4330d836529..fb235d57a40 100644 --- a/common/app/views/support/JavaScriptPage.scala +++ b/common/app/views/support/JavaScriptPage.scala @@ -13,7 +13,7 @@ import model.IpsosTags.getScriptTag import model.dotcomrendering.DotcomRenderingUtils.assetURL import play.api.mvc.RequestHeader import views.support.Commercial.isAdFree -import common.commercial.CommercialBundleUrlAgent +import common.CommercialBundle object JavaScriptPage { @@ -72,7 +72,7 @@ object JavaScriptPage { val ipsos = if (page.metadata.isFront) getScriptTag(page.metadata.id) else getScriptTag(page.metadata.sectionId) def commercialBundleUrl = JsString( - CommercialBundleUrlAgent.bundleUrl, + CommercialBundle.getBundleUrl, ) javascriptConfig ++ config ++ commercialMetaData ++ journalismMetaData ++ Map( diff --git a/dev-build/app/AppLoader.scala b/dev-build/app/AppLoader.scala index bc5a2702e98..b235ffd8615 100644 --- a/dev-build/app/AppLoader.scala +++ b/dev-build/app/AppLoader.scala @@ -16,7 +16,6 @@ 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} @@ -91,7 +90,6 @@ 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 d6bdad16818..e1677a9c5c2 100644 --- a/facia/app/AppLoader.scala +++ b/facia/app/AppLoader.scala @@ -4,7 +4,6 @@ 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 @@ -55,7 +54,6 @@ 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 3da928ee52f..73b9df1b38c 100644 --- a/preview/app/AppLoader.scala +++ b/preview/app/AppLoader.scala @@ -8,7 +8,6 @@ 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} @@ -54,7 +53,6 @@ trait PreviewLifecycleComponents wire[OnwardJourneyLifecycle], wire[ConfigAgentLifecycle], wire[FaciaDfpAgentLifecycle], - wire[CommercialBundleUrlLifecycle], wire[SwitchboardLifecycle], wire[FootballLifecycle], wire[CricketLifecycle],