Skip to content

Commit

Permalink
Updates to the model cache invalidation functions, and the DebugExcep…
Browse files Browse the repository at this point in the history
…tionReporter for better sdk compatibility.
  • Loading branch information
bjsvedin committed Apr 4, 2024
1 parent 5321a61 commit 39ba602
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
Expand Up @@ -32,7 +32,7 @@ class MockModelCollection<T : HasId<ID>, ID : Comparable<ID>>(val serializer: KS
actionPerformed()
}

override suspend fun invalidate() {
override fun invalidate() {
actionPerformed()
}

Expand Down
Expand Up @@ -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

Expand Down Expand Up @@ -95,7 +92,7 @@ class ModelCache<T : HasId<ID>, ID : Comparable<ID>>(
virtualDelete()
}

override suspend fun invalidate() {
override fun invalidate() {
invalid = true
}

Expand All @@ -112,7 +109,12 @@ class ModelCache<T : HasId<ID>, ID : Comparable<ID>>(
}
}

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<T>) : Readable<List<T>> {
var live: Int = 0
Expand Down Expand Up @@ -355,7 +357,7 @@ class ModelCache<T : HasId<ID>, ID : Comparable<ID>>(
}

override suspend fun bulkModify(bulkUpdate: MassModification<T>): Int {
totalInvalidation = clockMillis()
totallyInvalidate()
return skipCache.bulkModify(bulkUpdate)
}

Expand Down
Expand Up @@ -38,7 +38,7 @@ interface WritableModel<T> : Writable<T?> {
val serializer: KSerializer<T>
suspend fun modify(modification: Modification<T>): T?
suspend fun delete(): Unit
suspend fun invalidate(): Unit
fun invalidate(): Unit
}

interface ModelCollection<T : HasId<ID>, ID : Comparable<ID>> {
Expand All @@ -54,5 +54,6 @@ interface ModelCollection<T : HasId<ID>, ID : Comparable<ID>> {

interface CachingModelRestEndpoints<T : HasId<ID>, ID : Comparable<ID>> : ModelCollection<T, ID> {
val skipCache: ModelRestEndpoints<T, ID>
fun totallyInvalidate()
}

@@ -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.
Expand All @@ -15,6 +17,10 @@ import kotlinx.datetime.Instant
*/
object DebugExceptionReporter : ExceptionReporter {
val previousErrors = ArrayList<Triple<Instant, Throwable, Any?>>()
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(
"""
Expand All @@ -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,
)
}
}
)
}

0 comments on commit 39ba602

Please sign in to comment.