Skip to content

Commit

Permalink
fix: corrected automated offboarding log message
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkAtra committed Nov 17, 2024
1 parent bae0f65 commit 5ba71f9
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 6 deletions.
4 changes: 1 addition & 3 deletions src/main/kotlin/de/darkatra/vrising/discord/Bot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ class Bot(
CronTask(
{
if (isReady.get() && kord.isActive) {
runBlocking {
serverService.cleanupInactiveServers(kord)
}
serverService.cleanupInactiveServers()
}
},
CronTrigger("0 0 0 * * *", ZoneOffset.UTC)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ServerService(
}
}

suspend fun cleanupInactiveServers(kord: Kord) {
fun cleanupInactiveServers() {

val inactiveServers = serverRepository.getServers().filterInactive()
if (inactiveServers.isEmpty()) {
Expand All @@ -53,13 +53,14 @@ class ServerService(
}

val sevenDaysAgo = Instant.now().minus(7, ChronoUnit.DAYS)
inactiveServers.forEach { server ->
val serverIds = inactiveServers.map { server ->
if (server.lastUpdated.isBefore(sevenDaysAgo)) {
serverRepository.removeServer(server.id)
}
server.id
}

logger.info("Successfully removed ${inactiveServers.count()} servers with no active feature.")
logger.info("Successfully removed ${serverIds.count()} servers with no active feature: $serverIds")
}

private suspend fun updateStatusMonitor(kord: Kord, server: Server) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package de.darkatra.vrising.discord.serverstatus

import de.darkatra.vrising.discord.persistence.ServerRepository
import de.darkatra.vrising.discord.persistence.model.Server
import de.darkatra.vrising.discord.persistence.model.ServerTestUtils.API_HOST_NAME
import de.darkatra.vrising.discord.persistence.model.ServerTestUtils.API_PASSWORD
import de.darkatra.vrising.discord.persistence.model.ServerTestUtils.API_PORT
import de.darkatra.vrising.discord.persistence.model.ServerTestUtils.API_USERNAME
import de.darkatra.vrising.discord.persistence.model.ServerTestUtils.DISCORD_SERVER_ID
import de.darkatra.vrising.discord.persistence.model.ServerTestUtils.HOST_NAME
import de.darkatra.vrising.discord.persistence.model.ServerTestUtils.QUERY_PORT
import de.darkatra.vrising.discord.persistence.model.Version
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.condition.DisabledInNativeImage
import org.mockito.Mockito
import org.mockito.kotlin.given
import org.mockito.kotlin.never
import org.mockito.kotlin.verify
import java.time.Instant
import java.time.temporal.ChronoUnit

@DisabledInNativeImage
class ServerServiceTest {

private val serverRepository = Mockito.mock(ServerRepository::class.java)
private val statusMonitorService = Mockito.mock(StatusMonitorService::class.java)
private val playerActivityFeedService = Mockito.mock(PlayerActivityFeedService::class.java)
private val pvpKillFeedService = Mockito.mock(PvpKillFeedService::class.java)

private val serverService = ServerService(serverRepository, statusMonitorService, playerActivityFeedService, pvpKillFeedService)

@Test
fun shouldOffboardInactiveServers() {

val servers = listOf(
// should not be offboarded
Server(
id = "do-not-offboard",
version = Version(
revision = 1,
updated = Instant.now()
),
discordServerId = DISCORD_SERVER_ID,
hostname = HOST_NAME,
queryPort = QUERY_PORT,
apiHostname = API_HOST_NAME,
apiPort = API_PORT,
apiUsername = API_USERNAME,
apiPassword = API_PASSWORD
),
// should be offboarded
Server(
id = "offboard-me-pls",
version = Version(
revision = 1,
updated = Instant.now().minus(8, ChronoUnit.DAYS)
),
discordServerId = DISCORD_SERVER_ID,
hostname = HOST_NAME,
queryPort = QUERY_PORT,
apiHostname = API_HOST_NAME,
apiPort = API_PORT,
apiUsername = API_USERNAME,
apiPassword = API_PASSWORD
)
)
given(serverRepository.getServers()).willReturn(servers)

serverService.cleanupInactiveServers()

verify(serverRepository).removeServer("offboard-me-pls")
verify(serverRepository, never()).removeServer("do-not-offboard")
}
}

0 comments on commit 5ba71f9

Please sign in to comment.