Skip to content

Commit

Permalink
Update logs
Browse files Browse the repository at this point in the history
  • Loading branch information
schroda committed Jul 13, 2023
1 parent c23ba70 commit 17c2a15
Showing 1 changed file with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ enum class WebUI(var repoUrl: String, var versionMappingUrl: String, var zipFile

const val DEFAULT_WEB_UI = "WebUI"


object WebInterfaceManager {
private val logger = KotlinLogging.logger {}
private const val webUIPreviewVersion = "PREVIEW"
Expand Down Expand Up @@ -103,7 +102,7 @@ object WebInterfaceManager {
if (doesLocalWebUIExist(applicationDirs.webUIRoot)) {
val currentVersion = getLocalVersion(applicationDirs.webUIRoot)

logger.info { "WebUI Static files exists, version= $currentVersion" }
logger.info { "setupWebUI: found webUI files - flavor= ${serverConfig.webUIFlavor}, version= $currentVersion" }

if (!isLocalWebUIValid(applicationDirs.webUIRoot)) {
doInitialSetup()
Expand All @@ -117,11 +116,10 @@ object WebInterfaceManager {
return
}

logger.info { "No WebUI Static files found, starting download..." }
logger.info { "setupWebUI: no webUI files found, starting download..." }
doInitialSetup()
}


/**
* Tries to download the latest compatible version for the selected webUI and falls back to the default webUI in case of errors.
*/
Expand Down Expand Up @@ -150,9 +148,10 @@ object WebInterfaceManager {
}

private fun extractBundledWebUI() {
val resourceWebUI: InputStream = BuildConfig::class.java.getResourceAsStream("/WebUI.zip") ?: throw Error("No bundled webUI version found")
val resourceWebUI: InputStream = BuildConfig::class.java.getResourceAsStream("/WebUI.zip") ?: throw Error("extractBundledWebUI: No bundled webUI version found")

logger.info { "extractBundledWebUI: Using the bundled WebUI zip..." }

logger.info { "Using the bundled WebUI zip..." }
val webUIZip = WebUI.WEBUI.zipFile
val webUIZipPath = "$tmpDir/$webUIZip"
val webUIZipFile = File(webUIZipPath)
Expand All @@ -167,11 +166,14 @@ object WebInterfaceManager {
}

private fun checkForUpdate() {
if (isUpdateAvailable(getLocalVersion(applicationDirs.webUIRoot))) {
logger.info { "An update is available, starting download..." }
val localVersion = getLocalVersion(applicationDirs.webUIRoot)
if (isUpdateAvailable(localVersion)) {
logger.info { "checkForUpdate(${serverConfig.webUIFlavor}, $localVersion): An update is available, starting download..." }
downloadLatestCompatibleVersion()
preferences.putLong(lastWebUIUpdateCheckKey, System.currentTimeMillis())
}

logger.debug { "checkForUpdate(${serverConfig.webUIFlavor}, $localVersion): local version is the latest one" }
}

private fun getDownloadUrlFor(version: String): String {
Expand All @@ -197,14 +199,14 @@ object WebInterfaceManager {
return false
}

logger.info { "Verifying WebUI files..." }
logger.info { "isLocalWebUIValid: Verifying WebUI files..." }

val currentVersion = getLocalVersion(path)
val localMD5Sum = getLocalMD5Sum(path)
val currentVersionMD5Sum = fetchMD5SumFor(currentVersion)
val validationSucceeded = currentVersionMD5Sum == localMD5Sum

logger.info { "Validation ${if (validationSucceeded) "succeeded" else "failed"} - md5: local= $localMD5Sum; expected= $currentVersionMD5Sum" }
logger.info { "isLocalWebUIValid: Validation ${if (validationSucceeded) "succeeded" else "failed"} - md5: local= $localMD5Sum; expected= $currentVersionMD5Sum" }

return validationSucceeded
}
Expand Down Expand Up @@ -242,13 +244,14 @@ object WebInterfaceManager {

private fun getLatestCompatibleVersion(): String {
if (WebUIChannel.doesConfigChannelEqual(WebUIChannel.BUNDLED)) {
logger.debug { "getLatestCompatibleVersion: Channel is \"${WebUIChannel.BUNDLED}\", do not check for update" }
return BuildConfig.WEBUI_TAG
}

val currentServerVersionNumber = extractVersion(BuildConfig.REVISION)
val webUIToServerVersionMappings = JSONArray(URL(WebUI.WEBUI.versionMappingUrl).readText())

logger.debug { "webUIChannel= ${serverConfig.webUIChannel} currentServerVersion= ${BuildConfig.REVISION}, mappingFile= $webUIToServerVersionMappings" }
logger.debug { "getLatestCompatibleVersion: webUIChannel= ${serverConfig.webUIChannel}, currentServerVersion= ${BuildConfig.REVISION}, mappingFile= $webUIToServerVersionMappings" }

for (i in 0 until webUIToServerVersionMappings.length()) {
val webUIToServerVersionEntry = webUIToServerVersionMappings.getJSONObject(i)
Expand Down Expand Up @@ -281,7 +284,7 @@ object WebInterfaceManager {
val webUIZipPath = "$tmpDir/$webUIZip"
val webUIZipFile = File(webUIZipPath)

logger.info { "Downloading WebUI (version \"$latestCompatibleVersion\") zip from the Internet..." }
logger.info { "downloadLatestCompatibleVersion: Downloading WebUI (flavor= ${serverConfig.webUIFlavor}, version \"$latestCompatibleVersion\") zip from the Internet..." }

try {
val webUIZipURL = "${getDownloadUrlFor(latestCompatibleVersion)}/$webUIZip"
Expand All @@ -292,7 +295,7 @@ object WebInterfaceManager {
}
} catch (e: Exception) {
val retry = retryCount < 3
logger.error { "Download failed${if (retry) ", retrying ${retryCount + 1}/3" else ""} - error: $e" }
logger.error { "downloadLatestCompatibleVersion: Download failed${if (retry) ", retrying ${retryCount + 1}/3" else ""} - error: $e" }

if (retry) {
return downloadLatestCompatibleVersion(retryCount + 1)
Expand All @@ -304,9 +307,9 @@ object WebInterfaceManager {
File(applicationDirs.webUIRoot).deleteRecursively()

// extract webUI zip
logger.info { "Extracting WebUI zip..." }
logger.info { "downloadLatestCompatibleVersion: Extracting WebUI zip..." }
extractDownload(webUIZipPath, applicationDirs.webUIRoot)
logger.info { "Extracting WebUI zip Done." }
logger.info { "downloadLatestCompatibleVersion: Extracting WebUI zip Done." }

return true
}
Expand All @@ -324,7 +327,7 @@ object WebInterfaceManager {
connection.inputStream.buffered().use { inp ->
var totalCount = 0

print("Download progress: % 00")
print("downloadVersion: Download progress: % 00")
while (true) {
val count = inp.read(data, 0, 1024)

Expand All @@ -340,7 +343,7 @@ object WebInterfaceManager {
webUIZipFileOut.write(data, 0, count)
}
println()
logger.info { "Downloading WebUI Done." }
logger.info { "downloadVersion: Downloading WebUI Done." }
}
}
}
Expand All @@ -367,6 +370,7 @@ object WebInterfaceManager {
val latestCompatibleVersion = getLatestCompatibleVersion()
latestCompatibleVersion != currentVersion
} catch (e: Exception) {
logger.debug { "isUpdateAvailable: check failed due to $e" }
false
}
}
Expand Down

0 comments on commit 17c2a15

Please sign in to comment.