Skip to content

Commit

Permalink
tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakeii committed Dec 5, 2024
1 parent f0958ea commit b083b92
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
28 changes: 15 additions & 13 deletions common/app/common/CommercialBundle.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,32 @@ object CommercialBundle {

private lazy val parameterStore = new ParameterStore(Environment.awsRegion)

private var cachedBundlePath: String =
"test_commercial_bundles/" + bundlePathParameterValue
private var cachedTimestamp: Instant = Instant.now()
private val cacheDuration: FiniteDuration = 1.minute
private val stage = Environment.stage

private var cachedBundlePath: String = bundlePathFromParameterStore
private var cachedTimestamp: Instant = Instant.now()

private val assetHost = Configuration.assets.path
// when running locally Configuration.assets.path is set to "assets/" to serve local assets, but the commercial bundle no longer lives there, so we need to override it
private val basePath =
if (stage.equalsIgnoreCase("dev")) "https://assets-code.guim.co.uk/" else Configuration.assets.path

private def bundlePathParameterValue: String = {
if (Environment.stage == "CODE")
private def bundlePathFromParameterStore: String = {
if (stage == "CODE")
parameterStore.get("/frontend/CODE/commercial.bundlePath")
else
// for PROD and DEV to use the same bundle path
parameterStore.get("/frontend/commercial.bundlePath")
}

private def updateBundlePath(): Unit = {
cachedBundlePath = "test_commercial_bundles/" + bundlePathParameterValue
cachedTimestamp = Instant.now()
}

private def bundlePath: String = {
if (Instant.now().isAfter(cachedTimestamp.plus(cacheDuration.toMillis))) updateBundlePath()
if (Instant.now().isAfter(cachedTimestamp.plus(cacheDuration.toMillis))) {
cachedBundlePath = bundlePathFromParameterStore
cachedTimestamp = Instant.now()
}

cachedBundlePath
}

def getBundleUrl: String = s"$assetHost$bundlePath"
def bundleUrl: String = s"$basePath$bundlePath"
}
2 changes: 1 addition & 1 deletion common/app/views/support/JavaScriptPage.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ object JavaScriptPage {

val commercialBundleUrl =
if (ActiveExperiments.isParticipating(CommercialBundleUpdater)(request))
JsString(CommercialBundle.getBundleUrl)
JsString(CommercialBundle.bundleUrl)
else
JsString(
Configuration.commercial.overrideCommercialBundleUrl
Expand Down

0 comments on commit b083b92

Please sign in to comment.