Skip to content

Commit

Permalink
Add local service exception handler & increase waiting time
Browse files Browse the repository at this point in the history
  • Loading branch information
pplam committed Sep 20, 2024
1 parent 80e9a31 commit 3ecbcd4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/ai/devchat/core/DevChatClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ class DevChatClient(val project: Project, private val localServicePort: Int) {
const val DEFAULT_LOG_MAX_COUNT = 10000
const val LOG_RAW_DATA_SIZE_LIMIT = 4 * 1024 // 4kb
const val RETRY_INTERVAL: Long = 500 // ms
const val MAX_RETRIES: Int = 10
const val MAX_RETRIES: Int = 50
}
private val client = OkHttpClient()
private val json = Json { ignoreUnknownKeys = true }
Expand Down
11 changes: 9 additions & 2 deletions src/main/kotlin/ai/devchat/installer/DevChatSetupThread.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.intellij.openapi.project.Project
import java.io.BufferedReader
import java.io.File
import java.nio.file.Paths
import kotlin.system.measureTimeMillis

class DevChatSetupThread(val project: Project) : Thread() {
private val minimalPythonVersion: String = "3.8"
Expand All @@ -26,10 +27,16 @@ class DevChatSetupThread(val project: Project) : Thread() {
Notifier.info("Starting $ASSISTANT_NAME_EN initialization...")
try {
Log.info("Start configuring the $ASSISTANT_NAME_EN CLI environment.")
setupPython(PythonEnvManager())
val executionTime = measureTimeMillis {
setupPython(PythonEnvManager())
}
Log.info("-----------> Time took to setup python: ${executionTime/1000} s")
installTools()
updateWorkflows()
devChatService.browser?.executeJS("onInitializationFinish")
devChatService.browser?.let {
Log.info("-----------> Executing JS callback onInitializationFinish")
it.executeJS("onInitializationFinish")
}
DevChatState.instance.lastVersion = devChatVersion
Notifier.info("$ASSISTANT_NAME_EN initialization has completed successfully.")
} catch (e: Exception) {
Expand Down
14 changes: 11 additions & 3 deletions src/main/kotlin/ai/devchat/plugin/DevChatToolWindowFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,22 @@ class DevChatToolWindowFactory : ToolWindowFactory, DumbAware, Disposable {

DevChatSetupThread(project).start()

CoroutineScope(Dispatchers.IO).launch {
withTimeoutOrNull(5000) {
while (!devChatService.pythonReady) { delay(100) }
val exceptionHandler = CoroutineExceptionHandler { _, throwable ->
Log.error("-----------> Failed to start local service")
throwable.printStackTrace()
}
CoroutineScope(Dispatchers.Default + exceptionHandler).launch {
withTimeoutOrNull(60000) {
while (!devChatService.pythonReady) {
Log.info("-----------> Waiting python ready...")
delay(100)
}
LocalService(project).start()
}?.let {
Disposer.register(content, it)
devChatService.localServicePort = it.port!!
devChatService.client = DevChatClient(project, it.port!!)
Log.info("-----------> DevChat client ready")
}
}
IDEServer(project).start().let {
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/ai/devchat/plugin/LocalService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class LocalService(project: Project): Disposable {
ServerSocket(0).use {
port = it.localPort
}
Log.info("-----------> Starting local service..., port: $port")
val commandLine = GeneralCommandLine()
.withExePath(CONFIG["python_for_chat"] as String)
.withParameters(PathUtils.localServicePath)
Expand Down

0 comments on commit 3ecbcd4

Please sign in to comment.