From 053b9f85c02f907604300503f6ba5d893ea0ccc1 Mon Sep 17 00:00:00 2001 From: Roberto Tyley <52038+rtyley@users.noreply.github.com> Date: Fri, 13 Sep 2024 12:35:57 +0100 Subject: [PATCH] Make refresh interval a strongly-typed Duration I'm trying to find out why `PublicSettings` in s3-uploader (https://github.com/guardian/s3-upload/pull/58) didn't log that the panda settings had changed - maybe the refresh schedule did not start, or possibly the interval was no good (rounded to nothing)? --- .../pandomainauth/PanDomainAuthSettingsRefresher.scala | 4 +--- .../main/scala/com/gu/pandomainauth/PublicSettings.scala | 5 +++-- .../src/main/scala/com/gu/pandomainauth/Settings.scala | 9 +++++++-- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/pan-domain-auth-core/src/main/scala/com/gu/pandomainauth/PanDomainAuthSettingsRefresher.scala b/pan-domain-auth-core/src/main/scala/com/gu/pandomainauth/PanDomainAuthSettingsRefresher.scala index 48ddcf2..fe28101 100644 --- a/pan-domain-auth-core/src/main/scala/com/gu/pandomainauth/PanDomainAuthSettingsRefresher.scala +++ b/pan-domain-auth-core/src/main/scala/com/gu/pandomainauth/PanDomainAuthSettingsRefresher.scala @@ -2,8 +2,6 @@ package com.gu.pandomainauth import com.amazonaws.services.s3.AmazonS3 import com.gu.pandomainauth.model.PanDomainAuthSettings -import com.gu.pandomainauth.service.CryptoConf -import org.slf4j.LoggerFactory import java.util.concurrent.Executors.newScheduledThreadPool import java.util.concurrent.ScheduledExecutorService @@ -40,7 +38,7 @@ class PanDomainAuthSettingsRefresher( _.signingAndVerification, scheduler ) - settingsRefresher.start(1) + settingsRefresher.start() def settings: PanDomainAuthSettings = settingsRefresher.get() } diff --git a/pan-domain-auth-verification/src/main/scala/com/gu/pandomainauth/PublicSettings.scala b/pan-domain-auth-verification/src/main/scala/com/gu/pandomainauth/PublicSettings.scala index c578a59..e569a7a 100644 --- a/pan-domain-auth-verification/src/main/scala/com/gu/pandomainauth/PublicSettings.scala +++ b/pan-domain-auth-verification/src/main/scala/com/gu/pandomainauth/PublicSettings.scala @@ -6,9 +6,10 @@ import com.gu.pandomainauth.service.CryptoConf import com.gu.pandomainauth.service.CryptoConf.Verification import java.security.PublicKey +import java.time.Duration +import java.time.Duration.ofMinutes import java.util.concurrent.Executors.newScheduledThreadPool import java.util.concurrent.{Executors, ScheduledExecutorService} -import scala.concurrent.duration._ /** * Class that contains the static public settings and includes mechanism for fetching the public key. Once you have an @@ -28,7 +29,7 @@ class PublicSettings(loader: Settings.Loader, scheduler: ScheduledExecutorServic scheduler ) - def start(interval: FiniteDuration = 60.seconds): Unit = settingsRefresher.start(interval.toMinutes.toInt) + def start(interval: Duration = ofMinutes(1)): Unit = settingsRefresher.start(interval) def verification: Verification = settingsRefresher.get() diff --git a/pan-domain-auth-verification/src/main/scala/com/gu/pandomainauth/Settings.scala b/pan-domain-auth-verification/src/main/scala/com/gu/pandomainauth/Settings.scala index 0f89414..2bec6d8 100644 --- a/pan-domain-auth-verification/src/main/scala/com/gu/pandomainauth/Settings.scala +++ b/pan-domain-auth-verification/src/main/scala/com/gu/pandomainauth/Settings.scala @@ -6,8 +6,10 @@ import com.gu.pandomainauth.service.CryptoConf.Verification import org.slf4j.{Logger, LoggerFactory} import java.io.ByteArrayInputStream +import java.time.Duration +import java.time.Duration.ofMinutes import java.util.Properties -import java.util.concurrent.TimeUnit.MINUTES +import java.util.concurrent.TimeUnit.MILLISECONDS import java.util.concurrent.atomic.AtomicReference import java.util.concurrent.{Executors, ScheduledExecutorService} import scala.jdk.CollectionConverters._ @@ -92,7 +94,10 @@ object Settings { private val logger = LoggerFactory.getLogger(getClass) - def start(interval: Int): Unit = scheduler.scheduleAtFixedRate(() => refresh(), 0, interval, MINUTES) + def start(interval: Duration = ofMinutes(1)): Unit = { + logger.info(s"Starting refresh schedule with an interval of $interval") + scheduler.scheduleAtFixedRate(() => refresh(), 0, interval.toMillis, MILLISECONDS) + } def loadAndParseSettings(): SettingsResult[A] = loader.loadAndParseSettingsMap().flatMap(settingsParser)