diff --git a/client/src/commonMain/kotlin/com/lightningkite/lightningserver/db/ModelCache.kt b/client/src/commonMain/kotlin/com/lightningkite/lightningserver/db/ModelCache.kt index e27817a0..5ed47577 100644 --- a/client/src/commonMain/kotlin/com/lightningkite/lightningserver/db/ModelCache.kt +++ b/client/src/commonMain/kotlin/com/lightningkite/lightningserver/db/ModelCache.kt @@ -30,7 +30,7 @@ class ModelCache, ID : Comparable>( private val awaiting = ArrayList>() val inUse: Boolean get() = listeners.isNotEmpty() || awaiting.isNotEmpty() - val upToDate: Boolean get() = (ready && live > 0) || clockMillis() - lastSet < cacheMs + val upToDate: Boolean get() = ready && (live > 0 || clockMillis() - lastSet < cacheMs) var value: T? = null set(value) { @@ -122,7 +122,7 @@ class ModelCache, ID : Comparable>( var live: Int = 0 var lastSet: Double = 0.0 val now = clockMillis() - val upToDate: Boolean get() = (ready && live > 0) || (now - lastSet < cacheMs && now > totalInvalidation) + val upToDate: Boolean get() = ready && (live > 0 || (now - lastSet < cacheMs && now > totalInvalidation)) val comparator = query.orderBy.comparator ?: compareBy { it._id } var complete: Boolean = false diff --git a/server-core/src/main/kotlin/com/lightningkite/lightningserver/tasks/dsl.kt b/server-core/src/main/kotlin/com/lightningkite/lightningserver/tasks/dsl.kt index 10dbe098..a2aeb1f3 100644 --- a/server-core/src/main/kotlin/com/lightningkite/lightningserver/tasks/dsl.kt +++ b/server-core/src/main/kotlin/com/lightningkite/lightningserver/tasks/dsl.kt @@ -6,23 +6,28 @@ import com.lightningkite.lightningdb.* import com.lightningkite.lightningserver.core.LightningServerDsl import com.lightningkite.lightningserver.exceptions.exceptionSettings import com.lightningkite.lightningserver.serialization.Serialization -import kotlinx.coroutines.CoroutineScope -import kotlinx.datetime.Clock import com.lightningkite.now +import kotlinx.datetime.Instant import kotlinx.serialization.KSerializer import kotlinx.serialization.Serializable import kotlinx.serialization.UseContextualSerialization import kotlinx.serialization.serializer -import kotlinx.datetime.Instant import kotlin.time.Duration import kotlin.time.Duration.Companion.seconds @LightningServerDsl -inline fun task(name: String, noinline implementation: suspend Task.RunningTask.(INPUT) -> Unit) = +inline fun task( + name: String, + noinline implementation: suspend Task.RunningTask.(INPUT) -> Unit, +) = task(name, Serialization.module.serializer(), implementation) @LightningServerDsl -fun task(name: String, serializer: KSerializer, implementation: suspend Task.RunningTask.(INPUT) -> Unit) = +fun task( + name: String, + serializer: KSerializer, + implementation: suspend Task.RunningTask.(INPUT) -> Unit, +) = Task(name, serializer, implementation) @LightningServerDsl @@ -38,7 +43,7 @@ data class ActionHasOccurred( override val _id: String, val started: Instant? = null, val completed: Instant? = null, - val errorMessage: String? = null + val errorMessage: String? = null, ) : HasId @LightningServerDsl @@ -47,7 +52,7 @@ fun startupOnce( database: () -> Database, maxDuration: Duration = 60.seconds, priority: Double = 0.0, - action: suspend () -> Unit + action: suspend () -> Unit, ): StartupAction { prepareModels() return startup(priority) { @@ -55,13 +60,15 @@ fun startupOnce( } } +private class DoOnceException(message: String? = null, cause: Throwable? = null) : Exception(message, cause) + @LightningServerDsl suspend fun doOnce( name: String, database: () -> Database, maxDuration: Duration = 60.seconds, priority: Double = 0.0, - action: suspend () -> Unit + action: suspend () -> Unit, ) { prepareModels() val a = database().collection() @@ -87,7 +94,7 @@ suspend fun doOnce( } ) } catch (e: Exception) { - exceptionSettings().report(e, "doOnce: $name") + exceptionSettings().report(DoOnceException(cause = e), "doOnce: $name") a.updateOneById( name, modification {