Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added code to change the ttl if it doesn't match config #213

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand All @@ -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
Expand All @@ -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),
Expand Down