Skip to content

Commit

Permalink
Cronjob for outdated dialogmotekandidater (#138)
Browse files Browse the repository at this point in the history
* Cronjob for outdated dialogmotekandidater

* Remove redundant group by. Increase limit and job interval.
  • Loading branch information
andersrognstad authored Sep 20, 2023
1 parent 3af8057 commit 5983fae
Show file tree
Hide file tree
Showing 10 changed files with 370 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .nais/naiserator-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,7 @@ spec:
value: "https://pdl-api.dev-fss-pub.nais.io/graphql"
- name: STOPPUNKT_CRONJOB_DELAY
value: "1"
- name: OUTDATED_DIALOGMOTEKANDIDAT_CUTOFF
value: "2023-06-01"
- name: OUTDATED_DIALOGMOTEKANDIDAT_CRONJOB_ENABLED
value: "false"
4 changes: 4 additions & 0 deletions .nais/naiserator-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,7 @@ spec:
value: "https://pdl-api.prod-fss-pub.nais.io/graphql"
- name: STOPPUNKT_CRONJOB_DELAY
value: "240"
- name: OUTDATED_DIALOGMOTEKANDIDAT_CUTOFF
value: "2023-06-01"
- name: OUTDATED_DIALOGMOTEKANDIDAT_CRONJOB_ENABLED
value: "false"
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import no.nav.syfo.application.kafka.KafkaEnvironment
import no.nav.syfo.client.ClientEnvironment
import no.nav.syfo.client.ClientsEnvironment
import no.nav.syfo.client.azuread.AzureEnvironment
import java.time.LocalDate

const val NAIS_DATABASE_ENV_PREFIX = "NAIS_DATABASE_ISDIALOGMOTEKANDIDAT_ISDIALOGMOTEKANDIDAT_DB"

Expand Down Expand Up @@ -51,6 +52,8 @@ data class Environment(
),
),
val stoppunktCronjobDelay: Long = getEnvVar("STOPPUNKT_CRONJOB_DELAY").toLong(),
val outdatedCronjobEnabled: Boolean = getEnvVar("OUTDATED_DIALOGMOTEKANDIDAT_CRONJOB_ENABLED").toBoolean(),
val outdatedCutoff: LocalDate = LocalDate.parse(getEnvVar("OUTDATED_DIALOGMOTEKANDIDAT_CUTOFF")),
)

fun getEnvVar(varName: String, defaultValue: String? = null) =
Expand Down
23 changes: 18 additions & 5 deletions src/main/kotlin/no/nav/syfo/cronjob/CronjobModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package no.nav.syfo.cronjob
import no.nav.syfo.application.ApplicationState
import no.nav.syfo.application.Environment
import no.nav.syfo.application.backgroundtask.launchBackgroundTask
import no.nav.syfo.cronjob.dialogmotekandidat.DialogmotekandidatOutdatedCronjob
import no.nav.syfo.cronjob.dialogmotekandidat.DialogmotekandidatStoppunktCronjob
import no.nav.syfo.cronjob.leaderelection.LeaderPodClient
import no.nav.syfo.dialogmotekandidat.DialogmotekandidatService
Expand All @@ -12,6 +13,7 @@ fun launchCronjobModule(
environment: Environment,
dialogmotekandidatService: DialogmotekandidatService,
) {
val cronjobs = mutableListOf<Cronjob>()
val leaderPodClient = LeaderPodClient(
electorPath = environment.electorPath
)
Expand All @@ -23,12 +25,23 @@ fun launchCronjobModule(
dialogmotekandidatService = dialogmotekandidatService,
intervalDelayMinutes = environment.stoppunktCronjobDelay,
)
cronjobs.add(dialogmotekandidatStoppunktCronjob)

launchBackgroundTask(
applicationState = applicationState,
) {
cronjobRunner.start(
cronjob = dialogmotekandidatStoppunktCronjob
if (environment.outdatedCronjobEnabled) {
val dialogmotekandidatOutdatedCronjob = DialogmotekandidatOutdatedCronjob(
outdatedDialogmotekandidatCutoff = environment.outdatedCutoff,
dialogmotekandidatService = dialogmotekandidatService,
)
cronjobs.add(dialogmotekandidatOutdatedCronjob)
}

cronjobs.forEach {
launchBackgroundTask(
applicationState = applicationState,
) {
cronjobRunner.start(
cronjob = it
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package no.nav.syfo.cronjob.dialogmotekandidat

import no.nav.syfo.cronjob.Cronjob
import no.nav.syfo.cronjob.CronjobResult
import no.nav.syfo.dialogmotekandidat.DialogmotekandidatService
import no.nav.syfo.dialogmotekandidat.domain.DialogmotekandidatEndring
import org.slf4j.LoggerFactory
import java.time.LocalDate

class DialogmotekandidatOutdatedCronjob(
private val outdatedDialogmotekandidatCutoff: LocalDate,
private val dialogmotekandidatService: DialogmotekandidatService,
) : Cronjob {
override val initialDelayMinutes: Long = 4
override val intervalDelayMinutes: Long = 10

override suspend fun run() {
runJob()
}

fun runJob(): CronjobResult {
val result = CronjobResult()

val cutoff = outdatedDialogmotekandidatCutoff.atStartOfDay()
val outdatedDialogmotekandidater = dialogmotekandidatService.getOutdatedDialogmotekandidater(cutoff)
outdatedDialogmotekandidater.forEach {
try {
val dialogmotekandidatLukket = DialogmotekandidatEndring.lukket(it.personIdentNumber)
dialogmotekandidatService.createDialogmotekandidatEndring(dialogmotekandidatLukket)
result.updated++
} catch (e: Exception) {
result.failed++
log.error("Got exception when creating dialogmotekandidat-endring LUKKET", e)
}
}

return result
}

companion object {
private val log = LoggerFactory.getLogger(DialogmotekandidatOutdatedCronjob::class.java)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import no.nav.syfo.unntak.domain.Unntak
import org.slf4j.LoggerFactory
import java.sql.Connection
import java.time.LocalDate
import java.time.LocalDateTime

class DialogmotekandidatService(
private val oppfolgingstilfelleService: OppfolgingstilfelleService,
Expand All @@ -29,6 +30,8 @@ class DialogmotekandidatService(
fun getDialogmotekandidaterWithStoppunktPlanlagtTodayOrYesterday() =
database.getDialogmotekandidaterWithStoppunktTodayOrYesterday().toDialogmotekandidatStoppunktList()

fun getOutdatedDialogmotekandidater(cutoff: LocalDateTime) = database.findOutdatedDialogmotekandidater(cutoff).toDialogmotekandidatEndringList()

suspend fun updateDialogmotekandidatStoppunktStatus(
dialogmotekandidatStoppunkt: DialogmotekandidatStoppunkt,
) {
Expand Down Expand Up @@ -78,6 +81,20 @@ class DialogmotekandidatService(
}
}

fun createDialogmotekandidatEndring(dialogmotekandidatEndring: DialogmotekandidatEndring) {
database.connection.use { connection ->
connection.createDialogmotekandidatEndring(
dialogmotekandidatEndring = dialogmotekandidatEndring
)
connection.commit()
}
dialogmotekandidatEndringProducer.sendDialogmotekandidatEndring(
dialogmotekandidatEndring = dialogmotekandidatEndring,
tilfelleStart = null,
unntak = null,
)
}

fun createDialogmotekandidatEndring(
connection: Connection,
dialogmotekandidatEndring: DialogmotekandidatEndring,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package no.nav.syfo.dialogmotekandidat.database

import no.nav.syfo.application.database.DatabaseInterface
import no.nav.syfo.application.database.NoElementInsertedException
import no.nav.syfo.application.database.toList
import no.nav.syfo.dialogmotekandidat.domain.DialogmotekandidatEndring
import no.nav.syfo.domain.PersonIdentNumber
import java.sql.Connection
import java.sql.ResultSet
import java.sql.Timestamp
import java.time.Instant
import java.time.LocalDateTime
import java.time.OffsetDateTime
import java.time.ZoneId
import java.util.*

const val queryCreateDialogmotekandidatEndring =
Expand Down Expand Up @@ -54,6 +59,23 @@ fun Connection.getDialogmotekandidatEndringListForPerson(
it.executeQuery().toList { toPDialogmotekandidatEndringList() }
}

const val queryFindOutdatedDialogmotekandidater =
"""
select * from dialogmotekandidat_endring d
where d.created_at = (select max(d2.created_at) from dialogmotekandidat_endring d2 where d2.personident = d.personident)
and d.created_at < ? and d.kandidat
LIMIT 200;
"""

fun DatabaseInterface.findOutdatedDialogmotekandidater(
cutoff: LocalDateTime,
): List<PDialogmotekandidatEndring> = this.connection.use { connection ->
connection.prepareStatement(queryFindOutdatedDialogmotekandidater).use {
it.setTimestamp(1, Timestamp.from(cutoff.toInstantOslo()))
it.executeQuery().toList { toPDialogmotekandidatEndringList() }
}
}

fun ResultSet.toPDialogmotekandidatEndringList() =
PDialogmotekandidatEndring(
id = getInt("id"),
Expand All @@ -63,3 +85,7 @@ fun ResultSet.toPDialogmotekandidatEndringList() =
kandidat = getBoolean("kandidat"),
arsak = getString("arsak"),
)

fun LocalDateTime.toInstantOslo(): Instant = toInstant(
ZoneId.of("Europe/Oslo").rules.getOffset(this)
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ enum class DialogmotekandidatEndringArsak {
STOPPUNKT,
DIALOGMOTE_FERDIGSTILT,
UNNTAK,
LUKKET, // TODO: Konsumenter må støtte denne
}

data class DialogmotekandidatEndring private constructor(
Expand Down Expand Up @@ -57,6 +58,14 @@ data class DialogmotekandidatEndring private constructor(
arsak = DialogmotekandidatEndringArsak.UNNTAK
)

fun lukket(
personIdentNumber: PersonIdentNumber,
) = create(
personIdentNumber = personIdentNumber,
kandidat = false,
arsak = DialogmotekandidatEndringArsak.LUKKET
)

private fun create(
personIdentNumber: PersonIdentNumber,
kandidat: Boolean,
Expand Down
Loading

0 comments on commit 5983fae

Please sign in to comment.