From 39ba602ccf5f9bc6b5ac018c932ae897dd3b46c7 Mon Sep 17 00:00:00 2001 From: Brady Svedin Date: Thu, 4 Apr 2024 10:29:44 -0600 Subject: [PATCH] Updates to the model cache invalidation functions, and the DebugExceptionReporter for better sdk compatibility. --- .../lightningserver/db/MockModelCollection.kt | 2 +- .../lightningserver/db/ModelCache.kt | 14 ++++---- .../lightningserver/db/ModelRestEndpoints.kt | 3 +- .../exceptions/DebugExceptionReporter.kt | 32 +++++++++++++------ 4 files changed, 33 insertions(+), 18 deletions(-) diff --git a/client/src/commonMain/kotlin/com/lightningkite/lightningserver/db/MockModelCollection.kt b/client/src/commonMain/kotlin/com/lightningkite/lightningserver/db/MockModelCollection.kt index bce8f1fd..e39829ee 100644 --- a/client/src/commonMain/kotlin/com/lightningkite/lightningserver/db/MockModelCollection.kt +++ b/client/src/commonMain/kotlin/com/lightningkite/lightningserver/db/MockModelCollection.kt @@ -32,7 +32,7 @@ class MockModelCollection, ID : Comparable>(val serializer: KS actionPerformed() } - override suspend fun invalidate() { + override fun invalidate() { actionPerformed() } 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 471cc8e4..fa5a9c0a 100644 --- a/client/src/commonMain/kotlin/com/lightningkite/lightningserver/db/ModelCache.kt +++ b/client/src/commonMain/kotlin/com/lightningkite/lightningserver/db/ModelCache.kt @@ -2,13 +2,10 @@ package com.lightningkite.lightningserver.db import com.lightningkite.lightningdb.* import com.lightningkite.kiteui.* -import com.lightningkite.kiteui.reactive.BaseReadable import com.lightningkite.kiteui.reactive.Readable import com.lightningkite.kiteui.reactive.ReadableState import com.lightningkite.kiteui.reactive.await import kotlinx.serialization.KSerializer -import kotlin.coroutines.Continuation -import kotlin.coroutines.resume import kotlin.time.Duration import kotlin.time.Duration.Companion.minutes @@ -95,7 +92,7 @@ class ModelCache, ID : Comparable>( virtualDelete() } - override suspend fun invalidate() { + override fun invalidate() { invalid = true } @@ -112,7 +109,12 @@ class ModelCache, ID : Comparable>( } } - var totalInvalidation: Double = 0.0 + private var totalInvalidation: Double = 0.0 + + override fun totallyInvalidate(){ + totalInvalidation = clockMillis() + cache.values.forEach { it.invalidate() } + } inner class ListImpl(val query: Query) : Readable> { var live: Int = 0 @@ -355,7 +357,7 @@ class ModelCache, ID : Comparable>( } override suspend fun bulkModify(bulkUpdate: MassModification): Int { - totalInvalidation = clockMillis() + totallyInvalidate() return skipCache.bulkModify(bulkUpdate) } diff --git a/client/src/commonMain/kotlin/com/lightningkite/lightningserver/db/ModelRestEndpoints.kt b/client/src/commonMain/kotlin/com/lightningkite/lightningserver/db/ModelRestEndpoints.kt index e1569645..9525b785 100644 --- a/client/src/commonMain/kotlin/com/lightningkite/lightningserver/db/ModelRestEndpoints.kt +++ b/client/src/commonMain/kotlin/com/lightningkite/lightningserver/db/ModelRestEndpoints.kt @@ -38,7 +38,7 @@ interface WritableModel : Writable { val serializer: KSerializer suspend fun modify(modification: Modification): T? suspend fun delete(): Unit - suspend fun invalidate(): Unit + fun invalidate(): Unit } interface ModelCollection, ID : Comparable> { @@ -54,5 +54,6 @@ interface ModelCollection, ID : Comparable> { interface CachingModelRestEndpoints, ID : Comparable> : ModelCollection { val skipCache: ModelRestEndpoints + fun totallyInvalidate() } diff --git a/server-core/src/main/kotlin/com/lightningkite/lightningserver/exceptions/DebugExceptionReporter.kt b/server-core/src/main/kotlin/com/lightningkite/lightningserver/exceptions/DebugExceptionReporter.kt index f8502b91..e5325cdc 100644 --- a/server-core/src/main/kotlin/com/lightningkite/lightningserver/exceptions/DebugExceptionReporter.kt +++ b/server-core/src/main/kotlin/com/lightningkite/lightningserver/exceptions/DebugExceptionReporter.kt @@ -1,12 +1,14 @@ package com.lightningkite.lightningserver.exceptions +import com.lightningkite.lightningserver.auth.Authentication import com.lightningkite.lightningserver.core.ServerPath import com.lightningkite.lightningserver.http.get import com.lightningkite.lightningserver.logger +import com.lightningkite.lightningserver.typed.api import com.lightningkite.lightningserver.typed.typed -import kotlinx.datetime.Clock import com.lightningkite.now import kotlinx.datetime.Instant +import java.net.NetworkInterface /** * An ExceptionReporter implementation that logs all reports out to a logger using debug and stores the most recent 100 exceptions. @@ -15,6 +17,10 @@ import kotlinx.datetime.Instant */ object DebugExceptionReporter : ExceptionReporter { val previousErrors = ArrayList>() + val server: String = System.getenv("AWS_LAMBDA_LOG_STREAM_NAME")?.takeUnless { it.isEmpty() } + ?: NetworkInterface.getNetworkInterfaces().toList().sortedBy { it.name } + .firstOrNull()?.hardwareAddress?.sumOf { it.hashCode() }?.toString(16) ?: "?" + override suspend fun report(t: Throwable, context: Any?): Boolean { logger.debug( """ @@ -28,18 +34,24 @@ Exception Reported: return true } - val exceptionListEndpoint = ServerPath.root.path("exceptions").get.typed( + val exceptionListEndpoint = ServerPath.root.path("exceptions").get.api( summary = "List Recent Exceptions", description = "Lists the most recent 100 exceptions to have occurred on this server", errorCases = listOf(), - implementation = { user: Unit, input: Unit -> - previousErrors.map { - Triple( - it.first, - it.second.stackTraceToString(), - it.third.toString() - ) - } + authOptions = Authentication.isDeveloper, + implementation = { _: Unit -> + previousErrors + .sortedBy { it.first } + .mapIndexed { index, it -> + ReportedExceptionGroup( + _id = index, + lastOccurredAt = it.first, + context = it.third.toString(), + message = it.second.message ?: "", + trace = it.second.stackTraceToString(), + server = server, + ) + } } ) }