From c16479fc069dc8b0feb20f1561be6ae60dae8baa Mon Sep 17 00:00:00 2001 From: Tommy Schmidt Date: Fri, 21 Jun 2024 11:55:08 +0200 Subject: [PATCH] fix: improve error handling for activity and kill feed --- .../commands/GetServerDetailsCommand.kt | 19 +++++-- .../ServerStatusMonitorService.kt | 57 +++++++++++++++++-- 2 files changed, 65 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/de/darkatra/vrising/discord/commands/GetServerDetailsCommand.kt b/src/main/kotlin/de/darkatra/vrising/discord/commands/GetServerDetailsCommand.kt index 3d11721..13f15c7 100644 --- a/src/main/kotlin/de/darkatra/vrising/discord/commands/GetServerDetailsCommand.kt +++ b/src/main/kotlin/de/darkatra/vrising/discord/commands/GetServerDetailsCommand.kt @@ -149,18 +149,25 @@ class GetServerDetailsCommand( inline = true } + field { + name = "Current Failed Api Attempts" + value = "${serverStatusMonitor.currentFailedApiAttempts}" + inline = true + } + field { name = "Last Update Attempt" value = "" inline = true } - field { - name = "Most recent Errors" - value = when (serverStatusMonitor.recentErrors.isEmpty()) { - true -> "-" - false -> serverStatusMonitor.recentErrors.joinToString("\n") { - "```${StringUtils.truncate(it.message, botProperties.maxCharactersPerError)}```" + if (serverStatusMonitor.recentErrors.isNotEmpty()) { + serverStatusMonitor.recentErrors.chunked(5).forEachIndexed { i, chunk -> + field { + name = "Most recent Errors $i" + value = chunk.joinToString("\n") { + "```${StringUtils.truncate(it.message, botProperties.maxCharactersPerError)}```" + } } } } diff --git a/src/main/kotlin/de/darkatra/vrising/discord/serverstatus/ServerStatusMonitorService.kt b/src/main/kotlin/de/darkatra/vrising/discord/serverstatus/ServerStatusMonitorService.kt index 6dc49af..0f0207c 100644 --- a/src/main/kotlin/de/darkatra/vrising/discord/serverstatus/ServerStatusMonitorService.kt +++ b/src/main/kotlin/de/darkatra/vrising/discord/serverstatus/ServerStatusMonitorService.kt @@ -116,7 +116,7 @@ class ServerStatusMonitorService( ) } } catch (e: Exception) { - logger.error("Could not post status message for monitor '${serverStatusMonitor.id}'.", e) + logger.warn("Could not post status message for monitor '${serverStatusMonitor.id}'.", e) } return } @@ -130,13 +130,12 @@ class ServerStatusMonitorService( ).getOrElse { e -> logger.warn("Could not resolve characters for server monitor '${serverStatusMonitor.id}'. Player Gear level will not be displayed.", e) + serverStatusMonitor.currentFailedApiAttempts += 1 if (botProperties.maxRecentErrors > 0) { serverStatusMonitor.addError(e, botProperties.maxRecentErrors) } - serverStatusMonitor.currentFailedApiAttempts += 1 - try { if (botProperties.maxFailedApiAttempts != 0 && serverStatusMonitor.currentFailedApiAttempts >= botProperties.maxFailedApiAttempts) { @@ -151,7 +150,7 @@ class ServerStatusMonitorService( ) } } catch (e: Exception) { - logger.error("Could not post status message for monitor '${serverStatusMonitor.id}'", e) + logger.warn("Could not post status message for monitor '${serverStatusMonitor.id}'", e) } return } @@ -210,10 +209,34 @@ class ServerStatusMonitorService( serverStatusMonitor.apiPort!!, getInterceptors(serverStatusMonitor) ).getOrElse { e -> - logger.error("Exception updating the player activity feed of ${serverStatusMonitor.id}", e) + + logger.error("Exception updating the player activity feed of '${serverStatusMonitor.id}'", e) + serverStatusMonitor.currentFailedApiAttempts += 1 + + if (botProperties.maxRecentErrors > 0) { + serverStatusMonitor.addError(e, botProperties.maxRecentErrors) + } + + try { + if (botProperties.maxFailedApiAttempts != 0 && serverStatusMonitor.currentFailedApiAttempts >= botProperties.maxFailedApiAttempts) { + logger.warn("Disabling the player activity feed for server monitor '${serverStatusMonitor.id}' because it exceeded the max failed api attempts.") + serverStatusMonitor.playerActivityDiscordChannelId = null + + playerActivityChannel.createMessage( + """Disabled the player activity feed for server status monitor '${serverStatusMonitor.id}' because + |the bot companion did not respond successfully after ${botProperties.maxFailedApiAttempts} attempts. + |Please make sure the server-api-hostname and server-api-port are correct. + |You can re-enable the functionality using the update-server command.""".trimMargin() + ) + } + } catch (e: Exception) { + logger.warn("Could not post status message for monitor '${serverStatusMonitor.id}'", e) + } return } + serverStatusMonitor.currentFailedApiAttempts = 0 + playerActivities .filter { playerActivity -> playerActivity.occurred.isAfter(serverStatusMonitor.lastUpdated) } .sortedWith(Comparator.comparing(PlayerActivity::occurred)) @@ -249,10 +272,34 @@ class ServerStatusMonitorService( serverStatusMonitor.apiPort!!, getInterceptors(serverStatusMonitor) ).getOrElse { e -> + logger.error("Exception updating the pvp kill feed of ${serverStatusMonitor.id}", e) + serverStatusMonitor.currentFailedApiAttempts += 1 + + if (botProperties.maxRecentErrors > 0) { + serverStatusMonitor.addError(e, botProperties.maxRecentErrors) + } + + try { + if (botProperties.maxFailedApiAttempts != 0 && serverStatusMonitor.currentFailedApiAttempts >= botProperties.maxFailedApiAttempts) { + logger.warn("Disabling the pvp kill feed for server monitor '${serverStatusMonitor.id}' because it exceeded the max failed api attempts.") + serverStatusMonitor.pvpKillFeedDiscordChannelId = null + + pvpKillFeedChannel.createMessage( + """Disabled the pvp kill feed for server status monitor '${serverStatusMonitor.id}' because + |the bot companion did not respond successfully after ${botProperties.maxFailedApiAttempts} attempts. + |Please make sure the server-api-hostname and server-api-port are correct. + |You can re-enable the functionality using the update-server command.""".trimMargin() + ) + } + } catch (e: Exception) { + logger.warn("Could not post status message for monitor '${serverStatusMonitor.id}'", e) + } return } + serverStatusMonitor.currentFailedApiAttempts = 0 + pvpKills .filter { pvpKill -> pvpKill.occurred.isAfter(serverStatusMonitor.lastUpdated) } .sortedWith(Comparator.comparing(PvpKill::occurred))