Skip to content

Commit

Permalink
Legg til jobb som oppdaterer virksomhetsstatistikk_for_prioritering
Browse files Browse the repository at this point in the history
Co-authored-by: Per-Christian Nielsen <per-christian.nielsen@nav.no>
  • Loading branch information
klechr and perchrnie committed Sep 25, 2023
1 parent b5bdee9 commit d96b8e9
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main/kotlin/no/nav/lydia/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import no.nav.lydia.sykefraversstatistikk.api.geografi.GeografiService
import no.nav.lydia.sykefraversstatistikk.api.sykefraversstatistikk
import no.nav.lydia.sykefraversstatistikk.import.StatistikkMetadataVirksomhetConsumer
import no.nav.lydia.sykefraversstatistikk.import.StatistikkPerKategoriConsumer
import no.nav.lydia.vedlikehold.StatistikkViewOppdaterer
import no.nav.lydia.virksomhet.VirksomhetRepository
import no.nav.lydia.virksomhet.VirksomhetService
import no.nav.lydia.virksomhet.api.VIRKSOMHET_PATH
Expand Down Expand Up @@ -124,6 +125,10 @@ fun startLydiaBackend() {
run()
}.also { HelseMonitor.leggTilHelsesjekk(it) }

StatistikkViewOppdaterer.apply {
run(dataSource = dataSource)
}

embeddedServer(Netty, port = 8080) {
lydiaRestApi(
naisEnvironment = naisEnv,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package no.nav.lydia.vedlikehold

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.datetime.Clock.System.now
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime
import kotliquery.queryOf
import kotliquery.sessionOf
import kotliquery.using
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import javax.sql.DataSource
import kotlin.coroutines.CoroutineContext
import kotlin.system.measureTimeMillis

object StatistikkViewOppdaterer: CoroutineScope {
private val logger: Logger = LoggerFactory.getLogger(this::class.java)
lateinit var job: Job
lateinit var dataSource: DataSource

override val coroutineContext: CoroutineContext
get() = Dispatchers.IO + job

init {
Runtime.getRuntime().addShutdownHook(Thread(StatistikkViewOppdaterer::cancel))
}

fun run(dataSource: DataSource) {
logger.info("Starter statistikkview oppdaterer")

job = Job()
StatistikkViewOppdaterer.dataSource = dataSource

launch {
while (job.isActive) {
if (now().toLocalDateTime(TimeZone.UTC).hour == 0) {
logger.info("Oppdaterer statistikkview...")
val tidBrukt = measureTimeMillis {
using(sessionOf(dataSource)) { session ->
session.run(
queryOf(
"REFRESH MATERIALIZED VIEW virksomhetsstatistikk_for_prioritering"
).asExecute
)
}
}
logger.info("Oppdaterte statistikkview på $tidBrukt ms")
} else
logger.debug("Statistikkview oppdateres kun mellom kl 00:00 og 00:59")

delay(1000 * 60 * 30) // 30 minutter
}
}
}

fun cancel() = runBlocking {
logger.info("Avslutter statistikkview oppdaterer")
job.cancelAndJoin()
}
}

0 comments on commit d96b8e9

Please sign in to comment.