Temporary workaround for resource-based data

This commit is contained in:
Alexander Nozik 2023-03-27 13:23:05 +03:00
parent e6bee125d3
commit e4b4fcb39d
2 changed files with 18 additions and 10 deletions

View File

@ -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<Any> = io.readDataDirectory(path, setOf("md","html")) { dataPath, meta ->
public fun SnarkHtmlPlugin.readDirectory(path: Path): DataTree<Any> = io.readDataDirectory(
path,
setOf("md", "html", "yaml", "json")
) { dataPath, meta ->
val fileExtension = meta[FileData.FILE_EXTENSION_KEY].string ?: dataPath.extension
val parser: SnarkParser<Any> = parsers.values.filter { parser ->
fileExtension in parser.fileExtensions

View File

@ -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
}
}