/
AppServer.scala
215 lines (198 loc) · 7.62 KB
/
AppServer.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* Copyright © 2021 - 2022 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors.
* SPDX-License-Identifier: Apache-2.0
*/
package org.knora.webapi.core
import zio._
import org.knora.webapi.config.AppConfig
import org.knora.webapi.core.domain.AppState
import org.knora.webapi.messages.store.cacheservicemessages.CacheServiceStatusNOK
import org.knora.webapi.messages.store.cacheservicemessages.CacheServiceStatusOK
import org.knora.webapi.messages.store.sipimessages.IIIFServiceStatusNOK
import org.knora.webapi.messages.store.sipimessages.IIIFServiceStatusOK
import org.knora.webapi.store.cache.api.CacheService
import org.knora.webapi.store.iiif.api.IIIFService
import org.knora.webapi.store.triplestore.api.TriplestoreService
import org.knora.webapi.store.triplestore.domain.TriplestoreStatus
import org.knora.webapi.store.triplestore.upgrade.RepositoryUpdater
import org.knora.webapi.util.cache.CacheUtil
/**
* The application bootstrapper
*/
final case class AppServer(
state: State,
ts: TriplestoreService,
ru: RepositoryUpdater,
as: ActorSystem,
ar: AppRouter,
iiifs: IIIFService,
cs: CacheService,
hs: HttpServer,
appConfig: AppConfig
) {
/**
* Checks if the TriplestoreService is running and the repository is properly initialized.
*/
private val checkTriplestoreService: UIO[Unit] =
for {
_ <- state.set(AppState.WaitingForTriplestore)
status <- ts.checkTriplestore().map(_.triplestoreStatus)
_ <- status match {
case TriplestoreStatus.Available(_) => ZIO.unit
case TriplestoreStatus.NotInitialized(msg) => ZIO.die(new Exception(msg))
case TriplestoreStatus.Unavailable(msg) => ZIO.die(new Exception(msg))
}
_ <- state.set(AppState.TriplestoreReady)
} yield ()
/**
* Initiates repository upgrade if `requiresRepository` is `true` an logs the result.
*
* @param requiresRepository If `true`, calls the RepositoryUpdater to initiate the repository, otherwise returns ()
*/
private def upgradeRepository(requiresRepository: Boolean): UIO[Unit] =
for {
_ <- state.set(AppState.UpdatingRepository)
_ <- ru.maybeUpgradeRepository.flatMap(response => ZIO.logInfo(response.message)).when(requiresRepository)
_ <- state.set(AppState.RepositoryUpToDate)
} yield ()
/**
* Initiates building of all caches
*/
private val buildAllCaches: UIO[Unit] =
for {
_ <- state.set(AppState.CreatingCaches)
_ <- ZIO.attempt {
CacheUtil.removeAllCaches()
CacheUtil.createCaches(appConfig.cacheConfigs)
}.orDie
_ <- state.set(AppState.CachesReady)
} yield ()
/**
* Initiates population of the ontology caches if `requiresRepository` is `true`
*
* @param requiresRepository If `true`, calls the AppRouter to populate the ontology caches, otherwise returns ()
*/
private def populateOntologyCaches(requiresRepository: Boolean): UIO[Unit] =
for {
_ <- state.set(AppState.LoadingOntologies)
_ <- ar.populateOntologyCaches.when(requiresRepository)
_ <- 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)
_ <- iiifs
.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)
_ <- state.set(AppState.IIIFServiceReady)
} yield ()
/**
* Checks if the Cache service is running
*/
private val checkCacheService: UIO[Unit] =
for {
_ <- state.set(AppState.WaitingForCacheService)
_ <- cs.getStatus.flatMap {
case CacheServiceStatusNOK =>
ZIO.logError("Cache service not running.") *> ZIO.die(new Exception("Cache service not running."))
case CacheServiceStatusOK =>
ZIO.unit
}
_ <- state.set(AppState.CacheServiceReady)
} yield ()
/**
* Initiates the startup of the DSP-API server.
*
* @param requiresAdditionalRepositoryChecks If `true`, checks if repository service is running, updates data if necessary and loads ontology cache.
* If `false`, checks if repository service is running but doesn't run upgrades and doesn't load ontology cache.
* @param requiresIIIFService If `true`, ensures that the IIIF service is running.
*/
def start(
requiresAdditionalRepositoryChecks: Boolean,
requiresIIIFService: Boolean
): UIO[Unit] =
for {
_ <- ZIO.logInfo("=> Startup checks initiated")
_ <- checkTriplestoreService
_ <- upgradeRepository(requiresAdditionalRepositoryChecks)
_ <- buildAllCaches
_ <- populateOntologyCaches(requiresAdditionalRepositoryChecks)
_ <- checkIIIFService(requiresIIIFService)
_ <- checkCacheService
_ <- 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)
_ <- state.set(AppState.Running)
} yield ()
}
object AppServer {
private type AppServerEnvironment =
State
with TriplestoreService
with RepositoryUpdater
with ActorSystem
with AppRouter
with IIIFService
with CacheService
with HttpServer
with AppConfig
/**
* Initializes the AppServer instance with the required services
*/
def init(): ZIO[AppServerEnvironment, Nothing, AppServer] =
for {
state <- ZIO.service[State]
ts <- ZIO.service[TriplestoreService]
ru <- ZIO.service[RepositoryUpdater]
as <- ZIO.service[ActorSystem]
ar <- ZIO.service[AppRouter]
iiifs <- ZIO.service[IIIFService]
cs <- ZIO.service[CacheService]
hs <- ZIO.service[HttpServer]
c <- ZIO.service[AppConfig]
appServer = AppServer(state, ts, ru, as, ar, iiifs, cs, hs, c)
} yield appServer
/**
* The AppServer live layer
*/
val live: ZLayer[AppServerEnvironment, Nothing, Unit] =
ZLayer {
for {
appServer <- AppServer.init()
_ <- appServer.start(requiresAdditionalRepositoryChecks = true, requiresIIIFService = true)
} yield ()
}
/**
* The AppServer test layer with Sipi, which initiates the startup checks. Before this layer does what it does,
* the complete server should have already been started.
*/
val testWithSipi: ZLayer[AppServerEnvironment, Nothing, Unit] =
ZLayer {
for {
appServer <- AppServer.init()
_ <- appServer.start(requiresAdditionalRepositoryChecks = false, requiresIIIFService = true)
} yield ()
}
/**
* The AppServer test layer without Sipi, which initiates the startup checks. Before this layer does what it does,
* the complete server should have already been started.
*/
val testWithoutSipi: ZLayer[AppServerEnvironment, Nothing, Unit] =
ZLayer {
for {
appServer <- AppServer.init()
_ <- appServer.start(requiresAdditionalRepositoryChecks = false, requiresIIIFService = false)
} yield ()
}
}