Skip to content

Commit

Permalink
feat: impl http method for healthcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
manhinhang committed Apr 20, 2024
1 parent 946d870 commit 366cc4e
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 24 deletions.
64 changes: 64 additions & 0 deletions healthcheck/healthcheck-rest/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Kotlin application project to get you started.
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.7/userguide/building_java_projects.html in the Gradle documentation.
* This project uses @Incubating APIs which are subject to change.
*/
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
// Apply the org.jetbrains.kotlin.jvm Plugin to add support for Kotlin.
alias(libs.plugins.jvm)
id("org.springframework.boot") version "3.2.2"
id("io.spring.dependency-management") version "1.1.4"
kotlin("plugin.spring") version "1.9.22"
kotlin("plugin.jpa") version "1.9.22"
// Apply the application plugin to add support for building a CLI application in Java.
application
}

repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}

dependencies {
// This dependency is used by the application.
implementation(libs.guava)
implementation(platform("org.springframework.boot:spring-boot-dependencies:3.2.2"))
implementation("org.springframework.boot:spring-boot-starter")
implementation("org.springframework.boot:spring-boot-starter-web")
// implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1-Beta")
implementation(project(":healthcheck"))
}

tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "17"
}
}

testing {
suites {
// Configure the built-in test suite
val test by getting(JvmTestSuite::class) {
// Use Kotlin Test test framework
useKotlinTest("1.9.22")
}
}
}

// Apply a specific Java toolchain to ease working on different environments.
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}

application {
// Define the main class for the application.
mainClass = "com.manhinhang.ibgatewaydocker.healthcheck.rest.MainKt"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.manhinhang.ibgatewaydocker.healthcheck.rest
import com.manhinhang.ibgatewaydocker.healthcheck.IBGatewayClient
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import kotlinx.coroutines.*

@RestController
@RequestMapping("/")
class HealthcheckApiController() {

val ibClient = IBGatewayClient()

@GetMapping("/ready")
fun ready(): ResponseEntity<Any> {
return ResponseEntity.status(HttpStatus.OK).body("OK");
}

@GetMapping("/healthcheck")
fun healthcheck(): ResponseEntity<Any> {
val result = runBlocking { ibClient.ping() }
if (result.isSuccess) {
return ResponseEntity.status(HttpStatus.OK).body("OK")
}
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Fail")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.manhinhang.ibgatewaydocker.healthcheck.rest

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplication
class RestApplication

fun main(args: Array<String>) {
runApplication<RestApplication>(*args)
}
1 change: 1 addition & 0 deletions healthcheck/healthcheck/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ repositories {
dependencies {
// This dependency is used by the application.
implementation(libs.guava)
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1-Beta")
}

testing {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* This source file was generated by the Gradle 'init' task
*/
package com.manhinhang.ibgatewaydocker.healthcheck
import kotlinx.coroutines.*

fun main() = runBlocking {
val client = IBGatewayClient()
val result = client.ping()
result.exceptionOrNull()?.let { throw it }
return@runBlocking
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.manhinhang.ibgatewaydocker.healthcheck

import com.ib.client.EClientSocket
import com.ib.client.EJavaSignal
import kotlinx.coroutines.*

class IBGatewayClient {
val client: EClientSocket
companion object {
val clientId = (System.getenv("HEALTHCHECK_CLIENT_ID")?.toIntOrNull() ?: 999)
val port = (System.getenv("IB_GATEWAY_INTERNAL_PORT")?.toIntOrNull() ?: 4001)
val host = "localhost"
}

init {
client = createIBClient()
}

private fun createIBClient(): EClientSocket {
val signal = EJavaSignal();
val client = EClientSocket(Wrapper(), signal)
return client
}

suspend fun connect():Boolean = withContext(Dispatchers.IO) {
if (!client.isConnected) {
val client = createIBClient()
client.eConnect(host, port, clientId)
}
client.isConnected
}

fun disconnect() {
client.eDisconnect()
}

suspend fun ping():Result<Any> = coroutineScope {
runCatching {
val isConnected = connect()
if (isConnected) {
println("Ping IB Gateway successful")
client.eDisconnect()
}else {
throw InterruptedException("Can not connect to IB Gateway")
}
}
}

}

This file was deleted.

1 change: 1 addition & 0 deletions healthcheck/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ plugins {

rootProject.name = "healthcheck"
include("healthcheck")
include("healthcheck-rest")

0 comments on commit 366cc4e

Please sign in to comment.