From fbe8246e1d19feb6d7241fb1ea1ce66b1cefb0d5 Mon Sep 17 00:00:00 2001 From: JoeSimmonds <884957+JoeSimmonds@users.noreply.github.com> Date: Wed, 18 Sep 2024 13:53:03 +0100 Subject: [PATCH 1/3] Added code to change the ttl if it doesn't match config --- .../repository/MovementRepository.scala | 55 ++++++++++++++++++- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/app/uk/gov/hmrc/excisemovementcontrolsystemapi/repository/MovementRepository.scala b/app/uk/gov/hmrc/excisemovementcontrolsystemapi/repository/MovementRepository.scala index 32a892bd..a8fcccb1 100644 --- a/app/uk/gov/hmrc/excisemovementcontrolsystemapi/repository/MovementRepository.scala +++ b/app/uk/gov/hmrc/excisemovementcontrolsystemapi/repository/MovementRepository.scala @@ -19,9 +19,11 @@ package uk.gov.hmrc.excisemovementcontrolsystemapi.repository import cats.implicits.toFunctorOps import org.apache.pekko.Done import org.bson.conversions.Bson +import org.mongodb.scala.Document import org.mongodb.scala.model.Filters._ import org.mongodb.scala.model.{InsertManyOptions, _} -import play.api.libs.json.{JsObject, Json, OFormat} +import play.api.Logging +import play.api.libs.json.{Json, OFormat} import uk.gov.hmrc.excisemovementcontrolsystemapi.config.AppConfig import uk.gov.hmrc.excisemovementcontrolsystemapi.filters.MovementFilter import uk.gov.hmrc.excisemovementcontrolsystemapi.repository.MovementRepository._ @@ -36,7 +38,7 @@ import java.time.Instant import java.util.concurrent.TimeUnit import javax.inject.{Inject, Singleton} import scala.concurrent.duration.Duration -import scala.concurrent.{ExecutionContext, Future} +import scala.concurrent.{Await, ExecutionContext, Future, TimeoutException} import scala.jdk.CollectionConverters.SeqHasAsJava @Singleton @@ -57,10 +59,57 @@ class MovementRepository @Inject() ( Codecs.playFormatCodec(Total.format) ), replaceIndexes = false - ) { + ) + with Logging { private def byId(id: String): Bson = Filters.equal("_id", id) + override def ensureIndexes(): Future[Seq[String]] = Future.successful(Seq.empty) + + def updateTTL(newTtlInSeconds: Long): Future[Done] = + collection.listIndexes().toFuture().flatMap { idxs: Seq[Document] => + val idxMaybe = idxs.find(d => d.get("name").map(_.asString().getValue).contains("lastUpdated_ttl_idx")) + idxMaybe match { + case Some(idx) => + val expiryMaybe = idx.get("expireAfterSeconds").map(_.asInt32().intValue()) + logger.info(s"current TTL is $expiryMaybe seconds") + if (expiryMaybe.contains(newTtlInSeconds)) { + logger.info("TTL set correctly, not updating") + Future.successful(Done) + } else { + logger.info("TTL needs updating") + mongo.database + .runCommand( + Json + .obj( + "collMod" -> "movements", + "index" -> Json.obj( + "keyPattern" -> Json.obj("lastUpdated" -> 1), + "expireAfterSeconds" -> newTtlInSeconds + ) + ) + .toDocument() + ) + .toFuture() + .andThen { case _ => logger.info("TTL updated") } + .map(_ => Done) + } + + case None => + logger.error("Failed to update TTL on index. Index not found.") + Future.successful(Done) + } + } + + try + // We await to ensure failures are propagated on the constructor thread, but we + // don't care about long running index creation. + Await.result(updateTTL(appConfig.movementTTL.toSeconds), Duration.apply("10 seconds")) + catch { + case _: TimeoutException => logger.warn(s"Updating the TTL timed out s") + case t: Throwable => logger.error(s"Failed to update the TTL", t); throw t + } + private def byLrnAndErns(localReferenceNumber: String, erns: List[String]): Bson = and( equal("localReferenceNumber", localReferenceNumber), From d120854de8b7f8bf31113e17069cb5920767e531 Mon Sep 17 00:00:00 2001 From: JoeSimmonds <884957+JoeSimmonds@users.noreply.github.com> Date: Wed, 18 Sep 2024 16:37:30 +0100 Subject: [PATCH 2/3] Revert "Added code to change the ttl if it doesn't match config" This reverts commit fbe8246e1d19feb6d7241fb1ea1ce66b1cefb0d5. --- .../repository/MovementRepository.scala | 55 +------------------ 1 file changed, 3 insertions(+), 52 deletions(-) diff --git a/app/uk/gov/hmrc/excisemovementcontrolsystemapi/repository/MovementRepository.scala b/app/uk/gov/hmrc/excisemovementcontrolsystemapi/repository/MovementRepository.scala index a8fcccb1..32a892bd 100644 --- a/app/uk/gov/hmrc/excisemovementcontrolsystemapi/repository/MovementRepository.scala +++ b/app/uk/gov/hmrc/excisemovementcontrolsystemapi/repository/MovementRepository.scala @@ -19,11 +19,9 @@ package uk.gov.hmrc.excisemovementcontrolsystemapi.repository import cats.implicits.toFunctorOps import org.apache.pekko.Done import org.bson.conversions.Bson -import org.mongodb.scala.Document import org.mongodb.scala.model.Filters._ import org.mongodb.scala.model.{InsertManyOptions, _} -import play.api.Logging -import play.api.libs.json.{Json, OFormat} +import play.api.libs.json.{JsObject, Json, OFormat} import uk.gov.hmrc.excisemovementcontrolsystemapi.config.AppConfig import uk.gov.hmrc.excisemovementcontrolsystemapi.filters.MovementFilter import uk.gov.hmrc.excisemovementcontrolsystemapi.repository.MovementRepository._ @@ -38,7 +36,7 @@ import java.time.Instant import java.util.concurrent.TimeUnit import javax.inject.{Inject, Singleton} import scala.concurrent.duration.Duration -import scala.concurrent.{Await, ExecutionContext, Future, TimeoutException} +import scala.concurrent.{ExecutionContext, Future} import scala.jdk.CollectionConverters.SeqHasAsJava @Singleton @@ -59,57 +57,10 @@ class MovementRepository @Inject() ( Codecs.playFormatCodec(Total.format) ), replaceIndexes = false - ) - with Logging { + ) { private def byId(id: String): Bson = Filters.equal("_id", id) - override def ensureIndexes(): Future[Seq[String]] = Future.successful(Seq.empty) - - def updateTTL(newTtlInSeconds: Long): Future[Done] = - collection.listIndexes().toFuture().flatMap { idxs: Seq[Document] => - val idxMaybe = idxs.find(d => d.get("name").map(_.asString().getValue).contains("lastUpdated_ttl_idx")) - idxMaybe match { - case Some(idx) => - val expiryMaybe = idx.get("expireAfterSeconds").map(_.asInt32().intValue()) - logger.info(s"current TTL is $expiryMaybe seconds") - if (expiryMaybe.contains(newTtlInSeconds)) { - logger.info("TTL set correctly, not updating") - Future.successful(Done) - } else { - logger.info("TTL needs updating") - mongo.database - .runCommand( - Json - .obj( - "collMod" -> "movements", - "index" -> Json.obj( - "keyPattern" -> Json.obj("lastUpdated" -> 1), - "expireAfterSeconds" -> newTtlInSeconds - ) - ) - .toDocument() - ) - .toFuture() - .andThen { case _ => logger.info("TTL updated") } - .map(_ => Done) - } - - case None => - logger.error("Failed to update TTL on index. Index not found.") - Future.successful(Done) - } - } - - try - // We await to ensure failures are propagated on the constructor thread, but we - // don't care about long running index creation. - Await.result(updateTTL(appConfig.movementTTL.toSeconds), Duration.apply("10 seconds")) - catch { - case _: TimeoutException => logger.warn(s"Updating the TTL timed out s") - case t: Throwable => logger.error(s"Failed to update the TTL", t); throw t - } - private def byLrnAndErns(localReferenceNumber: String, erns: List[String]): Bson = and( equal("localReferenceNumber", localReferenceNumber), From f807a45cb0e984969be996ef7d459ebf3cdee71e Mon Sep 17 00:00:00 2001 From: JoeSimmonds <884957+JoeSimmonds@users.noreply.github.com> Date: Wed, 18 Sep 2024 16:38:20 +0100 Subject: [PATCH 3/3] setting replace indexes to true --- .../repository/MovementRepository.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/uk/gov/hmrc/excisemovementcontrolsystemapi/repository/MovementRepository.scala b/app/uk/gov/hmrc/excisemovementcontrolsystemapi/repository/MovementRepository.scala index 32a892bd..2a62e093 100644 --- a/app/uk/gov/hmrc/excisemovementcontrolsystemapi/repository/MovementRepository.scala +++ b/app/uk/gov/hmrc/excisemovementcontrolsystemapi/repository/MovementRepository.scala @@ -56,7 +56,7 @@ class MovementRepository @Inject() ( Codecs.playFormatCodec(ProblemMovement.format), Codecs.playFormatCodec(Total.format) ), - replaceIndexes = false + replaceIndexes = true ) { private def byId(id: String): Bson = Filters.equal("_id", id)