1
0
Fork 1
mirror of https://gitlab.com/mangadex-pub/mangadex_at_home.git synced 2024-01-19 02:48:37 +00:00

Update settings handling

This commit is contained in:
carbotaniuman 2021-01-25 12:22:07 -06:00
parent f649c97eb1
commit e2130823a0
3 changed files with 35 additions and 50 deletions

View file

@ -22,9 +22,8 @@ import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.module.kotlin.KotlinModule
import mdnet.ServerHandlerJackson.auto
import mdnet.logging.info
import mdnet.settings.DevSettings
import mdnet.settings.ClientSettings
import mdnet.settings.RemoteSettings
import mdnet.settings.ServerSettings
import org.apache.hc.client5.http.impl.DefaultSchemePortResolver
import org.apache.hc.client5.http.impl.classic.HttpClients
import org.apache.hc.client5.http.impl.routing.DefaultRoutePlanner
@ -48,17 +47,14 @@ object ServerHandlerJackson : ConfigurableJackson(
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
)
class BackendApi(
private val serverSettings: ServerSettings,
private val devSettings: DevSettings,
private val maxCacheSizeInMebibytes: Long
) {
class BackendApi(private val settings: ClientSettings) {
private val serverAddress = settings.devSettings.devUrl ?: SERVER_ADDRESS
private val client = ApacheClient(
client = HttpClients.custom()
.setRoutePlanner(
object : DefaultRoutePlanner(DefaultSchemePortResolver()) {
override fun determineLocalAddress(firstHop: HttpHost?, context: HttpContext?): InetAddress {
return InetAddress.getByName(serverSettings.hostname)
return InetAddress.getByName(settings.serverSettings.hostname)
}
}
)
@ -68,17 +64,18 @@ class BackendApi(
fun logoutFromControl(): Boolean {
LOGGER.info { "Disconnecting from the control server" }
val params = mapOf<String, Any>(
"secret" to serverSettings.secret
"secret" to settings.serverSettings.secret
)
val request = STRING_ANY_MAP_LENS(params, Request(Method.POST, getServerAddress() + "stop"))
val request = STRING_ANY_MAP_LENS(params, Request(Method.POST, serverAddress + "stop"))
val response = client(request)
return response.status.successful
}
private fun getPingParams(tlsCreatedAt: String? = null): Map<String, Any> =
mapOf(
private fun getPingParams(tlsCreatedAt: String? = null): Map<String, Any> {
val serverSettings = settings.serverSettings
return mapOf(
"secret" to serverSettings.secret,
"port" to let {
if (serverSettings.externalPort != 0) {
@ -87,7 +84,7 @@ class BackendApi(
serverSettings.port
}
},
"disk_space" to maxCacheSizeInMebibytes * 1024 * 1024,
"disk_space" to settings.maxCacheSizeInMebibytes * 1024 * 1024,
"network_speed" to serverSettings.externalMaxKilobitsPerSecond * 1000 / 8,
"build_version" to Constants.CLIENT_BUILD
).let {
@ -97,11 +94,18 @@ class BackendApi(
it
}
}
}
fun loginToControl(): RemoteSettings? {
LOGGER.info { "Connecting to the control server" }
val request = STRING_ANY_MAP_LENS(getPingParams(), Request(Method.POST, getServerAddress() + "ping"))
val request = STRING_ANY_MAP_LENS(
getPingParams(null),
Request(
Method.POST,
serverAddress + "ping"
)
)
val response = client(request)
return if (response.status.successful) {
@ -118,7 +122,7 @@ class BackendApi(
getPingParams(old.tls!!.createdAt),
Request(
Method.POST,
getServerAddress() + "ping"
serverAddress + "ping"
)
)
val response = client(request)
@ -130,10 +134,6 @@ class BackendApi(
}
}
private fun getServerAddress(): String {
return devSettings.devUrl ?: SERVER_ADDRESS
}
companion object {
private val LOGGER = LoggerFactory.getLogger(BackendApi::class.java)
private val STRING_ANY_MAP_LENS = Body.auto<Map<String, Any>>().toLens()

View file

@ -105,10 +105,7 @@ class MangaDexClient(private val settingsFile: File, databaseFile: File, cacheFo
}
LOGGER.info { "Server manager starting" }
imageServer = ServerManager(
settings.serverSettings,
settings.devSettings,
settings.maxCacheSizeInMebibytes,
settings.metricsSettings,
settings,
storage
).also {
it.start()
@ -165,14 +162,8 @@ class MangaDexClient(private val settingsFile: File, databaseFile: File, cacheFo
storage.maxSize = (newSettings.maxCacheSizeInMebibytes * 1024 * 1024 * 0.95).toLong()
val restartServer = newSettings.serverSettings != settings.serverSettings ||
newSettings.devSettings != settings.devSettings ||
newSettings.metricsSettings != settings.metricsSettings
if (restartServer) {
stopImageServer()
startImageServer()
}
settings = newSettings
} catch (e: UnrecognizedPropertyException) {

View file

@ -27,10 +27,7 @@ import mdnet.logging.info
import mdnet.logging.warn
import mdnet.metrics.DefaultMicrometerMetrics
import mdnet.server.getServer
import mdnet.settings.DevSettings
import mdnet.settings.MetricsSettings
import mdnet.settings.RemoteSettings
import mdnet.settings.ServerSettings
import mdnet.settings.*
import org.http4k.server.Http4kServer
import org.slf4j.LoggerFactory
import java.util.concurrent.CountDownLatch
@ -41,7 +38,7 @@ import java.util.concurrent.atomic.AtomicReference
sealed class State
// server is not running
data class Uninitialized(val serverSettings: ServerSettings, val devSettings: DevSettings) : State()
object Uninitialized : State()
// server has shut down
object Shutdown : State()
@ -50,18 +47,15 @@ object Shutdown : State()
data class GracefulStop(
val lastRunning: Running,
val counts: Int = 0,
val nextState: State = Uninitialized(lastRunning.serverSettings, lastRunning.devSettings),
val nextState: State = Uninitialized,
val action: () -> Unit = {}
) : State()
// server is currently running
data class Running(val server: Http4kServer, val settings: RemoteSettings, val serverSettings: ServerSettings, val devSettings: DevSettings) : State()
data class Running(val server: Http4kServer, val settings: RemoteSettings) : State()
class ServerManager(
serverSettings: ServerSettings,
devSettings: DevSettings,
maxCacheSizeInMebibytes: Long,
private val metricsSettings: MetricsSettings,
private val settings: ClientSettings,
private val storage: ImageStorage
) {
// this must remain single-threaded because of how the state mechanism works
@ -77,8 +71,8 @@ class ServerManager(
// end protected state
init {
state = Uninitialized(serverSettings, devSettings)
backendApi = BackendApi(serverSettings, devSettings, maxCacheSizeInMebibytes)
state = Uninitialized
backendApi = BackendApi(settings)
}
fun start() {
@ -117,7 +111,7 @@ class ServerManager(
val state = this.state
if (state is GracefulStop) {
val timesToWait = state.lastRunning.serverSettings.gracefulShutdownWaitSeconds / 5
val timesToWait = settings.serverSettings.gracefulShutdownWaitSeconds / 5
val requestCounters = registry.find("http.server.request.latency").timers()
println(requestCounters)
val curRequests = requestCounters.map { it.count() }.sum()
@ -164,7 +158,7 @@ class ServerManager(
val state = this.state
if (state is Running) {
val currentBytesSent = statistics.get().bytesSent - lastBytesSent
if (state.serverSettings.maxMebibytesPerHour != 0L && state.serverSettings.maxMebibytesPerHour * 1024 * 1024 /* MiB to bytes */ < currentBytesSent) {
if (settings.serverSettings.maxMebibytesPerHour != 0L && settings.serverSettings.maxMebibytesPerHour * 1024 * 1024 /* MiB to bytes */ < currentBytesSent) {
LOGGER.info { "Stopping image server as hourly bandwidth limit reached" }
this.state = GracefulStop(lastRunning = state)
@ -215,7 +209,7 @@ class ServerManager(
}
private fun loginAndStartServer() {
val state = this.state as Uninitialized
this.state as Uninitialized
val remoteSettings = backendApi.loginToControl()
?: throw RuntimeException("Failed to get a login response from server")
@ -225,13 +219,13 @@ class ServerManager(
val server = getServer(
storage,
remoteSettings,
state.serverSettings,
settings.serverSettings,
statistics,
metricsSettings,
settings.metricsSettings,
registry
).start()
this.state = Running(server, remoteSettings, state.serverSettings, state.devSettings)
this.state = Running(server, remoteSettings)
LOGGER.info { "Internal HTTP server was successfully started" }
}