From e4b4fcb39d5cc935d6c5aa52db2644a64368538b Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Mon, 27 Mar 2023 13:23:05 +0300 Subject: [PATCH] Temporary workaround for resource-based data --- .../kscience/snark/html/SnarkHtmlPlugin.kt | 7 ++++--- .../space/kscience/snark/ktor/extractData.kt | 21 ++++++++++++------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/snark-html/src/main/kotlin/space/kscience/snark/html/SnarkHtmlPlugin.kt b/snark-html/src/main/kotlin/space/kscience/snark/html/SnarkHtmlPlugin.kt index ec5c23e..2da57c1 100644 --- a/snark-html/src/main/kotlin/space/kscience/snark/html/SnarkHtmlPlugin.kt +++ b/snark-html/src/main/kotlin/space/kscience/snark/html/SnarkHtmlPlugin.kt @@ -3,7 +3,6 @@ package space.kscience.snark.html import io.ktor.utils.io.core.readBytes import space.kscience.dataforge.context.* import space.kscience.dataforge.data.DataTree -import space.kscience.dataforge.io.IOFormat import space.kscience.dataforge.io.IOPlugin import space.kscience.dataforge.io.IOReader import space.kscience.dataforge.io.JsonMetaFormat @@ -21,7 +20,6 @@ import space.kscience.dataforge.workspace.readDataDirectory import space.kscience.snark.SnarkParser import java.nio.file.Path import kotlin.io.path.extension -import kotlin.reflect.KClass /** * A plugin used for rendering a [DataTree] as HTML @@ -91,7 +89,10 @@ public class SnarkHtmlPlugin : AbstractPlugin() { } @OptIn(DFExperimental::class) -public fun SnarkHtmlPlugin.readDirectory(path: Path): DataTree = io.readDataDirectory(path, setOf("md","html")) { dataPath, meta -> +public fun SnarkHtmlPlugin.readDirectory(path: Path): DataTree = io.readDataDirectory( + path, + setOf("md", "html", "yaml", "json") +) { dataPath, meta -> val fileExtension = meta[FileData.FILE_EXTENSION_KEY].string ?: dataPath.extension val parser: SnarkParser = parsers.values.filter { parser -> fileExtension in parser.fileExtensions diff --git a/snark-ktor/src/main/kotlin/space/kscience/snark/ktor/extractData.kt b/snark-ktor/src/main/kotlin/space/kscience/snark/ktor/extractData.kt index d3cdb57..04daf87 100644 --- a/snark-ktor/src/main/kotlin/space/kscience/snark/ktor/extractData.kt +++ b/snark-ktor/src/main/kotlin/space/kscience/snark/ktor/extractData.kt @@ -2,10 +2,7 @@ package space.kscience.snark.ktor import io.ktor.server.application.Application import io.ktor.server.application.log -import space.kscience.dataforge.context.info -import space.kscience.dataforge.context.logger -import java.net.URI -import java.nio.file.FileSystems +import io.ktor.server.config.tryGetString import java.nio.file.Files import java.nio.file.Path import java.time.LocalDateTime @@ -43,15 +40,17 @@ private const val BUILD_DATE_FILE = "/buildDate" /** * Prepare the data cache directory for snark. Clear data if it is outdated. * TODO make internal + * + * @return true if cache is valid and false if it is reset */ -fun Application.prepareSnarkDataCacheDirectory(dataPath: Path) { +fun Application.prepareSnarkDataCacheDirectory(dataPath: Path): Boolean { // Clear data directory if it is outdated val deployDate = dataPath.resolve(DEPLOY_DATE_FILE).takeIf { it.exists() } ?.readText()?.let { LocalDateTime.parse(it) } val buildDate = javaClass.getResource(BUILD_DATE_FILE)?.readText()?.let { LocalDateTime.parse(it) } - val inProduction: Boolean = environment.config.propertyOrNull("ktor.environment.production") != null + val inProduction: Boolean = environment.config.tryGetString("ktor.environment.production") == "true" if (inProduction) { log.info("Production mode activated") @@ -59,7 +58,11 @@ fun Application.prepareSnarkDataCacheDirectory(dataPath: Path) { log.info("Deploy date: $deployDate") } - if (deployDate != null && buildDate != null && buildDate.isAfter(deployDate)) { + if (!dataPath.exists()) { + dataPath.createDirectories() + dataPath.resolve(DEPLOY_DATE_FILE).writeText(LocalDateTime.now().toString()) + return false + } else if (deployDate != null && buildDate != null && buildDate.isAfter(deployDate)) { log.info("Outdated data. Resetting data directory.") Files.walk(dataPath) @@ -69,6 +72,7 @@ fun Application.prepareSnarkDataCacheDirectory(dataPath: Path) { //Writing deploy date file dataPath.createDirectories() dataPath.resolve(DEPLOY_DATE_FILE).writeText(LocalDateTime.now().toString()) + return false } else if (inProduction && deployDate == null && buildDate != null) { val date = LocalDateTime.now().toString() @@ -76,5 +80,8 @@ fun Application.prepareSnarkDataCacheDirectory(dataPath: Path) { //Writing deploy date in production mode if it does not exist dataPath.createDirectories() dataPath.resolve(DEPLOY_DATE_FILE).writeText(date) + return false + } else { + return true } } \ No newline at end of file