Skip to content

Commit

Permalink
Wait 2 minutes before forcefully removing inactive instances
Browse files Browse the repository at this point in the history
  • Loading branch information
FluxCapacitor2 committed Dec 25, 2024
1 parent fb48500 commit bb4f94a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
25 changes: 22 additions & 3 deletions common/src/main/kotlin/com/bluedragonmc/server/Game.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import net.minestom.server.event.server.ServerTickMonitorEvent
import net.minestom.server.event.trait.InstanceEvent
import net.minestom.server.event.trait.PlayerEvent
import net.minestom.server.instance.Instance
import net.minestom.server.tag.Tag
import net.minestom.server.timer.ExecutionType
import net.minestom.server.utils.async.AsyncUtils
import org.slf4j.Logger
Expand Down Expand Up @@ -378,6 +379,16 @@ abstract class Game(val name: String, val mapName: String, val mode: String? = n
*/
private val INSTANCE_CLEANUP_PERIOD = System.getenv("SERVER_INSTANCE_CLEANUP_PERIOD")?.toLongOrNull() ?: 10_000L

/**
* Instances must be inactive for at least 2 minutes
* to be cleaned up (by default).
*/
private val CLEANUP_MIN_INACTIVE_TIME =
System.getenv("SERVER_INSTANCE_MIN_INACTIVE_TIME")?.toLongOrNull() ?: 120_000L

private val INACTIVE_SINCE_TAG = Tag.Long("instance_inactive_since")


fun findGame(player: Player): Game? =
games.find { player in it.players || it.ownsInstance(player.instance ?: return@find false) }

Expand Down Expand Up @@ -407,10 +418,18 @@ abstract class Game(val name: String, val mapName: String, val mode: String? = n

instances.forEach { instance ->
val owner = games.find { it.ownsInstance(instance) }
if (owner == null && games.none { it.getRequiredInstances().contains(instance) }) {
logger.info("Removing orphan instance ${instance.uniqueId} (${instance})")
InstanceUtils.forceUnregisterInstance(instance)
if (owner != null || games.any { instance in it.getRequiredInstances() }) {
return@forEach
}
if (!instance.hasTag(INACTIVE_SINCE_TAG)) {
instance.setTag(INACTIVE_SINCE_TAG, System.currentTimeMillis())
return@forEach
}
if (System.currentTimeMillis() - instance.getTag(INACTIVE_SINCE_TAG) <= CLEANUP_MIN_INACTIVE_TIME) {
return@forEach
}
logger.info("Removing orphan instance ${instance.uniqueId} (${instance})")
InstanceUtils.forceUnregisterInstance(instance)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import com.bluedragonmc.api.grpc.CommonTypes.GameType.GameTypeFieldSelector
import com.bluedragonmc.api.grpc.gameType
import com.bluedragonmc.server.Game
import com.bluedragonmc.server.api.Environment
import com.bluedragonmc.server.module.instance.InstanceModule
import com.bluedragonmc.server.module.minigame.SpawnpointModule
import net.kyori.adventure.text.Component
import net.minestom.server.MinecraftServer
import net.minestom.server.event.EventNode
Expand Down Expand Up @@ -52,12 +50,10 @@ object InstanceUtils {
// If the instance is not empty, attempt to send all players to a lobby
val lobby = Game.games.find { it.name == Environment.defaultGameName }
if (lobby != null) {
val lobbyInstanceOf = lobby.getModule<InstanceModule>()::getSpawningInstance
val spawnpointOf = lobby.getModule<SpawnpointModule>().spawnpointProvider::getSpawnpoint
return CompletableFuture.allOf(
*instance.players
.map { it.setInstance(lobbyInstanceOf(it), spawnpointOf(it)) }
.toTypedArray()
*instance.players.map {
lobby.addPlayer(it, sendPlayer = true)
}.toTypedArray()
)
} else {
// If there's no lobby, repeatedly queue players for a lobby on another server
Expand Down

0 comments on commit bb4f94a

Please sign in to comment.