Skip to content

Commit a37fd7d

Browse files
committed
Dummy update processor
1 parent d05f10c commit a37fd7d

File tree

10 files changed

+130
-4
lines changed

10 files changed

+130
-4
lines changed

core/build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
plugins {
22
kotlin("jvm")
33
}
4+
5+
dependencies {
6+
api(libs.bundles.tgbotapi)
7+
implementation(libs.log4j.api)
8+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package by.jprof.telegram.bot.core
2+
3+
import dev.inmo.tgbotapi.types.update.abstracts.Update
4+
import org.apache.logging.log4j.LogManager
5+
6+
class DummyUpdateProcessor : UpdateProcessor {
7+
companion object {
8+
private val logger = LogManager.getLogger(DummyUpdateProcessor::class.java)
9+
}
10+
11+
override suspend fun process(update: Update) {
12+
logger.debug("Doing nothing with {}", update)
13+
}
14+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package by.jprof.telegram.bot.core
2+
3+
import dev.inmo.tgbotapi.types.update.abstracts.Update
4+
import kotlinx.coroutines.CoroutineExceptionHandler
5+
import kotlinx.coroutines.joinAll
6+
import kotlinx.coroutines.launch
7+
import kotlinx.coroutines.runBlocking
8+
import kotlinx.coroutines.supervisorScope
9+
import org.apache.logging.log4j.LogManager
10+
11+
class UpdateProcessingPipeline(
12+
private val processors: List<UpdateProcessor>
13+
) {
14+
companion object {
15+
private val logger = LogManager.getLogger(UpdateProcessingPipeline::class.java)!!
16+
}
17+
18+
fun process(update: Update) = runBlocking {
19+
supervisorScope {
20+
processors
21+
.map { launch(exceptionHandler(it)) { it.process(update) } }
22+
.joinAll()
23+
}
24+
}
25+
26+
private fun exceptionHandler(updateProcessor: UpdateProcessor) = CoroutineExceptionHandler { _, exception ->
27+
logger.error("{} failed!", updateProcessor::class.simpleName, exception)
28+
}
29+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package by.jprof.telegram.bot.core
2+
3+
import dev.inmo.tgbotapi.types.update.abstracts.Update
4+
5+
interface UpdateProcessor {
6+
suspend fun process(update: Update)
7+
}

gradle/libs.versions.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ aws-lambda-java-core = "1.2.1"
66
aws-lambda-java-log4j2 = "1.2.0"
77
awssdk = "2.16.43"
88

9+
koin = "3.0.1"
10+
911
jackson = "2.12.3"
1012

1113
tgbotapi = "0.33.3"
@@ -23,10 +25,13 @@ aws-lambda-java-core = { group = "com.amazonaws", name = "aws-lambda-java-core",
2325
aws-lambda-java-log4j2 = { group = "com.amazonaws", name = "aws-lambda-java-log4j2", version.ref = "aws-lambda-java-log4j2" }
2426
dynamodb = { group = "software.amazon.awssdk", name = "dynamodb", version.ref = "awssdk" }
2527

28+
koin-core = { group = "io.insert-koin", name = "koin-core", version.ref = "koin" }
29+
2630
jackson-databind = { group = "com.fasterxml.jackson.core", name = "jackson-databind", version.ref = "jackson" }
2731

2832
tgbotapi-core = { group = "dev.inmo", name = "tgbotapi.core", version.ref = "tgbotapi" }
2933

34+
log4j-api = { group = "org.apache.logging.log4j", name = "log4j-api", version.ref = "log4j" }
3035
log4j-core = { group = "org.apache.logging.log4j", name = "log4j-core", version.ref = "log4j" }
3136
log4j-slf4j-impl = { group = "org.apache.logging.log4j", name = "log4j-slf4j-impl", version.ref = "log4j" }
3237

runners/lambda/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ plugins {
55

66
dependencies {
77
implementation(libs.bundles.aws.lambda)
8+
implementation(libs.koin.core)
89
implementation(libs.bundles.tgbotapi)
910
implementation(libs.bundles.log4j)
11+
implementation(project(":core"))
1012
}
Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,51 @@
11
package by.jprof.telegram.bot.runners.lambda
22

3+
import by.jprof.telegram.bot.core.UpdateProcessingPipeline
4+
import by.jprof.telegram.bot.runners.lambda.config.envModule
5+
import by.jprof.telegram.bot.runners.lambda.config.pipelineModule
36
import com.amazonaws.services.lambda.runtime.Context
47
import com.amazonaws.services.lambda.runtime.RequestHandler
58
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPEvent
69
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPResponse
10+
import dev.inmo.tgbotapi.types.update.abstracts.UpdateDeserializationStrategy
11+
import kotlinx.serialization.json.Json
12+
import org.apache.logging.log4j.LogManager
13+
import org.koin.core.component.KoinComponent
14+
import org.koin.core.component.inject
15+
import org.koin.core.context.startKoin
716

817
@Suppress("unused")
9-
class JProf : RequestHandler<APIGatewayV2HTTPEvent, APIGatewayV2HTTPResponse> {
10-
override fun handleRequest(input: APIGatewayV2HTTPEvent, context: Context): APIGatewayV2HTTPResponse {
11-
return APIGatewayV2HTTPResponse
18+
class JProf : RequestHandler<APIGatewayV2HTTPEvent, APIGatewayV2HTTPResponse>, KoinComponent {
19+
companion object {
20+
private val logger = LogManager.getLogger(JProf::class.java)
21+
private val OK = APIGatewayV2HTTPResponse
1222
.builder()
1323
.withStatusCode(200)
14-
.withBody("Hello, world!")
24+
.withHeaders(mapOf(
25+
"Content-Type" to "application/json"
26+
))
27+
.withBody("{}")
1528
.build()
1629
}
30+
31+
init {
32+
startKoin {
33+
modules(envModule, pipelineModule)
34+
}
35+
}
36+
37+
private val json: Json by inject()
38+
private val pipeline: UpdateProcessingPipeline by inject()
39+
40+
override fun handleRequest(input: APIGatewayV2HTTPEvent, context: Context): APIGatewayV2HTTPResponse {
41+
logger.debug("Incoming request: {}", input)
42+
43+
val update = json.decodeFromString(UpdateDeserializationStrategy, input.body ?: return OK)
44+
45+
logger.debug("Parsed update: {}", update)
46+
47+
pipeline.process(update)
48+
49+
return OK
50+
}
1751
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package by.jprof.telegram.bot.runners.lambda.config
2+
3+
import org.koin.core.qualifier.named
4+
import org.koin.dsl.module
5+
6+
val envModule = module {
7+
emptyList<String>().forEach { variable ->
8+
single(named(variable)) {
9+
System.getenv(variable)!!
10+
}
11+
}
12+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package by.jprof.telegram.bot.runners.lambda.config
2+
3+
import by.jprof.telegram.bot.core.DummyUpdateProcessor
4+
import by.jprof.telegram.bot.core.UpdateProcessingPipeline
5+
import by.jprof.telegram.bot.core.UpdateProcessor
6+
import org.koin.core.qualifier.named
7+
import org.koin.dsl.module
8+
9+
val pipelineModule = module {
10+
single {
11+
UpdateProcessingPipeline(getAll())
12+
}
13+
14+
single<UpdateProcessor>(named("DummyUpdateProcessor")) {
15+
DummyUpdateProcessor()
16+
}
17+
}

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ rootProject.name = "jprof_by_bot"
55
include(":utils:dynamodb")
66
include(":votes")
77
include(":votes:dynamodb")
8+
include(":core")
89
include(":runners:lambda")

0 commit comments

Comments
 (0)