From 19f4caf8b6b9e2634142c8dc93fc0ec1cadc12b3 Mon Sep 17 00:00:00 2001 From: mikrise2 Date: Fri, 8 Dec 2023 17:49:32 +0100 Subject: [PATCH] [ML4SE-249] Changed coroutine scopes for file sending. --- .../research/tasktracker/requests/FileRequests.kt | 8 +++----- .../jetbrains/research/tasktracker/tracking/Loggable.kt | 2 +- .../tasktracker/tracking/logger/DocumentLogger.kt | 8 ++++++-- .../tasktracker/ui/main/panel/MainPluginPanelFactory.kt | 8 ++++++-- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/ij-plugin/src/main/kotlin/org/jetbrains/research/tasktracker/requests/FileRequests.kt b/ij-plugin/src/main/kotlin/org/jetbrains/research/tasktracker/requests/FileRequests.kt index cbbe9cff..02e81b32 100644 --- a/ij-plugin/src/main/kotlin/org/jetbrains/research/tasktracker/requests/FileRequests.kt +++ b/ij-plugin/src/main/kotlin/org/jetbrains/research/tasktracker/requests/FileRequests.kt @@ -5,7 +5,6 @@ import io.ktor.client.* import io.ktor.client.engine.cio.* import io.ktor.client.request.forms.* import io.ktor.http.* -import kotlinx.coroutines.runBlocking import org.apache.http.client.utils.URIBuilder import org.jetbrains.research.tasktracker.config.MainTaskTrackerConfig.Companion.getRoute import org.jetbrains.research.tasktracker.ui.main.panel.storage.GlobalPluginStorage @@ -17,7 +16,7 @@ object FileRequests { private val logger: Logger = Logger.getInstance(FileRequests::class.java) @Suppress("TooGenericExceptionCaught") - fun sendFile(file: File, logFileType: String) = runBlocking { + suspend fun sendFile(file: File, logFileType: String): Boolean { try { val researchId = GlobalPluginStorage.currentResearchId ?: error("ResearchId is undefined") @@ -37,13 +36,12 @@ object FileRequests { ) } ) - true + return true } catch (e: IllegalStateException) { logger.warn(e.localizedMessage) - false } catch (e: Exception) { logger.warn("Server interaction error! File to send: ${file.path}", e) - false } + return false } } diff --git a/ij-plugin/src/main/kotlin/org/jetbrains/research/tasktracker/tracking/Loggable.kt b/ij-plugin/src/main/kotlin/org/jetbrains/research/tasktracker/tracking/Loggable.kt index 111b7464..69456886 100644 --- a/ij-plugin/src/main/kotlin/org/jetbrains/research/tasktracker/tracking/Loggable.kt +++ b/ij-plugin/src/main/kotlin/org/jetbrains/research/tasktracker/tracking/Loggable.kt @@ -9,7 +9,7 @@ abstract class Loggable { abstract fun getLogFiles(): List - fun send() = getLogFiles().all { + suspend fun send() = getLogFiles().all { FileRequests.sendFile(it, this.logFileType) } } diff --git a/ij-plugin/src/main/kotlin/org/jetbrains/research/tasktracker/tracking/logger/DocumentLogger.kt b/ij-plugin/src/main/kotlin/org/jetbrains/research/tasktracker/tracking/logger/DocumentLogger.kt index e5fa7d2e..0916b83f 100644 --- a/ij-plugin/src/main/kotlin/org/jetbrains/research/tasktracker/tracking/logger/DocumentLogger.kt +++ b/ij-plugin/src/main/kotlin/org/jetbrains/research/tasktracker/tracking/logger/DocumentLogger.kt @@ -2,6 +2,8 @@ package org.jetbrains.research.tasktracker.tracking.logger import com.intellij.openapi.diagnostic.Logger import com.intellij.openapi.editor.Document +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch import org.jetbrains.research.tasktracker.requests.FileRequests import java.io.File @@ -23,8 +25,10 @@ object DocumentLogger { ?: emptyList().also { logger.error("attempt to flush non-existing csv printer for document '$document'") } - logFiles.all { - FileRequests.sendFile(it, "document") + logFiles.forEach { + GlobalScope.launch { + FileRequests.sendFile(it, "document") + } } myDocumentsToPrinters.remove(document) } diff --git a/ij-plugin/src/main/kotlin/org/jetbrains/research/tasktracker/ui/main/panel/MainPluginPanelFactory.kt b/ij-plugin/src/main/kotlin/org/jetbrains/research/tasktracker/ui/main/panel/MainPluginPanelFactory.kt index 84b1744c..bc92956e 100644 --- a/ij-plugin/src/main/kotlin/org/jetbrains/research/tasktracker/ui/main/panel/MainPluginPanelFactory.kt +++ b/ij-plugin/src/main/kotlin/org/jetbrains/research/tasktracker/ui/main/panel/MainPluginPanelFactory.kt @@ -13,6 +13,8 @@ import com.intellij.openapi.wm.ToolWindowFactory import com.intellij.ui.components.JBPanel import com.intellij.ui.jcef.JBCefApp import com.intellij.util.ui.JBUI +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch import kotlinx.serialization.json.Json import org.jetbrains.concurrency.Promise import org.jetbrains.research.tasktracker.config.content.task.base.Task @@ -90,8 +92,10 @@ class MainPluginPanelFactory : ToolWindowFactory { trackers.forEach { it.stopTracking() } - trackers.forEach { - it.send() + GlobalScope.launch { + trackers.forEach { + it.send() + } } }