diff --git a/app/uk/gov/hmrc/thirdpartyapplication/config/ConfigurationProviders.scala b/app/uk/gov/hmrc/thirdpartyapplication/config/ConfigurationProviders.scala index 77e13cbde..72e59e3c9 100644 --- a/app/uk/gov/hmrc/thirdpartyapplication/config/ConfigurationProviders.scala +++ b/app/uk/gov/hmrc/thirdpartyapplication/config/ConfigurationProviders.scala @@ -42,7 +42,6 @@ class ConfigurationModule extends Module { bind[UpliftVerificationExpiryJobConfig].toProvider[UpliftVerificationExpiryJobConfigProvider], bind[SetLastAccessedDateJobConfig].toProvider[SetLastAccessDateJobConfigProvider], bind[ReconcileRateLimitsJobConfig].toProvider[ReconcileRateLimitsJobConfigProvider], - bind[RefreshMetricsJobConfig].toProvider[RefreshMetricsJobConfigProvider], bind[ApiDefinitionConfig].toProvider[ApiDefinitionConfigProvider], bind[ApiSubscriptionFieldsConfig].toProvider[ApiSubscriptionFieldsConfigProvider], bind[ApiStorageConfig].toProvider[ApiStorageConfigProvider], @@ -137,17 +136,6 @@ class ReconcileRateLimitsJobConfigProvider @Inject()(val runModeConfiguration: C } } -@Singleton -class RefreshMetricsJobConfigProvider @Inject()(val runModeConfiguration: Configuration, environment: Environment) extends Provider[RefreshMetricsJobConfig] with ServicesConfig { - override protected def mode: Mode = environment.mode - - override def get(): RefreshMetricsJobConfig = { - val jobConfig = runModeConfiguration.underlying.as[Option[JobConfig]](s"$env.refreshMetricsJob") - .getOrElse(JobConfig(FiniteDuration(30, SECONDS), FiniteDuration(1, HOURS), enabled = true)) - RefreshMetricsJobConfig(jobConfig.initialDelay, jobConfig.interval, jobConfig.enabled) - } -} - @Singleton class ApiDefinitionConfigProvider @Inject()(val runModeConfiguration: Configuration, environment: Environment) extends Provider[ApiDefinitionConfig] with ServicesConfig { diff --git a/app/uk/gov/hmrc/thirdpartyapplication/config/Scheduler.scala b/app/uk/gov/hmrc/thirdpartyapplication/config/Scheduler.scala index 08ab95ffd..f418ae377 100644 --- a/app/uk/gov/hmrc/thirdpartyapplication/config/Scheduler.scala +++ b/app/uk/gov/hmrc/thirdpartyapplication/config/Scheduler.scala @@ -41,33 +41,12 @@ class Scheduler @Inject()(upliftVerificationExpiryJobConfig: UpliftVerificationE app: Application) extends RunningOfScheduledJobs { override val scheduledJobs: Seq[ExclusiveScheduledJob] = { - - val upliftJob: Seq[ExclusiveScheduledJob] = if (upliftVerificationExpiryJobConfig.enabled) { - Seq(upliftVerificationExpiryJob) - } else { - Seq.empty - } - - val refreshJob = if (refreshSubscriptionsJobConfig.enabled) { - Seq(refreshSubscriptionsScheduledJob) - } else { - Seq.empty - } - - val accessDateJob = if (setLastAccessedDateJobConfig.enabled) { - Seq(setLastAccessedDateJob) - } else { - Seq.empty - } - - val rateLimitsJob = if (reconcileRateLimitsJobConfig.enabled) { - Seq(reconcileRateLimitsJob) - } else { - Seq.empty - } + val upliftJob = if (upliftVerificationExpiryJobConfig.enabled) Seq(upliftVerificationExpiryJob) else Seq.empty + val refreshJob = if (refreshSubscriptionsJobConfig.enabled) Seq(refreshSubscriptionsScheduledJob) else Seq.empty + val accessDateJob = if (setLastAccessedDateJobConfig.enabled) Seq(setLastAccessedDateJob) else Seq.empty + val rateLimitsJob = if (reconcileRateLimitsJobConfig.enabled) Seq(reconcileRateLimitsJob) else Seq.empty upliftJob ++ refreshJob ++ accessDateJob ++ rateLimitsJob - } onStart(app) diff --git a/app/uk/gov/hmrc/thirdpartyapplication/metrics/ApplicationCount.scala b/app/uk/gov/hmrc/thirdpartyapplication/metrics/ApplicationCount.scala index fd9132f8b..23c191f38 100644 --- a/app/uk/gov/hmrc/thirdpartyapplication/metrics/ApplicationCount.scala +++ b/app/uk/gov/hmrc/thirdpartyapplication/metrics/ApplicationCount.scala @@ -17,13 +17,16 @@ package uk.gov.hmrc.thirdpartyapplication.metrics import javax.inject.Inject +import play.api.Logger import uk.gov.hmrc.metrix.domain.MetricSource import uk.gov.hmrc.thirdpartyapplication.repository.ApplicationRepository - import scala.concurrent.{ExecutionContext, Future} class ApplicationCount @Inject()(val applicationRepository: ApplicationRepository) extends MetricSource { override def metrics(implicit ec: ExecutionContext): Future[Map[String, Int]] = - applicationRepository.count.map(applicationCount => Map("applicationCount" -> applicationCount)) + applicationRepository.count.map(applicationCount => { + Logger.info(s"[METRIC] Application Count: $applicationCount") + Map("applicationCount" -> applicationCount) + }) } diff --git a/app/uk/gov/hmrc/thirdpartyapplication/metrics/MetricsModule.scala b/app/uk/gov/hmrc/thirdpartyapplication/metrics/MetricsModule.scala index c9f712a06..3aaae6f3f 100644 --- a/app/uk/gov/hmrc/thirdpartyapplication/metrics/MetricsModule.scala +++ b/app/uk/gov/hmrc/thirdpartyapplication/metrics/MetricsModule.scala @@ -16,11 +16,12 @@ package uk.gov.hmrc.thirdpartyapplication.metrics +import akka.actor.ActorSystem import com.kenshoo.play.metrics.Metrics import javax.inject.{Inject, Provider, Singleton} import org.joda.time.Duration -import play.api.inject.{Binding, Module} -import play.api.{Configuration, Environment} +import play.api.inject.{ApplicationLifecycle, Binding, Module} +import play.api.{Configuration, Environment, Logger} import play.modules.reactivemongo.ReactiveMongoComponent import reactivemongo.api.DB import uk.gov.hmrc.lock.{ExclusiveTimePeriodLock, LockRepository} @@ -28,6 +29,9 @@ import uk.gov.hmrc.metrix.MetricOrchestrator import uk.gov.hmrc.metrix.domain.MetricSource import uk.gov.hmrc.metrix.persistence.MongoMetricRepository +import scala.concurrent.duration._ +import scala.concurrent.{ExecutionContext, Future} + class MetricsModule extends Module { override def bindings(environment: Environment, configuration: Configuration): Seq[Binding[_]] = { Seq( @@ -36,6 +40,21 @@ class MetricsModule extends Module { } } +@Singleton +class MetricsScheduler @Inject()(env: Environment, + lifecycle: ApplicationLifecycle, + actorSystem: ActorSystem, + configuration: Configuration, + metricOrchestrator: MetricOrchestrator)(implicit val ec: ExecutionContext) { + actorSystem.scheduler.schedule(2 minutes, 1 hour) { + Logger.info(s"Running Metrics Collection Process") + metricOrchestrator + .attemptToUpdateAndRefreshMetrics() + .map(_.andLogTheResult()) + .recover { case e: RuntimeException => Logger.error(s"An error occurred processing metrics: ${e.getMessage}", e) } + } +} + @Singleton class MetricsOrchestratorProvider @Inject()(configuration: Configuration, metricsSources: MetricsSources, @@ -47,7 +66,7 @@ class MetricsOrchestratorProvider @Inject()(configuration: Configuration, val Lock: ExclusiveTimePeriodLock = new ExclusiveTimePeriodLock { override def repo: LockRepository = new LockRepository() override def lockId: String = "MetricsLock" - override def holdLockFor: Duration = new Duration(10000) + override def holdLockFor: Duration = Duration.standardMinutes(2) } override def get(): MetricOrchestrator = { diff --git a/app/uk/gov/hmrc/thirdpartyapplication/scheduled/RefreshMetricsScheduledJob.scala b/app/uk/gov/hmrc/thirdpartyapplication/scheduled/RefreshMetricsScheduledJob.scala deleted file mode 100644 index 94b889b47..000000000 --- a/app/uk/gov/hmrc/thirdpartyapplication/scheduled/RefreshMetricsScheduledJob.scala +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 2019 HM Revenue & Customs - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package uk.gov.hmrc.thirdpartyapplication.scheduled - -import javax.inject.Inject -import org.joda.time.Duration -import play.modules.reactivemongo.ReactiveMongoComponent -import uk.gov.hmrc.lock.{LockKeeper, LockRepository} -import uk.gov.hmrc.metrix.MetricOrchestrator - -import scala.concurrent.{ExecutionContext, Future} -import scala.concurrent.duration.FiniteDuration - -class RefreshMetricsScheduledJob @Inject()(val lockKeeper: RefreshMetricsJobLockKeeper, - jobConfig: RefreshMetricsJobConfig, - val metricsOrchestrator: MetricOrchestrator) extends ScheduledMongoJob { - - override def name: String = "RefreshMetricsScheduledJob" - - override def initialDelay: FiniteDuration = jobConfig.initialDelay - - override def interval: FiniteDuration = jobConfig.interval - - override def runJob(implicit ec: ExecutionContext): Future[RunningOfJobSuccessful] = - metricsOrchestrator.attemptToUpdateAndRefreshMetrics().map(_.andLogTheResult()).map(_ => RunningOfJobSuccessful) - -} - -class RefreshMetricsJobLockKeeper @Inject()(mongo: ReactiveMongoComponent) extends LockKeeper { - override def repo: LockRepository = new LockRepository()(mongo.mongoConnector.db) - override def lockId: String = "RefreshMetricsScheduledJob" - override val forceLockReleaseAfter: Duration = Duration.standardMinutes(1) -} - -case class RefreshMetricsJobConfig(initialDelay: FiniteDuration, interval: FiniteDuration, enabled: Boolean) \ No newline at end of file diff --git a/test/unit/uk/gov/hmrc/thirdpartyapplication/controllers/SubscriptionControllerSpec.scala b/test/unit/uk/gov/hmrc/thirdpartyapplication/controllers/SubscriptionControllerSpec.scala index ab1f60be2..353135135 100644 --- a/test/unit/uk/gov/hmrc/thirdpartyapplication/controllers/SubscriptionControllerSpec.scala +++ b/test/unit/uk/gov/hmrc/thirdpartyapplication/controllers/SubscriptionControllerSpec.scala @@ -46,6 +46,7 @@ class SubscriptionControllerSpec extends UnitSpec with ScalaFutures with Mockito override implicit lazy val app: Application = GuiceApplicationBuilder() .disable[com.kenshoo.play.metrics.PlayModule] + .disable[uk.gov.hmrc.thirdpartyapplication.metrics.MetricsModule] .configure("metrics.enabled" -> false) .overrides(bind[SubscriptionRepository].to(mockSubscriptionRepository)) .build