Skip to content

Commit

Permalink
Created custom DoOnceException. Minor update to model cache upToDate.
Browse files Browse the repository at this point in the history
  • Loading branch information
bjsvedin committed Mar 7, 2024
1 parent 5e668ea commit 282d6db
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ModelCache<T : HasId<ID>, ID : Comparable<ID>>(
private val awaiting = ArrayList<Continuation<T?>>()

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) {
Expand Down Expand Up @@ -122,7 +122,7 @@ class ModelCache<T : HasId<ID>, ID : Comparable<ID>>(
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <reified INPUT> task(name: String, noinline implementation: suspend Task.RunningTask<INPUT>.(INPUT) -> Unit) =
inline fun <reified INPUT> task(
name: String,
noinline implementation: suspend Task.RunningTask<INPUT>.(INPUT) -> Unit,
) =
task(name, Serialization.module.serializer<INPUT>(), implementation)

@LightningServerDsl
fun <INPUT> task(name: String, serializer: KSerializer<INPUT>, implementation: suspend Task.RunningTask<INPUT>.(INPUT) -> Unit) =
fun <INPUT> task(
name: String,
serializer: KSerializer<INPUT>,
implementation: suspend Task.RunningTask<INPUT>.(INPUT) -> Unit,
) =
Task(name, serializer, implementation)

@LightningServerDsl
Expand All @@ -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<String>

@LightningServerDsl
Expand All @@ -47,21 +52,23 @@ fun startupOnce(
database: () -> Database,
maxDuration: Duration = 60.seconds,
priority: Double = 0.0,
action: suspend () -> Unit
action: suspend () -> Unit,
): StartupAction {
prepareModels()
return startup(priority) {
doOnce(name, database, maxDuration, priority, action)
}
}

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<ActionHasOccurred>()
Expand All @@ -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 {
Expand Down

0 comments on commit 282d6db

Please sign in to comment.