Skip to content

Commit

Permalink
review cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Corbin Smith authored and cgruber committed Nov 16, 2020
1 parent cc039db commit 3f4698a
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions src/main/kotlin/io/bazel/kotlin/builder/tasks/BazelWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import java.nio.charset.StandardCharsets.UTF_8
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.util.logging.Level
import java.util.logging.Level.SEVERE
import java.util.logging.Logger

/**
Expand Down Expand Up @@ -150,7 +150,7 @@ class PersistentWorker(
private val io: WorkerIO,
private val program: CommandLineProgram
) : Worker {
val logger = Logger.getLogger(PersistentWorker::class.java.canonicalName)
private val logger = Logger.getLogger(PersistentWorker::class.java.canonicalName)

enum class Status {
OK, INTERRUPTED, ERROR
Expand All @@ -163,17 +163,15 @@ class PersistentWorker(
val (status, exit) = WorkingDirectoryContext.newContext()
.runCatching {
request.argumentsList
?.let {
maybeExpand(it)
}
?.let { maybeExpand(it) }
.run {
Status.OK to program.apply(dir, maybeExpand(request.argumentsList))
}
}
.recover { e: Throwable ->
io.execution.write((e.message ?: e.toString()).toByteArray(UTF_8))
if (!e.wasInterrupted()) {
logger.log(Level.SEVERE,
logger.log(SEVERE,
"ERROR: Worker threw uncaught exception",
e)
Status.ERROR to 1
Expand All @@ -183,10 +181,10 @@ class PersistentWorker(
}
.getOrThrow()

val response = with(WorkResponse.newBuilder()) {
val response = WorkResponse.newBuilder().apply {
output = String(io.execution.toByteArray(), UTF_8)
exitCode = exit
setRequestId(request.requestId)
requestId = request.requestId
}.build()

// return the response
Expand All @@ -209,20 +207,18 @@ class InvocationWorker(
private val io: WorkerIO,
private val program: CommandLineProgram
) : Worker {
val logger: Logger = Logger.getLogger(InvocationWorker::class.java.canonicalName)
override fun run(args: List<String>): Int {
return WorkingDirectoryContext.newContext().runCatching {
program.apply(dir, maybeExpand(args))
}.recover { e ->
logger.log(Level.SEVERE,
private val logger: Logger = Logger.getLogger(InvocationWorker::class.java.canonicalName)
override fun run(args: List<String>): Int = WorkingDirectoryContext.newContext()
.runCatching { program.apply(dir, maybeExpand(args)) }
.recover { e ->
logger.log(SEVERE,
"ERROR: Worker threw uncaught exception with args: ${maybeExpand(args)}",
e)
1
}.also {
return@recover 1 // return non-0 exitcode
}
.also {
// print execution log
println(String(io.execution.toByteArray(), UTF_8))
}.getOrDefault(0)
}
}
.getOrDefault(0)
}



0 comments on commit 3f4698a

Please sign in to comment.