Skip to content

Commit

Permalink
added write read actions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniil.Karol committed Jan 3, 2025
1 parent 845da13 commit 0494ec9
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jetbrains.research.tasktracker

import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.diagnostic.Logger
import org.jetbrains.research.tasktracker.config.DefaultConfigsFactory
import org.jetbrains.research.tasktracker.config.MainTaskTrackerConfig
Expand Down Expand Up @@ -38,7 +39,7 @@ object TaskTrackerPlugin {
DataHandler.LOCAL_FILE -> {
val configFolderRoot =
props.getProperty(CONFIG_ROOT_PROPERTY_NAME)?.let { File(it) } ?: defaultConfigRoot
MainTaskTrackerConfig.buildConfig(pluginProps, configFolderRoot)
runReadAction { MainTaskTrackerConfig.buildConfig(pluginProps, configFolderRoot) }
}

DataHandler.SERVER_CONNECTION -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.jetbrains.research.tasktracker.config

import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.application.runWriteAction
import com.intellij.openapi.diagnostic.Logger
import org.jetbrains.research.tasktracker.config.agreement.AgreementConfig
import org.jetbrains.research.tasktracker.config.content.FinalPageContentConfig
Expand Down Expand Up @@ -46,8 +48,8 @@ object DefaultConfigsFactory {

private fun writeFromResources(configName: String, filePath: String) {
val configFile = File(filePath)
DefaultConfigsFactory::class.java.getResource(configName)?.readText()?.let {
configFile.writeText(it)
runReadAction { DefaultConfigsFactory::class.java.getResource(configName)?.readText() }?.let {
runWriteAction { configFile.writeText(it) }
} ?: logger.warn("There are no file with name $configName")
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.jetbrains.research.tasktracker.properties

import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.application.runWriteAction
import org.jetbrains.research.tasktracker.config.MainTaskTrackerConfig
import java.io.File
import java.io.FileInputStream
Expand All @@ -19,10 +21,10 @@ object PropertiesController {

fun loadProps() = Properties().also { props ->
createPropertiesFile()
FileInputStream(propertiesFile).use { props.load(it) }
runReadAction { FileInputStream(propertiesFile).use { props.load(it) } }
}

private fun createPropertiesFile() {
private fun createPropertiesFile() = runWriteAction {
defaultConfigRoot.mkdirs()
propertiesFile.createNewFile()
createDefaultPropertiesFile()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.jetbrains.research.tasktracker.tracking.logger

import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.runWriteAction
import com.intellij.openapi.util.io.FileUtil
import com.jetbrains.rd.util.AtomicInteger
import org.apache.commons.csv.CSVFormat
Expand All @@ -22,7 +24,9 @@ abstract class BaseLogger {

protected fun log(dataToPrint: Iterable<*>) {
val logPrinter = getActiveLogPrinter()
logPrinter.csvPrinter.printRecord(dataToPrint)
ApplicationManager.getApplication().invokeLater {
runWriteAction { logPrinter.csvPrinter.printRecord(dataToPrint) }
}
}

/**
Expand All @@ -47,19 +51,21 @@ abstract class BaseLogger {
val logFile = createLogFile("$logPrinterFilename${atomicInteger.getAndIncrement()}.csv")
val fileWriter = OutputStreamWriter(FileOutputStream(logFile), StandardCharsets.UTF_8)
val csvPrinter = CSVPrinter(fileWriter, CSVFormat.DEFAULT)
csvPrinter.printRecord(loggedData.headers)
runWriteAction { csvPrinter.printRecord(loggedData.headers) }
logPrinters.add(LogPrinter(csvPrinter, fileWriter, logFile))
return logPrinters.last()
}

private fun createLogFile(fileName: String): File {
private fun createLogFile(fileName: String): File = runWriteAction {
val logFile = File("${MainTaskTrackerConfig.logFilesFolder}/$fileName")
FileUtil.createIfDoesntExist(logFile)
return logFile
logFile
}

fun getLogFiles(): List<File> = logPrinters.map {
it.csvPrinter.flush()
it.logFile
fun getLogFiles(): List<File> = runWriteAction {
logPrinters.map {
it.csvPrinter.flush()
it.logFile
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jetbrains.research.tasktracker.tracking.logger

import com.intellij.openapi.application.runWriteAction
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.editor.Document
import com.intellij.openapi.fileEditor.FileDocumentManager
Expand Down Expand Up @@ -41,17 +42,17 @@ class DocumentLogPrinter {
logPrinters.last()
}

private fun addLogPrinter(project: Project, document: Document): LogPrinter {
private fun addLogPrinter(project: Project, document: Document): LogPrinter = runWriteAction {
logger.info("${MainTaskTrackerConfig.PLUGIN_NAME}: init printer")
val logFile = createLogFile(document)
val fileWriter = OutputStreamWriter(FileOutputStream(logFile), StandardCharsets.UTF_8)
val csvPrinter = CSVPrinter(fileWriter, CSVFormat.DEFAULT)
csvPrinter.printRecord(DocumentLoggedData(project).headers)
logPrinters.add(LogPrinter(csvPrinter, fileWriter, logFile))
return logPrinters.last()
logPrinters.last()
}

private fun createLogFile(document: Document): File {
private fun createLogFile(document: Document): File = runWriteAction {
File(MainTaskTrackerConfig.logFilesFolder).mkdirs()
val trackedFile = FileDocumentManager.getInstance().getFile(document)
logger.info("${MainTaskTrackerConfig.PLUGIN_NAME}: create log file for tracked file ${trackedFile?.name}")
Expand All @@ -60,7 +61,7 @@ class DocumentLogPrinter {
"${trackedFile?.nameWithoutExtension}_${trackedFile.hashCode()}_${document.hashCode()}_$logFilesNumber.csv"
val logFile = File("${MainTaskTrackerConfig.logFilesFolder}/$logFileName")
FileUtil.createIfDoesntExist(logFile)
return logFile
logFile
}

/**
Expand All @@ -76,8 +77,10 @@ class DocumentLogPrinter {
/**
* We need to flush printers before getting their log files.
*/
fun getLogFiles() = logPrinters.map {
it.csvPrinter.flush()
it.logFile
fun getLogFiles() = runWriteAction {
logPrinters.map {
it.csvPrinter.flush()
it.logFile
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jetbrains.research.tasktracker.tracking.logger

import com.intellij.openapi.application.runWriteAction
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.editor.Document
import com.intellij.openapi.project.Project
Expand All @@ -15,7 +16,7 @@ object DocumentLogger {
fun log(project: Project, document: Document) {
val docPrinter = myDocumentsToPrinters.getOrPut(document) { DocumentLogPrinter() }
val logPrinter = docPrinter.getActiveLogPrinter(project, document)
logPrinter.csvPrinter.printRecord(DocumentLoggedData(project).getData(document))
runWriteAction { logPrinter.csvPrinter.printRecord(DocumentLoggedData(project).getData(document)) }
}

fun getDocumentLogPrinter(document: Document): DocumentLogPrinter? = myDocumentsToPrinters[document]
Expand Down

0 comments on commit 0494ec9

Please sign in to comment.