-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Legg til jobb som oppdaterer virksomhetsstatistikk_for_prioritering
Co-authored-by: Per-Christian Nielsen <per-christian.nielsen@nav.no>
- Loading branch information
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/main/kotlin/no/nav/lydia/vedlikehold/StatistikkViewOppdaterer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |