diff --git a/README.md b/README.md index fe78f473..1ba7d398 100644 --- a/README.md +++ b/README.md @@ -76,18 +76,18 @@ Maven: com.simiacryptus skyenet-webui - 1.0.28 + 1.0.29 ``` Gradle: ```groovy -implementation group: 'com.simiacryptus', name: 'skyenet', version: '1.0.28' +implementation group: 'com.simiacryptus', name: 'skyenet', version: '1.0.29' ``` ```kotlin -implementation("com.simiacryptus:skyenet:1.0.28") +implementation("com.simiacryptus:skyenet:1.0.29") ``` ### 🌟 To Use diff --git a/core/src/main/kotlin/com/simiacryptus/skyenet/util/AuthorizationManager.kt b/core/src/main/kotlin/com/simiacryptus/skyenet/util/AuthorizationManager.kt index b818e34f..517be110 100644 --- a/core/src/main/kotlin/com/simiacryptus/skyenet/util/AuthorizationManager.kt +++ b/core/src/main/kotlin/com/simiacryptus/skyenet/util/AuthorizationManager.kt @@ -1,7 +1,5 @@ package com.simiacryptus.skyenet.util -import java.nio.file.Files -import java.nio.file.Paths import java.util.* object AuthorizationManager { @@ -22,7 +20,7 @@ object AuthorizationManager { if (isUserAuthorized("/permissions/${operationType.name.lowercase(Locale.getDefault())}.txt", user)) { log.debug("User {} authorized for {} globally", user, operationType) true - } else if(null != applicationClass) { + } else if (null != applicationClass) { val packagePath = applicationClass.`package`.name.replace('.', '/') val opName = operationType.name.lowercase(Locale.getDefault()) if (isUserAuthorized("/$packagePath/$opName.txt", user)) { @@ -42,14 +40,10 @@ object AuthorizationManager { } private fun isUserAuthorized(permissionPath: String, user: String?): Boolean { - val url = this::class.java.getResource(permissionPath) - if (url == null) { - log.debug("No permissions file found at $permissionPath") - return false - } - val path = Paths.get(url.toURI()) - val lines = Files.readAllLines(path) - return lines.any { it.equals(user, ignoreCase = true) || it == "*" } + return this::class.java.getResourceAsStream(permissionPath)?.use { stream -> + val lines = stream.bufferedReader().readLines() + lines.any { it.equals(user, ignoreCase = true) || it == "*" } + } ?: false } val log = org.slf4j.LoggerFactory.getLogger(AuthorizationManager::class.java) diff --git a/core/src/main/kotlin/com/simiacryptus/skyenet/util/UsageManager.kt b/core/src/main/kotlin/com/simiacryptus/skyenet/util/UsageManager.kt index 3aee74d6..b59e251e 100644 --- a/core/src/main/kotlin/com/simiacryptus/skyenet/util/UsageManager.kt +++ b/core/src/main/kotlin/com/simiacryptus/skyenet/util/UsageManager.kt @@ -13,7 +13,7 @@ object UsageManager { private val scheduler = Executors.newSingleThreadScheduledExecutor() private val txLogFile = File(".skyenet/usage/log.csv") - private val txLogFileWriter by lazy { FileWriter(txLogFile, true) } + @Volatile private var txLogFileWriter: FileWriter private val usagePerSession = HashMap() private val sessionsByUser = HashMap>() private val usersBySession = HashMap>() @@ -21,6 +21,7 @@ object UsageManager { init { txLogFile.parentFile.mkdirs() loadFromLog(txLogFile) + txLogFileWriter = FileWriter(txLogFile, true) scheduler.scheduleAtFixedRate({ saveCounters() }, 1, 1, TimeUnit.HOURS) } @@ -34,6 +35,7 @@ object UsageManager { } } } + @Suppress("MemberVisibilityCanBePrivate") fun writeCompactLog(file: File) { val writer = FileWriter(file) @@ -48,8 +50,33 @@ object UsageManager { } private fun saveCounters() { - txLogFile.renameTo(File(txLogFile.absolutePath + "." + System.currentTimeMillis())) - writeCompactLog(txLogFile) + txLogFileWriter = FileWriter(txLogFile, true) + val timedFile = File(txLogFile.absolutePath + "." + System.currentTimeMillis()) + writeCompactLog(timedFile) + val swapFile = File(txLogFile.absolutePath + ".old") + synchronized(txLogFile) { + try { + txLogFileWriter.close() + } catch (e: Exception) { + log.warn("Error closing log file", e) + } + try { + txLogFile.renameTo(swapFile) + } catch (e: Exception) { + log.warn("Error renaming log file", e) + } + try { + timedFile.renameTo(txLogFile) + } catch (e: Exception) { + log.warn("Error renaming log file", e) + } + try { + swapFile.renameTo(timedFile) + } catch (e: Exception) { + log.warn("Error renaming log file", e) + } + txLogFileWriter = FileWriter(txLogFile, true) + } File(".skyenet/usage/counters.json").writeText(JsonUtil.toJson(usagePerSession)) } @@ -67,9 +94,13 @@ object UsageManager { } sessions.add(sessionId) } - synchronized(txLogFileWriter) { - txLogFileWriter.write("$sessionId,$user,${model.modelName},$tokens\n") - txLogFileWriter.flush() + try { + synchronized(txLogFile) { + txLogFileWriter.write("$sessionId,$user,${model.modelName},$tokens\n") + txLogFileWriter.flush() + } + } catch (e: Exception) { + log.warn("Error incrementing usage", e) } } diff --git a/gradle.properties b/gradle.properties index 3204842a..c7da930c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ # Gradle Releases -> https://github.com/gradle/gradle/releases libraryGroup = com.simiacryptus.skyenet -libraryVersion = 1.0.28 +libraryVersion = 1.0.29 gradleVersion = 7.6.1 # Opt-out flag for bundling Kotlin standard library -> https://plugins.jetbrains.com/docs/intellij/kotlin.html#kotlin-standard-library