Skip to content

Commit

Permalink
fix: retrying Solr cores creation on failure
Browse files Browse the repository at this point in the history
  • Loading branch information
jachro committed Feb 9, 2024
1 parent 6ef2c77 commit a052341
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions project/SolrServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package io.renku.servers

import java.util.concurrent.atomic.AtomicBoolean
import scala.annotation.tailrec
import scala.sys.process.*
import scala.util.Try

Expand Down Expand Up @@ -94,10 +95,28 @@ class SolrServer(module: String, port: Int) {
}
Try(startCmd.!!).fold(retryOnContainerFailedToRun, _ => wasStartedHere.set(true))
Thread.sleep(500)
createCores(cores)
}

@tailrec
private def createCores(cores: Set[String], attempt: Int = 1): Unit = {
if (attempt > 10)
sys.error(
s"Solr core(s) ${cores.map(c => s"'$c'").mkString(", ")} couldn't be created"
)

val rcs = cores.map(c => c -> createCoreCmd(c).!)
println(
s"Created solr cores: ${rcs.map { case (core, rc) => s"'$core' ($rc)" }.mkString(", ")}"
)

val toRetry = rcs.foldLeft(Set.empty[String]) {
case (toRetry, (c, 0)) =>
println(s"Solr '$c' core created")
toRetry
case (toRetry, (c, rc)) =>
println(s"Solr '$c' core creation failed $rc")
toRetry + c
}

if (toRetry.nonEmpty) createCores(toRetry)
}

def stop(): Unit =
Expand Down

0 comments on commit a052341

Please sign in to comment.