Skip to content

Commit

Permalink
chore: Do not fail if IIIF server is not available at startup
Browse files Browse the repository at this point in the history
  • Loading branch information
seakayone committed Apr 25, 2024
1 parent d188459 commit e611b1b
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 65 deletions.
8 changes: 1 addition & 7 deletions integration/src/test/scala/org/knora/webapi/CoreSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,8 @@ abstract class CoreSpec
/* Here we start our app and initialize the repository before each suit runs */
Unsafe.unsafe { implicit u =>
runtime.unsafe
.run(
(for {
_ <- AppServer.testWithoutSipi
_ <- prepareRepository(rdfDataObjects) @@ LogAspect.logSpan("prepare-repo")
} yield ()),
)
.run(AppServer.test *> prepareRepository(rdfDataObjects) @@ LogAspect.logSpan("prepare-repo"))
.getOrThrow()

}

final override def afterAll(): Unit =
Expand Down
11 changes: 4 additions & 7 deletions integration/src/test/scala/org/knora/webapi/E2ESpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ package org.knora.webapi

import com.typesafe.scalalogging._
import org.apache.pekko
import org.apache.pekko.http.scaladsl.client.RequestBuilding
import org.apache.pekko.http.scaladsl.model._
import org.apache.pekko.testkit.TestKitBase
import org.scalatest.BeforeAndAfterAll
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.matchers.should.Matchers
Expand Down Expand Up @@ -38,10 +41,6 @@ import org.knora.webapi.testservices.TestClientService
import org.knora.webapi.util.FileUtil
import org.knora.webapi.util.LogAspect

import pekko.http.scaladsl.client.RequestBuilding
import pekko.http.scaladsl.model._
import pekko.testkit.TestKitBase

/**
* This class can be used in End-to-End testing. It starts the DSP stack
* and provides access to settings and logging.
Expand Down Expand Up @@ -110,9 +109,7 @@ abstract class E2ESpec

final override def beforeAll(): Unit =
/* Here we start our app and initialize the repository before each suit runs */
UnsafeZioRun.runOrThrow(
AppServer.testWithoutSipi *> (prepareRepository(rdfDataObjects) @@ LogAspect.logSpan("prepare-repo")),
)
UnsafeZioRun.runOrThrow(AppServer.test *> (prepareRepository(rdfDataObjects) @@ LogAspect.logSpan("prepare-repo")))

final override def afterAll(): Unit =
/* Stop ZIO runtime and release resources (e.g., running docker containers) */
Expand Down
2 changes: 1 addition & 1 deletion integration/src/test/scala/org/knora/webapi/E2EZSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ abstract class E2EZSpec extends ZIOSpecDefault with TestStartupUtils {

private def prepare: ZIO[AppServer.AppServerEnvironment, Throwable, AppServer] = for {
appServer <- AppServer.init()
_ <- appServer.start(requiresAdditionalRepositoryChecks = false, requiresIIIFService = false).orDie
_ <- appServer.start(requiresAdditionalRepositoryChecks = false).orDie
_ <- prepareRepository(rdfDataObjects)
} yield appServer

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ abstract class ITKnoraLiveSpec
runtime.unsafe
.run(
for {
_ <- AppServer.testWithSipi
_ <- AppServer.test
_ <- prepareRepository(rdfDataObjects) @@ LogAspect.logSpan("prepare-repo")
} yield (),
)
Expand Down
7 changes: 1 addition & 6 deletions integration/src/test/scala/org/knora/webapi/R2RSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,7 @@ abstract class R2RSpec
/* Here we start our app and initialize the repository before each suit runs */
Unsafe.unsafe { implicit u =>
runtime.unsafe
.run(
for {
_ <- AppServer.testWithoutSipi
_ <- prepareRepository(rdfDataObjects) @@ LogAspect.logSpan("prepare-repo")
} yield (),
)
.run(AppServer.test *> prepareRepository(rdfDataObjects) @@ LogAspect.logSpan("prepare-repo"))
.getOrThrow()
}

Expand Down
43 changes: 4 additions & 39 deletions webapi/src/main/scala/org/knora/webapi/core/AppServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import zio._

import org.knora.webapi.config.AppConfig
import org.knora.webapi.core.domain.AppState
import org.knora.webapi.messages.store.sipimessages.IIIFServiceStatusNOK
import org.knora.webapi.messages.store.sipimessages.IIIFServiceStatusOK
import org.knora.webapi.messages.util.KnoraSystemInstances
import org.knora.webapi.slice.ontology.repo.service.OntologyCache
import org.knora.webapi.store.iiif.api.SipiService
Expand Down Expand Up @@ -74,27 +72,6 @@ final case class AppServer(
_ <- state.set(AppState.OntologiesReady)
} yield ()

/**
* Checks if the IIIF service is running
*
* @param requiresIIIFService If `true`, checks the status of the IIIFService instance, otherwise returns ()
*/
private def checkIIIFService(requiresIIIFService: Boolean): UIO[Unit] =
for {
_ <- state.set(AppState.WaitingForIIIFService)
_ <- sipiService
.getStatus()
.flatMap {
case IIIFServiceStatusOK =>
ZIO.logInfo("IIIF service running")
case IIIFServiceStatusNOK =>
ZIO.logError("IIIF service not running") *> ZIO.die(new Exception("IIIF service not running"))
}
.when(requiresIIIFService)
.orDie
_ <- state.set(AppState.IIIFServiceReady)
} yield ()

/**
* Initiates the startup of the DSP-API server.
*
Expand All @@ -104,14 +81,12 @@ final case class AppServer(
*/
def start(
requiresAdditionalRepositoryChecks: Boolean,
requiresIIIFService: Boolean,
): Task[Unit] =
for {
_ <- ZIO.logInfo("=> Startup checks initiated")
_ <- checkTriplestoreService
_ <- upgradeRepository(requiresAdditionalRepositoryChecks)
_ <- populateOntologyCaches(requiresAdditionalRepositoryChecks)
_ <- checkIIIFService(requiresIIIFService)
_ <- ZIO.logInfo("=> Startup checks finished")
_ <- ZIO.logInfo(s"DSP-API Server started: ${appConfig.knoraApi.internalKnoraApiBaseUrl}")
_ <- ZIO.logWarning("Resetting DB over HTTP is turned ON").when(appConfig.allowReloadOverHttp)
Expand Down Expand Up @@ -146,26 +121,16 @@ object AppServer {
val make: URIO[AppServerEnvironment, Unit] =
for {
appServer <- AppServer.init()
_ <- appServer.start(requiresAdditionalRepositoryChecks = true, requiresIIIFService = true).orDie
} yield ()

/**
* The test AppServer with Sipi, which initiates the startup checks. Before this effect does what it does,
* the complete server should have already been started.
*/
val testWithSipi: ZIO[AppServerEnvironment, Nothing, Unit] =
for {
appServer <- AppServer.init()
_ <- appServer.start(requiresAdditionalRepositoryChecks = false, requiresIIIFService = true).orDie
_ <- appServer.start(requiresAdditionalRepositoryChecks = true).orDie
} yield ()

/**
* The test AppServer without Sipi, which initiates the startup checks. Before this effect does what it does,
* The test AppServer with or without Sipi, which initiates the startup checks. Before this effect does what it does,
* the complete server should have already been started.
*/
val testWithoutSipi: ZIO[AppServerEnvironment, Nothing, Unit] =
val test: ZIO[AppServerEnvironment, Nothing, Unit] =
for {
appServer <- AppServer.init()
_ <- appServer.start(requiresAdditionalRepositoryChecks = false, requiresIIIFService = false).orDie
_ <- appServer.start(requiresAdditionalRepositoryChecks = false).orDie
} yield ()
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ object AppState {
case object RepositoryUpToDate extends AppState
case object LoadingOntologies extends AppState
case object OntologiesReady extends AppState
case object WaitingForIIIFService extends AppState
case object IIIFServiceReady extends AppState
case object MaintenanceMode extends AppState
case object Running extends AppState
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ object HealthResponse {
case AppState.RepositoryUpToDate => unhealthy("Repository up to date. Please retry later.")
case AppState.LoadingOntologies => unhealthy("Loading ontologies. Please retry later.")
case AppState.OntologiesReady => unhealthy("Ontologies ready. Please retry later.")
case AppState.WaitingForIIIFService => unhealthy("Waiting for IIIF service. Please retry later.")
case AppState.IIIFServiceReady => unhealthy("IIIF service ready. Please retry later.")
case AppState.MaintenanceMode => unhealthy("Application is in maintenance mode. Please retry later.")
case AppState.Running => healthy
}
Expand Down

0 comments on commit e611b1b

Please sign in to comment.