Skip to content

Commit

Permalink
chore: use kotlin autoclosable for schema-registry client in tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
e.rudikov committed Jan 26, 2024
1 parent 706d050 commit d87496e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,20 @@ abstract class CompatibilityCheckTask : DefaultTask() {
val schemaName = schemaForCheck.get()
val subject = topic.get()

test(client = client, subject = subject, schemaName = schemaName)
client.use { test(client = it, subject = subject, schemaName = schemaName) }
}

private fun checkForAllSchemas() {
if (subjectToSchema.orNull.isNullOrEmpty()) {
error("No schema has been announced!")
}

val client = CachedSchemaRegistryClient(schemaRegistryUrl.get(), subjectToSchema.get().entries.size)
val registryClient = CachedSchemaRegistryClient(schemaRegistryUrl.get(), subjectToSchema.get().entries.size)

subjectToSchema.get().forEach { (subject, schemaName) ->
test(client = client, subject = subject, schemaName = schemaName)
registryClient.use { client ->
subjectToSchema.get().forEach { (subject, schemaName) ->
test(client = client, subject = subject, schemaName = schemaName)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,21 @@ abstract class RegisterTask : DefaultTask() {
error("No schema has been announced!")
}

val client = CachedSchemaRegistryClient(schemaRegistryUrl.get(), subjectToSchema.get().entries.size)

subjectToSchema.get().forEach { (subject, schemaName) ->
searchAvroFilesPaths.get().forEach { searchPath ->
runCatching {
val avroFile = findAvroFileByName(searchPath = searchPath, schemaName = schemaName)
val schema = AvroSchema(Schema.Parser().parse(avroFile))

client.register(subject, schema)
}.onSuccess {
logger.lifecycle("$schemaName: $it")
}.onFailure {
logger.warn("Failed register $schemaName for $subject!", it)
val registryClient = CachedSchemaRegistryClient(schemaRegistryUrl.get(), subjectToSchema.get().entries.size)

registryClient.use { client ->
subjectToSchema.get().forEach { (subject, schemaName) ->
searchAvroFilesPaths.get().forEach { searchPath ->
runCatching {
val avroFile = findAvroFileByName(searchPath = searchPath, schemaName = schemaName)
val schema = AvroSchema(Schema.Parser().parse(avroFile))

client.register(subject, schema)
}.onSuccess {
logger.lifecycle("$schemaName: $it")
}.onFailure {
logger.warn("Failed register $schemaName for $subject!", it)
}
}
}
}
Expand Down

0 comments on commit d87496e

Please sign in to comment.