Skip to content

Commit 033ff90

Browse files
committed
chore: application changes
1 parent b306681 commit 033ff90

File tree

12 files changed

+108
-67
lines changed

12 files changed

+108
-67
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ jobs:
129129
with:
130130
name: ${{ steps.gradle-build.outputs.artifact_name }}
131131
path: |
132-
backend/jvm/build/libs/jvm-app
132+
backend/jvm/build/libs/jvm
133133
if-no-files-found: error
134134

135135
- name: 🕸 Deploy Wasm, Js & Compose webapp to Github Pages

backend/agent/jfr/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import common.jvmRunArgs
1+
import common.*
22
import kotlin.collections.plus
33

44
plugins {
@@ -12,7 +12,7 @@ description = "JVM JFR Agent!"
1212

1313
application {
1414
mainClass = libs.versions.app.mainclass.get()
15-
applicationDefaultJvmArgs += project.jvmRunArgs
15+
applicationDefaultJvmArgs += ""
1616
}
1717

1818
tasks {

backend/agent/otel/build.gradle.kts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@file:Suppress("UnstableApiUsage")
22

33
import com.github.ajalt.mordant.rendering.TextColors.magenta
4-
import common.byteDisplaySize
4+
import me.saket.bytesize.*
55

66
plugins {
77
java
@@ -39,8 +39,7 @@ val extendedAgent by
3939
doLast {
4040
val agent = archiveFile.get().asFile
4141
logger.lifecycle(
42-
magenta(
43-
"OpenTelemetry Agent: ${agent.absolutePath} (${agent.length().byteDisplaySize()})"))
42+
magenta("OpenTelemetry Agent: ${agent.absolutePath} (${agent.length().decimalBytes})"))
4443
}
4544
}
4645

backend/boot/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import common.jvmRunArgs
1+
import common.*
22

33
plugins {
44
dev.suresh.plugin.kotlin.jvm
@@ -42,4 +42,4 @@ dependencies {
4242
// }
4343
// }
4444

45-
tasks { bootRun { jvmArgs = project.jvmRunArgs } }
45+
tasks { bootRun { jvmArgs = project.runJvmArgs } }

backend/jvm/build.gradle.kts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ plugins {
1818

1919
description = "Ktor backend jvm application"
2020

21-
application {
22-
mainClass = libs.versions.app.mainclass.get()
23-
applicationDefaultJvmArgs += project.jvmRunArgs
24-
}
21+
application { mainClass = libs.versions.app.mainclass.get() }
2522

2623
ktor { fatJar { archiveFileName = "${project.name}-all.jar" } }
2724

@@ -62,7 +59,7 @@ jib {
6259
entrypoint = buildList {
6360
add("java")
6461
add("-javaagent:${appRoot}/otel/otel-javaagent.jar")
65-
addAll(application.applicationDefaultJvmArgs.map { it.replace(tmp, "/tmp/") })
62+
addAll(runJvmArgs.map { it.replace(tmp, "/tmp/") })
6663
add("-cp")
6764
add("@${appRoot}/jib-classpath-file")
6865
add("@${appRoot}/jib-main-class-file")

backend/jvm/src/main/kotlin/dev/suresh/App.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ fun Application.module() {
2828
configureHTTP()
2929
configureSecurity()
3030
errorRoutes()
31+
scheduledTasks()
3132

3233
routing {
3334
adminRoutes()

backend/jvm/src/main/kotlin/dev/suresh/lang/VThread.kt

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ package dev.suresh.lang
22

33
import dev.suresh.*
44
import io.github.oshai.kotlinlogging.KLogger
5-
import java.util.concurrent.StructuredTaskScope
5+
import java.lang.classfile.ClassFile
66
import java.util.stream.Gatherers
7+
import kotlin.jvm.optionals.getOrNull
78
import kotlin.metadata.jvm.KotlinClassMetadata
8-
import kotlin.time.Duration.Companion.seconds
9-
import kotlinx.datetime.Clock
109
import kotlinx.datetime.LocalDateTime
11-
import kotlinx.datetime.toJavaInstant
1210
import langFeatures
1311
import stdlibFeatures
1412

@@ -33,41 +31,41 @@ object VThread {
3331

3432
private fun KLogger.structuredConcurrency() {
3533
info { "Structured concurrency..." }
36-
val taskList =
37-
StructuredTaskScope<String>().use { sts ->
38-
val start = Clock.System.now()
39-
val tasks =
40-
(1..100).map {
41-
sts.fork {
42-
when (it) {
43-
in 1..40 -> "Task $it"
44-
in 41..60 -> kotlin.error("Error in task $it")
45-
else -> {
46-
while (!Thread.currentThread().isInterrupted) {
47-
debug { "Task $it ..." }
48-
Thread.sleep(100)
49-
}
50-
"Task $it"
51-
}
52-
}
53-
}
54-
}
55-
runCatching { sts.joinUntil(start.plus(1.seconds).toJavaInstant()) }
56-
tasks
57-
}
58-
59-
info { "Total Tasks: ${taskList.size}" }
60-
val states = taskList.groupBy { it.state() }
61-
states.forEach { (t, u) -> info { "$t --> ${u.size}" } }
62-
check(states[StructuredTaskScope.Subtask.State.SUCCESS]?.size == 40)
63-
check(states[StructuredTaskScope.Subtask.State.FAILED]?.size == 20)
64-
check(states[StructuredTaskScope.Subtask.State.UNAVAILABLE]?.size == 40)
65-
66-
StructuredTaskScope.ShutdownOnFailure().use {
67-
val task = it.fork { "Virtual thread on ${Lang("Kotlin")} ${platform.name}!" }
68-
it.join().throwIfFailed()
69-
info { task.get() }
70-
}
34+
// val taskList =
35+
// StructuredTaskScope<String>().use { sts ->
36+
// val start = Clock.System.now()
37+
// val tasks =
38+
// (1..100).map {
39+
// sts.fork {
40+
// when (it) {
41+
// in 1..40 -> "Task $it"
42+
// in 41..60 -> kotlin.error("Error in task $it")
43+
// else -> {
44+
// while (!Thread.currentThread().isInterrupted) {
45+
// debug { "Task $it ..." }
46+
// Thread.sleep(100)
47+
// }
48+
// "Task $it"
49+
// }
50+
// }
51+
// }
52+
// }
53+
// runCatching { sts.joinUntil(start.plus(1.seconds).toJavaInstant()) }
54+
// tasks
55+
// }
56+
//
57+
// info { "Total Tasks: ${taskList.size}" }
58+
// val states = taskList.groupBy { it.state() }
59+
// states.forEach { (t, u) -> info { "$t --> ${u.size}" } }
60+
// check(states[StructuredTaskScope.Subtask.State.SUCCESS]?.size == 40)
61+
// check(states[StructuredTaskScope.Subtask.State.FAILED]?.size == 20)
62+
// check(states[StructuredTaskScope.Subtask.State.UNAVAILABLE]?.size == 40)
63+
//
64+
// StructuredTaskScope.ShutdownOnFailure().use {
65+
// val task = it.fork { "Virtual thread on ${Lang("Kotlin")} ${platform.name}!" }
66+
// it.join().throwIfFailed()
67+
// info { task.get() }
68+
// }
7169
}
7270

7371
private fun gatherers(): List<String> {
@@ -90,11 +88,11 @@ object VThread {
9088
}
9189

9290
fun classFileApi() {
93-
// val codeModel =
94-
// Classfile.of()
95-
// .parse(Class.forName("AppKt").toBytes())
96-
// .methods()
97-
// .filter { it.methodName().equalsString("main") }
98-
// .firstNotNullOfOrNull { it.code().getOrNull() }
99-
// codeModel?.elementList()?.forEach { log.info {it.toString()} }
91+
val codeModel =
92+
ClassFile.of()
93+
.parse(Class.forName("dev.suresh.AppKt").toBytes())
94+
.methods()
95+
.filter { it.methodName().equalsString("main") }
96+
.firstNotNullOfOrNull { it.code().getOrNull() }
97+
codeModel?.elementList()?.forEach { log.info { it.toString() } }
10098
}

backend/jvm/src/main/kotlin/dev/suresh/plugins/Error.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ fun Application.errorRoutes() {
1919
}
2020
}
2121

22+
status(HttpStatusCode.NotFound) { call, _ ->
23+
call.respondRedirect("/app", permanent = true) // 301
24+
}
25+
2226
exception<Throwable> { call, cause ->
2327
val status =
2428
when (cause) {
@@ -30,10 +34,6 @@ fun Application.errorRoutes() {
3034
call.respondError(status, cause.message ?: "Unknown error", cause)
3135
}
3236

33-
status(HttpStatusCode.NotFound) { call, _ ->
34-
call.respondRedirect("/app", permanent = true) // 301
35-
}
36-
3737
unhandled {
3838
it.respondError(
3939
HttpStatusCode.NotFound, "The requested URL ${it.request.path()} was not found")

backend/jvm/src/main/kotlin/dev/suresh/plugins/Http.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import io.ktor.serialization.kotlinx.json.*
88
import io.ktor.server.application.*
99
import io.ktor.server.plugins.*
1010
import io.ktor.server.plugins.autohead.*
11-
import io.ktor.server.plugins.callid.*
1211
import io.ktor.server.plugins.calllogging.*
1312
import io.ktor.server.plugins.compression.*
1413
import io.ktor.server.plugins.contentnegotiation.*
@@ -17,6 +16,7 @@ import io.ktor.server.plugins.defaultheaders.*
1716
import io.ktor.server.plugins.forwardedheaders.*
1817
import io.ktor.server.plugins.hsts.*
1918
import io.ktor.server.plugins.partialcontent.*
19+
import io.ktor.server.plugins.ratelimit.*
2020
import io.ktor.server.request.*
2121
import io.ktor.server.resources.*
2222
import io.ktor.server.response.*
@@ -90,6 +90,20 @@ fun Application.configureHTTP() {
9090
}
9191
}
9292

93+
install(RateLimit) {
94+
global {
95+
// requestKey {}
96+
rateLimiter(limit = 100, refillPeriod = 60.seconds)
97+
}
98+
99+
register { rateLimiter(limit = 5, refillPeriod = 60.seconds) }
100+
101+
register(RateLimitName("api")) {
102+
requestKey {}
103+
rateLimiter(limit = 100, refillPeriod = 60.seconds)
104+
}
105+
}
106+
93107
install(HSTS)
94108

95109
// install(CallId) {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package dev.suresh.routes
2+
3+
import dev.suresh.virtualThreadScope
4+
import io.github.kevincianfarini.cardiologist.*
5+
import io.ktor.server.application.*
6+
import io.ktor.util.logging.*
7+
import io.opentelemetry.instrumentation.annotations.*
8+
import kotlinx.coroutines.launch
9+
import kotlin.time.Duration.Companion.seconds
10+
import kotlinx.datetime.*
11+
12+
fun Application.scheduledTasks() {
13+
log.info("Starting scheduled tasks...")
14+
virtualThreadScope.launch {
15+
Clock.System.fixedPeriodPulse(10.seconds).beat(RecurringJobMode.Skip) { scheduled, occurred ->
16+
context(log){
17+
task("Task at ${occurred.toLocalDateTime(TimeZone.currentSystemDefault())}")
18+
}
19+
}
20+
}
21+
}
22+
23+
@WithSpan("scheduled-task")
24+
context(log: Logger)
25+
fun task(name: String) {
26+
try {
27+
log.warn("Running $name")
28+
// log.info("Scheduled task: ${MediaApiClient().images().size}")
29+
} catch (e: Exception) {
30+
log.error("Failed to run scheduled task: ${e.message}", e)
31+
}
32+
}

0 commit comments

Comments
 (0)