Skip to content

Commit

Permalink
La statusType og karanteneTil som egne databasefelter
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Kroken committed Nov 23, 2023
1 parent de7469c commit 9a554de
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,37 +36,45 @@ class PersongrunnlagMeldingService(

fun process(): FullførteBehandlinger? {
return transactionTemplate.execute {
finnNesteMeldingForBehandling()?.let { melding ->
Mdc.scopedMdc(melding.correlationId) {
Mdc.scopedMdc(melding.innlesingId) {
try {
transactionTemplate.execute {
behandle(melding).let { fullførte ->
persongrunnlagRepo.updateStatus(melding.ferdig())
fullførte.also {
it.håndterUtfall(
innvilget = ::håndterInnvilgelse,
manuell = oppgaveService::opprettOppgaveHvisNødvendig,
avslag = {} //noop
)
log.info("Melding prosessert")
try {
finnNesteMeldingForBehandling()?.let { melding ->
Mdc.scopedMdc(melding.correlationId) {
Mdc.scopedMdc(melding.innlesingId) {
try {
log.info("Started behandling av melding")
transactionTemplate.execute {
behandle(melding).let { fullførte ->
persongrunnlagRepo.updateStatus(melding.ferdig())
fullførte.also {
it.håndterUtfall(
innvilget = ::håndterInnvilgelse,
manuell = oppgaveService::opprettOppgaveHvisNødvendig,
avslag = {} //noop
)
log.info("Melding prosessert")
}
}
}
}
} catch (ex: Throwable) {
transactionTemplate.execute {
melding.retry(ex.stackTraceToString()).let { melding ->
melding.opprettOppgave()?.let {
log.error("Gir opp videre prosessering av melding")
oppgaveService.opprett(it)
} catch (ex: Throwable) {
transactionTemplate.execute {
melding.retry(ex.stackTraceToString()).let { melding ->
melding.opprettOppgave()?.let {
log.error("Gir opp videre prosessering av melding")
oppgaveService.opprett(it)
}
persongrunnlagRepo.updateStatus(melding)
}
persongrunnlagRepo.updateStatus(melding)
}
throw ex
} finally {
log.info("Avsluttet behandling av melding")
}
throw ex
}
}
}
} catch (ex: Throwable) {
log.error("Fikk exception ved uthenting av melding", ex)
throw ex
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,21 @@ class PersongrunnlagRepo(
keyHolder
)
jdbcTemplate.update(
"""insert into melding_status (id, status, statushistorikk) values (:id, to_jsonb(:status::jsonb), to_jsonb(:statushistorikk::jsonb))""",
"""insert into melding_status (id, status, statushistorikk, status_type, karantene_til) values (:id, to_jsonb(:status::jsonb), to_jsonb(:statushistorikk::jsonb),:statusType, :karanteneTil)""",
MapSqlParameterSource(
mapOf<String, Any>(
mapOf<String, Any?>(
"id" to keyHolder.keys!!["id"] as UUID,
"status" to serialize(melding.status),
"statusType" to when(melding.status) {
is PersongrunnlagMelding.Status.Feilet -> "Feilet"
is PersongrunnlagMelding.Status.Ferdig -> "Ferdig"
is PersongrunnlagMelding.Status.Klar -> "Klar"
is PersongrunnlagMelding.Status.Retry -> "Retry"
},
"karanteneTil" to when (val m = melding.status) {
is PersongrunnlagMelding.Status.Retry -> m.karanteneTil
else -> null
},
"statushistorikk" to melding.statushistorikk.serializeList(),
),
),
Expand All @@ -51,11 +61,21 @@ class PersongrunnlagRepo(

fun updateStatus(melding: PersongrunnlagMelding.Mottatt) {
jdbcTemplate.update(
"""update melding_status set status = to_jsonb(:status::jsonb), statushistorikk = to_jsonb(:statushistorikk::jsonb) where id = :id""",
"""update melding_status set status = to_jsonb(:status::jsonb), status_type = :statusType, karantene_til = :karanteneTil::timestamptz, statushistorikk = to_jsonb(:statushistorikk::jsonb) where id = :id""",
MapSqlParameterSource(
mapOf<String, Any>(
mapOf<String, Any?>(
"id" to melding.id,
"status" to serialize(melding.status),
"statusType" to when(melding.status) {
is PersongrunnlagMelding.Status.Feilet -> "Feilet"
is PersongrunnlagMelding.Status.Ferdig -> "Ferdig"
is PersongrunnlagMelding.Status.Klar -> "Klar"
is PersongrunnlagMelding.Status.Retry -> "Retry"
},
"karanteneTil" to when (val m = melding.status) {
is PersongrunnlagMelding.Status.Retry -> m.karanteneTil.toString()
else -> null
},
"statushistorikk" to melding.statushistorikk.serializeList()
),
),
Expand Down Expand Up @@ -83,7 +103,7 @@ class PersongrunnlagRepo(
"""select m.*, ms.statushistorikk
|from melding m, melding_status ms
|where m.id = ms.id
|and (ms.status->>'type') = 'Klar'
|and ms.status_type = 'Klar'
|fetch first row only for no key update of m skip locked""".trimMargin(),
mapOf(
"now" to Instant.now(clock).toString()
Expand All @@ -98,8 +118,8 @@ class PersongrunnlagRepo(
"""select m.*, ms.statushistorikk
|from melding m, melding_status ms
|where m.id = ms.id
|and ms.status->>'type' = 'Retry'
|and (ms.status->>'karanteneTil')::timestamptz < (:now)::timestamptz
|and status_type = 'Retry'
|and karantene_til < (:now)::timestamptz
|fetch first row only for no key update of m skip locked""".trimMargin(),
mapOf(
"now" to now
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/db/migration/V15__karantenefelt.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
alter table melding_status add column karantene_til timestamp with time zone;
alter table melding_status add column status_type varchar(10);

create index melding_status_type_karantene on melding_status(status_type, karantene_til);

0 comments on commit 9a554de

Please sign in to comment.