Skip to content
Merged

Dev #134

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/build.main.kts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ val wbServices = listOf(
"wiertarbot-ttrs-api",
setOf("services/ttrs-api/**"),
),
Service(
"cex-api",
"wiertarbot-cex-api",
setOf("services/cex-api/**"),
),
Service(
"migration",
"wiertarbot-migration",
Expand Down
43 changes: 42 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ on:
description: 'Services to build'
type: 'string'
required: true
default: 'core,connector-fb,connector-telegram,download-api,ttrs-api,migration'
default: 'core,connector-fb,connector-telegram,download-api,ttrs-api,cex-api,migration'
concurrency:
group: '${{ github.ref }}'
cancel-in-progress: true
Expand Down Expand Up @@ -44,6 +44,7 @@ jobs:
connector-telegram: '${{ steps.step-1.outputs.connector-telegram }}'
download-api: '${{ steps.step-1.outputs.download-api }}'
ttrs-api: '${{ steps.step-1.outputs.ttrs-api }}'
cex-api: '${{ steps.step-1.outputs.cex-api }}'
migration: '${{ steps.step-1.outputs.migration }}'
steps:
- id: 'step-0'
Expand Down Expand Up @@ -82,6 +83,8 @@ jobs:
- 'services/download-api/**'
ttrs-api:
- 'services/ttrs-api/**'
cex-api:
- 'services/cex-api/**'
migration:
- 'services/migration/**'
gradle-build:
Expand Down Expand Up @@ -346,6 +349,43 @@ jobs:
${{ secrets.DOCKERHUB_USER }}/wiertarbot-ttrs-api:latest
${{ secrets.DOCKERHUB_USER }}/wiertarbot-ttrs-api:ci-${{ github.run_id }}
push: '${{ github.ref_name == ''main'' && (github.event_name == ''push'' || github.event_name == ''workflow_dispatch'') }}'
docker-cex-api:
runs-on: 'ubuntu-latest'
needs:
- 'gradle-build'
- 'detect-changes'
- 'check_yaml_consistency'
if: '${{ needs.detect-changes.outputs.cex-api == ''true'' || contains(github.event.inputs.services, ''cex-api'') }}'
steps:
- id: 'step-0'
name: 'Check out'
uses: 'actions/checkout@v6'
- id: 'step-1'
name: 'Setup docker buildx'
uses: 'docker/setup-buildx-action@v3'
- id: 'step-2'
name: 'Login to docker registry'
uses: 'docker/login-action@v3'
with:
username: '${{ secrets.DOCKER_USERNAME }}'
password: '${{ secrets.DOCKER_PASSWORD }}'
if: '${{ github.ref_name == ''main'' && (github.event_name == ''push'' || github.event_name == ''workflow_dispatch'') }}'
- id: 'step-3'
name: 'Download build artifacts'
uses: 'actions/download-artifact@v6'
with:
name: 'jars'
- id: 'step-4'
name: 'Build docker image'
uses: 'docker/build-push-action@v6'
with:
context: 'services/cex-api'
file: 'services/cex-api/Dockerfile'
platforms: 'linux/amd64'
tags: |-
${{ secrets.DOCKERHUB_USER }}/wiertarbot-cex-api:latest
${{ secrets.DOCKERHUB_USER }}/wiertarbot-cex-api:ci-${{ github.run_id }}
push: '${{ github.ref_name == ''main'' && (github.event_name == ''push'' || github.event_name == ''workflow_dispatch'') }}'
docker-migration:
runs-on: 'ubuntu-latest'
needs:
Expand Down Expand Up @@ -391,6 +431,7 @@ jobs:
- 'docker-connector-telegram'
- 'docker-download-api'
- 'docker-ttrs-api'
- 'docker-cex-api'
- 'docker-migration'
- 'check_yaml_consistency'
if: '${{ github.ref_name == ''main'' && (github.event_name == ''push'' || github.event_name == ''workflow_dispatch'') }}'
Expand Down
6 changes: 4 additions & 2 deletions Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ PROFILES = {
"telegram": ["connector-telegram", "build-connector-telegram"],
"fb": ["connector-fb", "build-connector-fb"],
"api": ["ttrs-api", "download-api", "build-download-api"],
"cex": ["cex-api"],
"core": ["core", "build-core"]
}
COMPOSE_LABELS = {
"WiertarBot": ["core", "connector-telegram", "connector-fb"],
"API": ["ttrs-api", "download-api"],
"API": ["ttrs-api", "download-api", "cex-api"],
"Infrastructure": ["minio", "rabbitmq", "db"],
}

Expand Down Expand Up @@ -56,7 +57,7 @@ build_service(
build_service(
name="wiertarbot-connector-telegram",
service_name="connector-telegram",
deps=deps("services/connector-telegram", CONNECTOR_DEPS),
deps=deps("services/connector-telegram", CONNECTOR_DEPS) + deps("libs/telegram-kt"),
label="WiertarBot",
)

Expand All @@ -74,6 +75,7 @@ build_service(
label="API",
)

docker_build(image("wiertarbot-cex-api"), "services/cex-api")
docker_build(image("wiertarbot-ttrs-api"), "services/ttrs-api", platform="linux/amd64")
docker_build(image("wiertarbot-migration"), "services/migration")

Expand Down
2 changes: 2 additions & 0 deletions dev/connector-telegram.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
wiertarbot:
telegram:
token: ${TELEGRAM_TOKEN}
connector:
port: 8081

spring:
r2dbc:
Expand Down
6 changes: 5 additions & 1 deletion dev/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ services:
image: kugo12/wiertarbot-ttrs-api
platform: linux/amd64
env_file: ../.env
cex-api:
image: kugo12/wiertarbot-cex-api
env_file: ../.env
ports: [ "8088:8080" ]

connector-telegram:
image: kugo12/wiertarbot-connector-telegram
Expand All @@ -17,7 +21,7 @@ services:
links:
- db
- rabbitmq
ports: [ "8088:8080" ]
ports: [ "8087:8081", "8086:8080" ]
connector-fb:
image: kugo12/wiertarbot-connector-fb
links:
Expand Down
7 changes: 3 additions & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ caffeine = "3.2.3"
scrimage = "4.3.5"
sentry = "8.28.0"
skrape = "1.2.2"
inmo-telegram = "24.0.2"
kotlin-aws-sdk = "1.5.22"
okhttp = "5.3.2"
jaffree = "2024.08.29"
logback = "1.5.22"
slf4j = "2.0.17"
advrieze-xmlutil = "0.91.3"

commons-codec = "1.20.0"
Expand Down Expand Up @@ -91,8 +91,6 @@ sentry-logback = { module = "io.sentry:sentry-logback", version.ref = "sentry" }
caffeine = { module = "com.github.ben-manes.caffeine:caffeine", version.ref = "caffeine" }
scrimage-core = { module = "com.sksamuel.scrimage:scrimage-core", version.ref = "scrimage" }
skrape-html = { module = "it.skrape:skrapeit-html-parser", version.ref = "skrape" }
inmo-telegram-core = { module = "dev.inmo:tgbotapi.core", version.ref = "inmo-telegram" }
inmo-telegram-utils = { module = "dev.inmo:tgbotapi.utils", version.ref = "inmo-telegram" }

spring-ai-bom = { module = "org.springframework.ai:spring-ai-bom", version.ref = "spring-ai" }

Expand Down Expand Up @@ -123,6 +121,7 @@ okhttp = { module = "com.squareup.okhttp3:okhttp", version.ref = "okhttp" }

jaffree = { module = "com.github.kokorin.jaffree:jaffree", version.ref = "jaffree" }

slf4j-api = { module = "org.slf4j:slf4j-api", version.ref = "slf4j" }
logback = { module = "ch.qos.logback:logback-core", version.ref = "logback" }
logback-classic = { module = "ch.qos.logback:logback-classic", version.ref = "logback" }

Expand All @@ -136,6 +135,7 @@ mockk = { module = "io.mockk:mockk", version.ref = "mockk" }
mockk-spring = { module = "com.ninja-squad:springmockk", version.ref = "springmockk" }

protobuf-java = { module = "com.google.protobuf:protobuf-java", version.ref = "protobuf" }
protobuf-java-util = { module = "com.google.protobuf:protobuf-java-util", version.ref = "protobuf" }
protobuf-kotlin = { module = "com.google.protobuf:protobuf-kotlin", version.ref = "protobuf" }
protobuf-protoc = { module = "com.google.protobuf:protoc", version.ref = "protobuf" }

Expand Down Expand Up @@ -173,7 +173,6 @@ spring-webflux = [
"spring-kotlin-reactor-extensions",
"spring-kotlinx-coroutines-reactor",
]
telegram = ["inmo-telegram-core", "inmo-telegram-utils"]
s3 = ["aws-s3", "okhttp"]
imageio = ["imageio-core", "imageio", "imageio-jpeg"]
kotest-spring = ["kotest-junit5", "kotest-assertions-core", "kotest-property", "kotest-spring"]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package pl.kvgx12.wiertarbot.connector

import io.grpc.BindableService
import io.grpc.Server
import io.grpc.ServerBuilder
import org.springframework.beans.factory.BeanRegistrarDsl
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean
import pl.kvgx12.wiertarbot.connector.utils.getLogger

class ConnectorBeanRegistrar : BeanRegistrarDsl({
registerBean<RabbitMQ>()
Expand All @@ -19,9 +21,15 @@ data class ConnectorProperties(val port: Int = 8080)

@EnableConfigurationProperties(ConnectorProperties::class)
class ConnectorConfiguration(private val props: ConnectorProperties) {
private val log = getLogger()

@Bean
fun connectorServer(services: List<BindableService>) =
ServerBuilder.forPort(props.port)
fun connectorServer(services: List<BindableService>): Server {
log.info("Starting Connector gRPC server on port ${props.port}")

return ServerBuilder.forPort(props.port)
.intercept(GrpcExceptionInterceptor())
.addServices(services.map { it.bindService() })
.build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package pl.kvgx12.wiertarbot.connector

import io.grpc.*
import pl.kvgx12.wiertarbot.connector.utils.getLogger

class GrpcExceptionInterceptor : ServerInterceptor {
private val logger = getLogger()

override fun <ReqT : Any, RespT : Any> interceptCall(
call: ServerCall<ReqT, RespT>,
headers: Metadata,
next: ServerCallHandler<ReqT, RespT>
): ServerCall.Listener<ReqT> {
val wrappedCall = object : ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(call) {
override fun close(status: Status, trailers: Metadata) {
if (!status.isOk) {
val cause = status.cause
if (cause != null) {
logger.error("gRPC call failed: ${methodDescriptor.fullMethodName}, status: $status", cause)
} else {
logger.error("gRPC call failed: ${methodDescriptor.fullMethodName}, status: $status")
}
}
super.close(status, trailers)
}
}

val listener = try {
next.startCall(wrappedCall, headers)
} catch (e: Exception) {
logger.error("Exception starting gRPC call: ${call.methodDescriptor.fullMethodName}", e)
call.close(Status.INTERNAL.withDescription("Internal server error").withCause(e), Metadata())
return object : ServerCall.Listener<ReqT>() {}
}

return object : ForwardingServerCallListener.SimpleForwardingServerCallListener<ReqT>(listener) {
override fun onHalfClose() {
try {
super.onHalfClose()
} catch (e: Exception) {
handleException(wrappedCall, e)
}
}

override fun onReady() {
try {
super.onReady()
} catch (e: Exception) {
handleException(wrappedCall, e)
}
}

override fun onMessage(message: ReqT) {
try {
super.onMessage(message)
} catch (e: Exception) {
handleException(wrappedCall, e)
}
}

override fun onCancel() {
try {
super.onCancel()
} catch (e: Exception) {
handleException(wrappedCall, e)
}
}

override fun onComplete() {
try {
super.onComplete()
} catch (e: Exception) {
handleException(wrappedCall, e)
}
}
}
}

private fun <ReqT, RespT> handleException(call: ServerCall<ReqT, RespT>, e: Exception) {
logger.error("Uncaught exception in gRPC listener: ${call.methodDescriptor.fullMethodName}", e)
try {
call.close(Status.INTERNAL.withDescription("Internal server error").withCause(e), Metadata())
} catch (closeException: Exception) {
logger.warn("Failed to close gRPC call after exception", closeException)
}
}
}
16 changes: 16 additions & 0 deletions libs/telegram-kt/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
plugins {
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.kotlin.serialization)
}

dependencies {
implementation(libs.kotlinx.coroutines.core)
implementation(libs.bundles.kotlinx.serialization)

api(libs.bundles.ktor.client)
api(libs.ktor.client.resources)

implementation(libs.slf4j.api)

testImplementation(libs.kotlin.test)
}
Loading
Loading