Skip to content

Commit

Permalink
fix: improve error handling for activity and kill feed
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkAtra committed Jun 21, 2024
1 parent ac453ea commit c16479f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<t:${serverStatusMonitor.lastUpdated.epochSecond}:R>"
inline = true
}

field {
name = "Most recent Errors"
value = when (serverStatusMonitor.recentErrors.isEmpty()) {
true -> "-"
false -> serverStatusMonitor.recentErrors.joinToString("\n") {
"<t:${it.timestamp}:R>```${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") {
"<t:${it.timestamp}:R>```${StringUtils.truncate(it.message, botProperties.maxCharactersPerError)}```"
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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) {
Expand All @@ -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
}
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit c16479f

Please sign in to comment.