-
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.
* WIP: Batch tracking * Fix syntax feil i batchprograss dao * Add test for BatchTrackingDao * Remove unused dep * Bruk _ for ubrukte parameter --------- Co-authored-by: sigurdgroneng <[email protected]>
- Loading branch information
1 parent
264ce19
commit 435a6d2
Showing
9 changed files
with
162 additions
and
52 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
32 changes: 0 additions & 32 deletions
32
src/main/java/no/nav/veilarbaktivitet/stilling_fra_nav/DelingAvCvManueltAvbruttService.java
This file was deleted.
Oops, something went wrong.
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
58 changes: 58 additions & 0 deletions
58
src/main/kotlin/no/nav/veilarbaktivitet/stilling_fra_nav/BatchTrackingDAO.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,58 @@ | ||
package no.nav.veilarbaktivitet.stilling_fra_nav | ||
|
||
import org.slf4j.LoggerFactory | ||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate | ||
import org.springframework.stereotype.Repository | ||
|
||
enum class BatchJob { | ||
Deling_av_cv_avbrutt_eller_fuulfort_uten_svar | ||
} | ||
|
||
@Repository | ||
open class BatchTrackingDAO( | ||
val template: NamedParameterJdbcTemplate | ||
) { | ||
private val log = LoggerFactory.getLogger(javaClass) | ||
|
||
open fun setSisteProsesserteVersjon(batch: BatchJob, sisteProsesserteVersjon: Long) { | ||
template.update(""" | ||
INSERT INTO batch_tracking (batch_name, last_offset) VALUES (:batchName, :sisteProsesserteVersjon) | ||
ON CONFLICT(batch_name) | ||
DO UPDATE SET last_offset = :sisteProsesserteVersjon | ||
""".trimIndent(), mapOf("sisteProsesserteVersjon" to sisteProsesserteVersjon, "batchName" to batch.name)) | ||
} | ||
|
||
open fun hentSisteProsseserteVersjon(batch: BatchJob): Long { | ||
val results = template.query(""" | ||
SELECT last_offset FROM batch_tracking where batch_name = :batchName | ||
""".trimIndent(), mapOf("batchName" to batch.name) | ||
) { row, _ -> row.getLong("last_offset") } | ||
if (results.isEmpty()) { | ||
log.warn("Could not find last_offset for batch: ${batch.name} ") | ||
return 0 | ||
} | ||
return results.first() | ||
} | ||
|
||
public fun withOffset(batch: BatchJob, processBatch: (sisteProsesserteVersjon: Long) -> List<BatchResult>): List<BatchResult> { | ||
val siste = hentSisteProsseserteVersjon(batch) | ||
val batchResults = processBatch(siste) | ||
setSisteProsesserteVersjon(batch, batchResults.sisteProsesserteVersjon(siste)) | ||
return batchResults | ||
} | ||
} | ||
|
||
sealed class BatchResult(val versjon: Long) { | ||
class Success(versjon: Long): BatchResult(versjon) | ||
class Failure(versjon: Long): BatchResult(versjon) | ||
} | ||
|
||
fun List<BatchResult>.sisteProsesserteVersjon(fallbackOffset: Long): Long { | ||
val førsteFeiledeVersjon = this | ||
.filterIsInstance<BatchResult.Failure>() | ||
.minByOrNull { it.versjon } | ||
?.versjon | ||
return (førsteFeiledeVersjon?.minus(1) | ||
?: this.filterIsInstance<BatchResult.Success>().maxByOrNull { it.versjon }?.versjon) | ||
?: fallbackOffset | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/kotlin/no/nav/veilarbaktivitet/stilling_fra_nav/DelingAvCvManueltAvbruttService.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,34 @@ | ||
package no.nav.veilarbaktivitet.stilling_fra_nav | ||
|
||
import io.micrometer.core.annotation.Timed | ||
import lombok.RequiredArgsConstructor | ||
import no.nav.veilarbaktivitet.aktivitet.domain.AktivitetData | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
open class DelingAvCvManueltAvbruttService( | ||
private val delingAvCvService: DelingAvCvService, | ||
private val delingAvCvDAO: DelingAvCvDAO, | ||
private val batchTrackingDao: BatchTrackingDAO | ||
) { | ||
private val log = LoggerFactory.getLogger(javaClass) | ||
|
||
@Timed(value = "stillingFraNavAvbruttEllerFullfortUtenSvar", histogram = true) | ||
open fun notifiserFullfortEllerAvbruttUtenSvar(maxantall: Int): Int { | ||
return batchTrackingDao.withOffset(BatchJob.Deling_av_cv_avbrutt_eller_fuulfort_uten_svar) { sisteProsesserteVersjonFørBatch -> | ||
val aktivitetData = delingAvCvDAO.hentStillingFraNavSomErFullfortEllerAvbruttUtenSvar(maxantall.toLong(), sisteProsesserteVersjonFørBatch) | ||
aktivitetData.map { aktivitet: AktivitetData -> | ||
try { | ||
delingAvCvService.notifiserAvbruttEllerFullfortUtenSvar(aktivitet) | ||
return@map BatchResult.Success(aktivitet.versjon) | ||
} catch (e: Exception) { | ||
log.warn("Behandling av fullført/avbrutt aktivitet aktivitetId=${aktivitet.id} feilet") | ||
log.error("Kunne ikke behandle avbrutt/fullført aktivitet", e) | ||
return@map BatchResult.Failure(aktivitet.versjon) | ||
} | ||
} | ||
}.size | ||
} | ||
} |
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,5 @@ | ||
CREATE TABLE IF NOT EXISTS batch_tracking( | ||
batch_name varchar(60) primary key, | ||
last_offset bigint | ||
) | ||
|
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
58 changes: 58 additions & 0 deletions
58
src/test/java/no/nav/veilarbaktivitet/stilling_fra_nav/BatchTrackingDAOTest.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,58 @@ | ||
package no.nav.veilarbaktivitet.stilling_fra_nav | ||
|
||
import no.nav.veilarbaktivitet.LocalDatabaseSingleton.postgres | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate | ||
import javax.sql.DataSource | ||
|
||
class BatchTrackingDAOTest { | ||
|
||
private val db: DataSource = postgres | ||
private val namedParameterJdbcTemplate = NamedParameterJdbcTemplate(db) | ||
private val batchTrackingDAO = BatchTrackingDAO(namedParameterJdbcTemplate) | ||
|
||
@BeforeEach | ||
fun setup() { | ||
batchTrackingDAO.setSisteProsesserteVersjon(BatchJob.Deling_av_cv_avbrutt_eller_fuulfort_uten_svar, 0) | ||
} | ||
|
||
@Test | ||
fun should_not_increment_offset_if_nothing_is_processsed() { | ||
batchTrackingDAO.withOffset(BatchJob.Deling_av_cv_avbrutt_eller_fuulfort_uten_svar) { offset -> | ||
assertThat(offset).isEqualTo(0) | ||
emptyList() | ||
} | ||
batchTrackingDAO.withOffset(BatchJob.Deling_av_cv_avbrutt_eller_fuulfort_uten_svar) { offset -> | ||
assertThat(offset).isEqualTo(0) | ||
emptyList() | ||
} | ||
} | ||
|
||
@Test | ||
fun should_set_offset_to_highest_success_result_processsed() { | ||
batchTrackingDAO.withOffset(BatchJob.Deling_av_cv_avbrutt_eller_fuulfort_uten_svar) { offset -> | ||
assertThat(offset).isEqualTo(0) | ||
listOf(BatchResult.Success(10), BatchResult.Success(20)) | ||
} | ||
batchTrackingDAO.withOffset(BatchJob.Deling_av_cv_avbrutt_eller_fuulfort_uten_svar) { offset -> | ||
assertThat(offset).isEqualTo(20) | ||
emptyList() | ||
} | ||
} | ||
|
||
|
||
@Test | ||
fun should_set_offset_to_first_failure_result_processsed() { | ||
batchTrackingDAO.withOffset(BatchJob.Deling_av_cv_avbrutt_eller_fuulfort_uten_svar) { offset -> | ||
assertThat(offset).isEqualTo(0) | ||
listOf(BatchResult.Failure(10), BatchResult.Failure(2), BatchResult.Success(20)) | ||
} | ||
batchTrackingDAO.withOffset(BatchJob.Deling_av_cv_avbrutt_eller_fuulfort_uten_svar) { offset -> | ||
assertThat(offset).isEqualTo(1) | ||
emptyList() | ||
} | ||
} | ||
|
||
} |
3 changes: 0 additions & 3 deletions
3
src/test/kotlin/no/nav/veilarbaktivitet/aktivitetskort/EksternaktivitetDAOTest.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