Skip to content

Commit 46a17f6

Browse files
committed
Legg på mer logging og try/catch
1 parent c4bf70e commit 46a17f6

File tree

2 files changed

+50
-44
lines changed

2 files changed

+50
-44
lines changed

dp-inntekt-api/src/main/kotlin/no/nav/dagpenger/inntekt/Application.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ fun main() {
4949
val subsumsjonBruktDataConsumer =
5050
KafkaSubsumsjonBruktDataConsumer(config, postgresInntektStore)
5151
.apply {
52+
LOGGER.info("Prøver å starte lytting på kafka")
5253
listen()
5354
}.also {
5455
Runtime.getRuntime().addShutdownHook(

dp-inntekt-api/src/main/kotlin/no/nav/dagpenger/inntekt/subsumsjonbrukt/KafkaSubsumsjonBruktDataConsumer.kt

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -47,58 +47,63 @@ internal class KafkaSubsumsjonBruktDataConsumer(
4747
launch(coroutineContext) {
4848
logger.info { "Starting ${config.application.id}" }
4949

50-
KafkaConsumer<String, String>(
51-
consumerConfig(
52-
groupId = config.application.id,
53-
bootstrapServerUrl = config.application.brokers,
54-
credential = config.application.credential,
55-
).also {
56-
it[ConsumerConfig.AUTO_OFFSET_RESET_CONFIG] = "earliest"
57-
it[ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG] = "false"
58-
it[ConsumerConfig.MAX_POLL_RECORDS_CONFIG] = 10
59-
},
60-
).use { consumer ->
61-
try {
62-
consumer.subscribe(listOf(config.inntektBruktDataTopic))
63-
while (job.isActive) {
64-
val records = consumer.poll(Duration.ofMillis(100))
65-
val ids =
66-
records
67-
.asSequence()
68-
.map { record -> record.value() }
69-
.map { jacksonObjectMapper.readTree(it) }
70-
.filter { packet ->
71-
packet.has("@event_name") &&
72-
packet.has("aktorId") &&
73-
packet.has("inntektsId") &&
74-
packet.has("kontekst") &&
75-
packet.get("@event_name").asText() == "brukt_inntekt"
76-
}.map { packet -> InntektId(packet.get("inntektsId").asText()) }
77-
.toList()
50+
try {
51+
KafkaConsumer<String, String>(
52+
consumerConfig(
53+
groupId = config.application.id,
54+
bootstrapServerUrl = config.application.brokers,
55+
credential = config.application.credential,
56+
).also {
57+
it[ConsumerConfig.AUTO_OFFSET_RESET_CONFIG] = "earliest"
58+
it[ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG] = "false"
59+
it[ConsumerConfig.MAX_POLL_RECORDS_CONFIG] = 10
60+
},
61+
).use { consumer ->
62+
try {
63+
consumer.subscribe(listOf(config.inntektBruktDataTopic))
64+
while (job.isActive) {
65+
val records = consumer.poll(Duration.ofMillis(100))
66+
val ids =
67+
records
68+
.asSequence()
69+
.map { record -> record.value() }
70+
.map { jacksonObjectMapper.readTree(it) }
71+
.filter { packet ->
72+
packet.has("@event_name") &&
73+
packet.has("aktorId") &&
74+
packet.has("inntektsId") &&
75+
packet.has("kontekst") &&
76+
packet.get("@event_name").asText() == "brukt_inntekt"
77+
}.map { packet -> InntektId(packet.get("inntektsId").asText()) }
78+
.toList()
7879

79-
try {
80-
ids.forEach { id ->
81-
if (inntektStore.markerInntektBrukt(id) == 1) {
82-
logger.info("Marked inntekt with id $id as used")
80+
try {
81+
ids.forEach { id ->
82+
if (inntektStore.markerInntektBrukt(id) == 1) {
83+
logger.info("Marked inntekt with id $id as used")
84+
}
8385
}
86+
if (ids.isNotEmpty()) {
87+
consumer.commitSync()
88+
}
89+
} catch (e: CommitFailedException) {
90+
logger.warn("Kafka threw a commit fail exception, looping back", e)
8491
}
85-
if (ids.isNotEmpty()) {
86-
consumer.commitSync()
87-
}
88-
} catch (e: CommitFailedException) {
89-
logger.warn("Kafka threw a commit fail exception, looping back", e)
9092
}
91-
}
92-
} catch (e: Exception) {
93-
logger.error(
94-
"""
93+
} catch (e: Exception) {
94+
logger.error(
95+
"""
9596
"Unexpected exception while consuming messages.
9697
Stopping consumer, grace period ${grace.duration.seconds / 60} minutes"
9798
""".trimIndent(),
98-
e,
99-
)
100-
stop()
99+
e,
100+
)
101+
stop()
102+
}
101103
}
104+
} catch (e: Exception) {
105+
logger.error("Fikk en feil ved oppsett av KafkaConsumer", e)
106+
stop()
102107
}
103108
}
104109
}

0 commit comments

Comments
 (0)