Skip to content

Commit

Permalink
Fix to embedded mongo. It now properly shuts down the process and del…
Browse files Browse the repository at this point in the history
…etes files.
  • Loading branch information
bjsvedin committed Sep 25, 2023
1 parent 4755cc0 commit d984b05
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ buildscript {
val kotlinVersion:String by extra
val khrysalisVersion: String by extra
repositories {
mavenLocal()
// mavenLocal()
maven(url = "https://s01.oss.sonatype.org/content/repositories/snapshots/")
maven(url = "https://s01.oss.sonatype.org/content/repositories/releases/")
google()
Expand All @@ -20,7 +20,7 @@ buildscript {
allprojects {
group = "com.lightningkite.lightningserver"
repositories {
mavenLocal()
// mavenLocal()
maven(url = "https://s01.oss.sonatype.org/content/repositories/snapshots/")
maven(url = "https://s01.oss.sonatype.org/content/repositories/releases/")
mavenCentral()
Expand Down
2 changes: 1 addition & 1 deletion server-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencies {
implementation("com.lightningkite.khrysalis:jvm-runtime:$khrysalisVersion")

api("io.ktor:ktor-client-content-negotiation:$ktorVersion")
api("io.ktor:ktor-client-cio:$ktorVersion")
api("io.ktor:ktor-client-cio-jvm:2.3.4")

implementation("org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,25 @@ private fun embeddedMongo(deleteAfter: Boolean, replFile: File, port: Int): Mong
}
}

val wrapped = MongoClient.create(
val client = MongoClient.create(
MongoClientSettings.builder()
.applyConnectionString(ConnectionString("mongodb://localhost:$port/?replicaSet=rs0"))
.uuidRepresentation(UuidRepresentation.STANDARD)
.build())
var closeable: Closeable? = null
var fromShutdownHook = false
val shutdownHook = Thread {
fromShutdownHook = true
closeable?.close()
}
val client = wrapped
closeable = client
Runtime.getRuntime().addShutdownHook(shutdownHook)
.apply {

applyConnectionString(ConnectionString("mongodb://localhost:$port/?replicaSet=rs0"))
uuidRepresentation(UuidRepresentation.STANDARD)
}
.build()
)


Runtime.getRuntime().addShutdownHook(Thread {
try {
client.close()
} catch (e: Exception) {
e.printStackTrace()
}
mongodExecutable.stop()
if (deleteAfter) replFile.deleteRecursively()
})
return client
}

0 comments on commit d984b05

Please sign in to comment.