Commit b7778b13 authored by Jonas Waeber's avatar Jonas Waeber
Browse files

Improves exception handling and reporting

parent 9f674898
Pipeline #11845 passed with stages
in 5 minutes and 15 seconds
......@@ -41,7 +41,7 @@ dependencies {
//implementation "org.apache.kafka:kafka-streams:${kafkaV}"
compile group: 'org.apache.kafka', name: 'kafka-clients', version: kafkaV
implementation 'org.memobase:memobase-service-utilities:1.4.0'
implementation 'org.memobase:memobase-service-utilities:1.7.1'
// JSON Parser
implementation 'com.beust:klaxon:5.2'
// CSV Reader
......
......@@ -21,6 +21,7 @@ package org.memobase
import java.io.File
import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Logger
import org.memobase.exceptions.SftpClientException
import org.memobase.settings.SettingsLoader
import org.memobase.sftp.SftpClient
......@@ -43,28 +44,59 @@ class Service(fileName: String = "app.yml") {
producer.use { producer ->
sftpClient.use { sftp ->
log.info("Connected to SFTP & Kafka.")
val validator = FileValidation()
val files = sftp.listFiles(settings.appSettings.getProperty("directory")).map { File(it) }
val reports = mutableListOf<Report>()
for (file in files) {
log.info("Validate file ${file.absolutePath}.")
val format = validator.validateExtension(file)
val validationResult = validator.validate(sftp.open(file), format, file)
producer.sendMessage(validationResult.second.id, validationResult.first)
producer.sendReport(validationResult.second)
reports.add(validationResult.second)
}
val failures = reports.count { report -> report.status == ReportStatus.failure }
if (failures > 0) {
log.warn("Validation ended with $failures failures!")
try {
val validator = FileValidation()
val files = sftp.listFiles(settings.appSettings.getProperty("directory")).map { File(it) }
val reports = mutableListOf<Report>()
for (file in files) {
log.info("Validate file ${file.absolutePath}.")
val format = validator.validateExtension(file)
val validationResult = validator.validate(sftp.open(file), format, file)
producer.sendMessage(validationResult.second.id, validationResult.first)
producer.sendReport(validationResult.second)
reports.add(validationResult.second)
}
val failures = reports.count { report -> report.status == ReportStatus.failure }
if (failures > 0) {
log.warn("Validation ended with $failures failures!")
producer.sendJobReport(
Report(
"text-file-validation",
status = ReportStatus.failure,
message = ReportMessages.processFailure(failures, reports.size)
),
settings.processReportTopic
)
} else {
log.info("Validation was successful!")
producer.sendJobReport(
Report(
"text-file-validation",
status = ReportStatus.success,
message = ReportMessages.processSuccess(reports.size)
),
settings.processReportTopic
)
}
} catch (ex: SftpClientException) {
log.error("SFTP Exception: ${ex.localizedMessage}")
producer.sendJobReport(
Report("text-file-validation", status = ReportStatus.failure, message = ReportMessages.processFailure(failures, reports.size)),
Report(
"text-file-validation",
status = ReportStatus.failure,
message = "SFTP Exception: ${ex.localizedMessage}."
),
settings.processReportTopic
)
} else {
log.info("Validation was successful!")
} catch (ex: Exception) {
ex.printStackTrace()
log.error(ex.javaClass.canonicalName + ": " + ex.localizedMessage)
producer.sendJobReport(
Report("text-file-validation", status = ReportStatus.success, message = ReportMessages.processSuccess(reports.size)),
Report(
"text-file-validation",
status = ReportStatus.failure,
message = "Unknown Exception: ${ex.javaClass.canonicalName}: ${ex.localizedMessage}."
),
settings.processReportTopic
)
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment