Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: run HealthCheck fail #87

Merged
merged 9 commits into from
Apr 20, 2024
Merged
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
8 changes: 6 additions & 2 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ jobs:
pip install pytest
if [ -f requirements-test.txt ]; then pip install -r requirements-test.txt; fi
- name: Build Docker image
run: docker build -t $IMAGE_NAME .
uses: docker/build-push-action@v5
with:
context: .
push: false
tags: ${{ env.IMAGE_NAME }}
- name: Smoke tests container image
run: pytest -x
env:
Expand All @@ -60,7 +64,7 @@ jobs:
python examples/ib_insync/scripts/connect_gateway.py;
docker stop $(docker ps -a -q)
env:
TRADING_MODE: paper
IB_ACCOUNT: ${{ secrets.IB_ACCOUNT }}
IB_PASSWORD: ${{ secrets.IB_PASSWORD }}
TRADING_MODE: paper

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.secrets
.env
.vscode
healthcheck/healthcheck/src/main/java
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ COPY ibc/config.ini ${IBC_INI}

# install healthcheck tool
ADD healthcheck/healthcheck/build/distributions/healthcheck.tar /
ENV PATH="${PATH}:/healthcheck/healthcheck/bin"
ENV PATH="${PATH}:/healthcheck/bin"

# copy cmd script
WORKDIR /root
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.template
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ COPY ibc/config.ini ${IBC_INI}

# install healthcheck tool
ADD healthcheck/healthcheck/build/distributions/healthcheck.tar /
ENV PATH="${PATH}:/healthcheck/healthcheck/bin"
ENV PATH="${PATH}:/healthcheck/bin"

# copy cmd script
WORKDIR /root
Expand Down
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,45 @@ ib-gateway-docker
| - | - | - |
| ib_insync | [examples/ib_insync](./examples/ib_insync) | This example demonstrated how to connect `IB Gateway`


## Health check container

Execute `healthcheck` to detect IB gateway haelth status

```bash
healthcheck
# output: Ping IB Gateway successful
echo $?
# output: 0
```

```bash
healthcheck
# output: Can not connect to IB Gateway
echo $?
# output: 1
```

- Docker compose example

```yaml
services:
ib-gateway:
image: manhinhang/ib-gateway-docker
ports:
- 4002:4002
environment:
- IB_ACCOUNT=$IB_ACCOUNT
- IB_PASSWORD=$IB_PASSWORD
- TRADING_MODE=$TRADING_MODE
healthcheck:
test: /healthcheck/bin/healthcheck
interval: 60s
timeout: 30s
retries: 3
start_period: 60s
```

# Tests

The [test cases](test/test_ib_gateway.py) written with testinfra.
Expand Down
17 changes: 9 additions & 8 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
services:
ib-gateway:
image: manhinhang/ib-gateway-docker
build:
context: .
ports:
- 4002:4002
environment:
- IB_ACCOUNT=$IB_ACCOUNT
- IB_PASSWORD=$IB_PASSWORD
- TRADING_MODE=$TRADING_MODE
secrets:
IB_ACCOUNT:
environment: "IB_ACCOUNT"
IB_PASSWORD:
environment: "IB_PWD"
TRADING_MODE:
environment: "TRADING_MODE"
healthcheck:
test: /healthcheck/bin/healthcheck
interval: 60s
timeout: 30s
retries: 3
start_period: 60s

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)
}
26 changes: 25 additions & 1 deletion healthcheck/healthcheck/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.gradle.wrapper.Download

/*
* This file was generated by the Gradle 'init' task.
*
Expand All @@ -9,7 +11,7 @@
plugins {
// Apply the org.jetbrains.kotlin.jvm Plugin to add support for Kotlin.
alias(libs.plugins.jvm)

id("de.undercouch.download") version "5.6.0"
// Apply the application plugin to add support for building a CLI application in Java.
application
}
Expand All @@ -22,7 +24,29 @@ repositories {
dependencies {
// This dependency is used by the application.
implementation(libs.guava)
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1-Beta")
}

val downloadIbApiTask = tasks.register<org.jetbrains.kotlin.de.undercouch.gradle.tasks.download.Download>("downloadIbApi") {
onlyIf("ibapi.zip not found") {
!File("${layout.buildDirectory.asFile.get().path}/ibapi.zip").exists()
}
src("https://interactivebrokers.github.io/downloads/twsapi_macunix.1019.04.zip")
dest("${layout.buildDirectory.asFile.get().path}/ibapi.zip")
}

val unzipIbApiTask = tasks.register<Copy>("unzipIbApi") {
from(zipTree("${layout.buildDirectory.asFile.get().path}/ibapi.zip")) {
include("IBJts/source/JavaClient/com/**")
eachFile {
relativePath = RelativePath(true, *relativePath.segments.drop(3).toTypedArray())
}
includeEmptyDirs = false
}
into("${projectDir}/src/main/java")
}
unzipIbApiTask { dependsOn(downloadIbApiTask) }
tasks.compileKotlin { dependsOn(unzipIbApiTask) }

testing {
suites {
Expand Down
60 changes: 0 additions & 60 deletions healthcheck/healthcheck/src/main/java/com/ib/client/Bar.java

This file was deleted.

42 changes: 0 additions & 42 deletions healthcheck/healthcheck/src/main/java/com/ib/client/BitMask.java

This file was deleted.

Loading