Refactoring: extract withDaemon
This commit is contained in:
+31
-28
@@ -138,46 +138,22 @@ abstract class KotlinCompilerRunner<in Env : CompilerEnvironment> {
|
|||||||
protected open fun compileWithDaemon(
|
protected open fun compileWithDaemon(
|
||||||
compilerClassName: String,
|
compilerClassName: String,
|
||||||
argsArray: Array<String>,
|
argsArray: Array<String>,
|
||||||
environment: Env,
|
environment: Env
|
||||||
retryOnConnectionError: Boolean = true
|
|
||||||
): ExitCode? {
|
): ExitCode? {
|
||||||
log.debug("Try to connect to daemon")
|
|
||||||
val connection = getDaemonConnection(environment)
|
|
||||||
|
|
||||||
if (connection.daemon != null) {
|
|
||||||
log.info("Connected to daemon")
|
|
||||||
|
|
||||||
val compilerOut = ByteArrayOutputStream()
|
val compilerOut = ByteArrayOutputStream()
|
||||||
val daemonOut = ByteArrayOutputStream()
|
val daemonOut = ByteArrayOutputStream()
|
||||||
|
|
||||||
val services = CompilationServices(
|
val services = CompilationServices(
|
||||||
incrementalCompilationComponents = environment.services.get(IncrementalCompilationComponents::class.java),
|
incrementalCompilationComponents = environment.services.get(IncrementalCompilationComponents::class.java),
|
||||||
compilationCanceledStatus = environment.services.get(CompilationCanceledStatus::class.java))
|
compilationCanceledStatus = environment.services.get(CompilationCanceledStatus::class.java))
|
||||||
|
|
||||||
val targetPlatform = when (compilerClassName) {
|
val targetPlatform = when (compilerClassName) {
|
||||||
K2JVM_COMPILER -> CompileService.TargetPlatform.JVM
|
K2JVM_COMPILER -> CompileService.TargetPlatform.JVM
|
||||||
K2JS_COMPILER -> CompileService.TargetPlatform.JS
|
K2JS_COMPILER -> CompileService.TargetPlatform.JS
|
||||||
else -> throw IllegalArgumentException("Unknown compiler type $compilerClassName")
|
else -> throw IllegalArgumentException("Unknown compiler type $compilerClassName")
|
||||||
}
|
}
|
||||||
|
|
||||||
fun retryOrFalse(e: Exception): ExitCode? {
|
val res: Int = withDaemon(environment) { daemon, sessionId ->
|
||||||
if (retryOnConnectionError) {
|
KotlinCompilerClient.incrementalCompile(daemon, sessionId, targetPlatform, argsArray, services, compilerOut, daemonOut)
|
||||||
log.debug("retrying once on daemon connection error: ${e.message}")
|
} ?: return null
|
||||||
return compileWithDaemon(compilerClassName, argsArray, environment, retryOnConnectionError = false)
|
|
||||||
}
|
|
||||||
log.info("daemon connection error: ${e.message}")
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
val res: Int = try {
|
|
||||||
KotlinCompilerClient.incrementalCompile(connection.daemon, connection.sessionId, targetPlatform, argsArray, services, compilerOut, daemonOut)
|
|
||||||
}
|
|
||||||
catch (e: java.rmi.ConnectException) {
|
|
||||||
return retryOrFalse(e)
|
|
||||||
}
|
|
||||||
catch (e: java.rmi.UnmarshalException) {
|
|
||||||
return retryOrFalse(e)
|
|
||||||
}
|
|
||||||
|
|
||||||
val exitCode = exitCodeFromProcessExitCode(res)
|
val exitCode = exitCodeFromProcessExitCode(res)
|
||||||
processCompilerOutput(environment, compilerOut, exitCode)
|
processCompilerOutput(environment, compilerOut, exitCode)
|
||||||
@@ -187,6 +163,33 @@ abstract class KotlinCompilerRunner<in Env : CompilerEnvironment> {
|
|||||||
return exitCode
|
return exitCode
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected fun <T> withDaemon(environment: Env, retryOnConnectionError: Boolean = true, fn: (CompileService, sessionId: Int)->T): T? {
|
||||||
|
fun retryOrFalse(e: Exception): T? {
|
||||||
|
if (retryOnConnectionError) {
|
||||||
|
log.debug("retrying once on daemon connection error: ${e.message}")
|
||||||
|
return withDaemon(environment, false, fn)
|
||||||
|
}
|
||||||
|
log.info("daemon connection error: ${e.message}")
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
log.debug("Try to connect to daemon")
|
||||||
|
val connection = getDaemonConnection(environment)
|
||||||
|
|
||||||
|
if (connection.daemon != null) {
|
||||||
|
log.info("Connected to daemon")
|
||||||
|
|
||||||
|
try {
|
||||||
|
return fn(connection.daemon, connection.sessionId)
|
||||||
|
}
|
||||||
|
catch (e: java.rmi.ConnectException) {
|
||||||
|
return retryOrFalse(e)
|
||||||
|
}
|
||||||
|
catch (e: java.rmi.UnmarshalException) {
|
||||||
|
return retryOrFalse(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
log.info("Daemon not found")
|
log.info("Daemon not found")
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user