@@ -13,17 +13,23 @@ import play.api.Logger
1313import play .api .i18n .MessagesApi
1414import play .api .libs .json .{JsArray , JsNull , JsObject , JsValue , Json }
1515import cats .implicits .*
16- import cron4s .Cron
16+ import cron4s ._
17+ import cron4s .lib .joda ._
18+ import org .apache .pekko .actor .Cancellable
1719
20+ import java .util .concurrent .atomic .AtomicReference
1821import scala .concurrent .{ExecutionContext , Future }
22+ import scala .concurrent .duration .*
1923
2024class ApiKeySecretRotationJob (
2125 client : OtoroshiClient ,
2226 env : Env ,
2327 translator : Translator ,
2428 messagesApi : MessagesApi
2529 ) {
26- private val rotationlogger = Logger (" APIkey-Rotation-Synchronizer" )
30+ private val logger = Logger (" APIkey-Rotation-Synchronizer" )
31+
32+ private val ref = new AtomicReference [Cancellable ]()
2733
2834 implicit val ec : ExecutionContext = env.defaultExecutionContext
2935 implicit val mat : Materializer = env.defaultMaterializer
@@ -32,7 +38,7 @@ class ApiKeySecretRotationJob(
3238 implicit val tr : Translator = translator
3339
3440 def start (): Unit = {
35- val syncAvalaible = env.config.rotationJobEnabled && env.config.otoroshiSyncMaster // FIXME: use also otoroshiSyncMaster ???
41+ val syncAvalaible = env.config.rotationJobEnabled && env.config.otoroshiSyncMaster // FIXME: use also otoroshiSyncMaster ???
3642
3743 if (
3844 syncAvalaible && ref.get() == null
@@ -51,9 +57,9 @@ class ApiKeySecretRotationJob(
5157 logger.info(s " next cron run scheduled at $nextRun (in ${delay.toSeconds}s) " )
5258
5359 ref.set(
54- env.defaultActorSystem.scheduler.scheduleOnce(delay) { _ =>
60+ env.defaultActorSystem.scheduler.scheduleOnce(delay) {
5561 logger.info(s " cron triggered at $now" )
56- env.dataStore.tenantRepo
62+ val _ = env.dataStore.tenantRepo
5763 .findAllNotDeleted()
5864 .flatMap(tenants =>
5965 Future .sequence(
@@ -67,6 +73,7 @@ class ApiKeySecretRotationJob(
6773 .andThen { case _ =>
6874 scheduleNext()
6975 }
76+ ()
7077 }
7178 )
7279
@@ -80,7 +87,7 @@ class ApiKeySecretRotationJob(
8087 case SchedulingMode .Interval =>
8188 ref.set(
8289 env.defaultActorSystem.scheduler
83- .scheduleAtFixedRate(10 .seconds, env.config.rotationJobInterval) { _ =>
90+ .scheduleAtFixedRate(10 .seconds, env.config.rotationJobInterval) { () =>
8491 env.dataStore.tenantRepo
8592 .findAllNotDeleted()
8693 .flatMap(tenants =>
@@ -97,7 +104,7 @@ class ApiKeySecretRotationJob(
97104 }
98105 }
99106 }
100-
107+
101108 def stop (): Unit = {
102109 Option (ref.get()).foreach(_.cancel())
103110 }
@@ -242,7 +249,7 @@ class ApiKeySecretRotationJob(
242249 if (
243250 ! pendingRotation && otoroshiNextSecret.isDefined && otoroshiActualSecret == daikokuActualSecret
244251 ) {
245- rotationlogger .info(
252+ logger .info(
246253 s " rotation state updated to Pending for ${apk.clientName}"
247254 )
248255 newSubscription = subscription
@@ -274,7 +281,7 @@ class ApiKeySecretRotationJob(
274281 )
275282
276283 } else if (pendingRotation && otoroshiNextSecret.isEmpty) {
277- rotationlogger .info(
284+ logger .info(
278285 s " rotation state updated to Ended for ${apk.clientName}"
279286 )
280287 notification = Notification (
@@ -342,16 +349,16 @@ class ApiKeySecretRotationJob(
342349 .save(notification)
343350 } yield ()
344351 case (_, _) =>
345- rotationlogger .info(s " no need to update rotation for ${apk.clientName}" )
352+ logger .info(s " no need to update rotation for ${apk.clientName}" )
346353 }
347354 }
348355 }
349356 )
350357 }
351358 }
352-
359+
353360 def run (entryPoint : ApiId | UsagePlanId | ApiSubscriptionId | SyncAllSubscription = SyncAllSubscription (), tenant : Tenant ): Future [Unit ] = {
354- rotationlogger .info(s " run apikey rotation check with entry point as $entryPoint" )
361+ logger .info(s " run apikey rotation check with entry point as $entryPoint" )
355362
356363 val query = entryPoint match {
357364 case apiId : ApiId => Json .obj(" api" -> apiId.asJson)
@@ -372,7 +379,7 @@ class ApiKeySecretRotationJob(
372379 " status" -> JobStatus .Running .value))
373380 .flatMap {
374381 case Some (_) =>
375- rotationlogger .info(" can't run another ApiKeyRotationVerifier, already one is running" )
382+ logger .info(" can't run another ApiKeyRotationVerifier, already one is running" )
376383 Future .successful(())
377384 case None =>
378385 val jobInfo = JobInformation (
@@ -382,20 +389,20 @@ class ApiKeySecretRotationJob(
382389 lockedBy = " apikey-rotation-verifier-job" ,
383390 lockedAt = now,
384391 expiresAt = now.plusHours(1 ),
385- cursor = " " ,
392+ cursor = 0 ,
386393 startedAt = now,
387394 lastBatchAt = now,
388395 status = JobStatus .Running
389396 )
390397 jobRepo.save(jobInfo).flatMap { _ =>
391398 checkRotation(query)
392399 .flatMap { _ =>
393- rotationlogger .info(" verify rotation ended" )
400+ logger .info(" verify rotation ended" )
394401 jobRepo.save(jobInfo.copy(status = JobStatus .Completed , lastBatchAt = DateTime .now()))
395402 .map(_ => ())
396403 }
397404 .recoverWith { case e =>
398- rotationlogger .error(s " verify rotation failed: ${e.getMessage}" , e)
405+ logger .error(s " verify rotation failed: ${e.getMessage}" , e)
399406 jobRepo.save(jobInfo.copy(status = JobStatus .Failed , lastBatchAt = DateTime .now()))
400407 .map(_ => ())
401408 }
0 commit comments