Fixed locking statistics files when multiple gradle daemons started

This commit is contained in:
Andrey Uskov
2020-01-27 17:59:46 +03:00
parent a68a6b77df
commit 9ae3b2cf43
3 changed files with 7 additions and 5 deletions
@@ -175,9 +175,10 @@ class KotlinGradleFUSLogger : StartupActivity, DumbAware, Runnable {
try {
for (gradleUserHome in gradleUserDirs) {
BuildSessionLogger.listProfileFiles(File(gradleUserHome, STATISTICS_FOLDER_NAME))?.forEach { statisticFile ->
var fileWasRead = true
try {
var previousEvent: MetricsContainer? = null
MetricsContainer.readFromFile(statisticFile) { metricContainer ->
fileWasRead = MetricsContainer.readFromFile(statisticFile) { metricContainer ->
processMetricsContainer(metricContainer, previousEvent)
previousEvent = metricContainer
}
@@ -185,7 +186,7 @@ class KotlinGradleFUSLogger : StartupActivity, DumbAware, Runnable {
Logger.getInstance(KotlinFUSLogger::class.java)
.info("Failed to process file ${statisticFile.absolutePath}: ${e.message}", e)
} finally {
if (!statisticFile.delete()) {
if (fileWasRead && !statisticFile.delete()) {
Logger.getInstance(KotlinFUSLogger::class.java)
.warn("[FUS] Failed to delete file ${statisticFile.absolutePath}")
}
@@ -24,7 +24,7 @@ class FileRecordLogger(file: File) : IRecordLogger {
init {
lock = try {
channel.lock()
channel.tryLock() ?: throw IOException("Could not acquire an exclusive lock of file ${file.name}")
} catch (e: Exception) {
channel.close()
// wrap in order to unify with FileOverlappingException
@@ -43,9 +43,9 @@ class MetricsContainer : IStatisticsValuesConsumer {
private val numericalMetricsMap = NumericalMetrics.values().associateBy(NumericalMetrics::name)
fun readFromFile(file: File, consumer: (MetricsContainer) -> Unit) {
fun readFromFile(file: File, consumer: (MetricsContainer) -> Unit): Boolean {
val channel = FileChannel.open(Paths.get(file.toURI()), StandardOpenOption.WRITE, StandardOpenOption.READ)
channel.lock()
channel.tryLock() ?: return false
val inputStream = Channels.newInputStream(channel)
try {
@@ -86,6 +86,7 @@ class MetricsContainer : IStatisticsValuesConsumer {
} finally {
channel.close()
}
return true
}
}