From b7834ab389fc1f37818809fa0becb6eb7a1820fc Mon Sep 17 00:00:00 2001 From: Liam Date: Tue, 30 Jul 2024 21:04:37 +0100 Subject: [PATCH] change how the mojang server status' are fetched --- .../backend/controller/MojangController.java | 8 +- .../backend/controller/PlayerController.java | 2 +- .../backend/controller/ServerController.java | 2 +- .../redis/EndpointStatusRepository.java | 11 --- .../backend/service/MojangService.java | 77 ++++++++++--------- 5 files changed, 43 insertions(+), 57 deletions(-) delete mode 100644 src/main/java/xyz/mcutils/backend/repository/redis/EndpointStatusRepository.java diff --git a/src/main/java/xyz/mcutils/backend/controller/MojangController.java b/src/main/java/xyz/mcutils/backend/controller/MojangController.java index 3784d7a..4494b3a 100644 --- a/src/main/java/xyz/mcutils/backend/controller/MojangController.java +++ b/src/main/java/xyz/mcutils/backend/controller/MojangController.java @@ -15,8 +15,8 @@ import xyz.mcutils.backend.service.MojangService; import java.util.concurrent.TimeUnit; @RestController -@Tag(name = "Mojang Controller", description = "The Mojang Controller is used to get information about the Mojang APIs.") @RequestMapping(value = "/mojang/", produces = MediaType.APPLICATION_JSON_VALUE) +@Tag(name = "Mojang Controller", description = "The Mojang Controller is used to get information about the Mojang APIs.") public class MojangController { @Autowired @@ -25,12 +25,8 @@ public class MojangController { @ResponseBody @GetMapping(value = "/status") public ResponseEntity getStatus() { - CachedEndpointStatus status = mojangService.getMojangApiStatus(); - - - return ResponseEntity.ok() .cacheControl(CacheControl.maxAge(1, TimeUnit.MINUTES).cachePublic()) - .body(status); + .body(mojangService.getMojangServerStatus()); } } diff --git a/src/main/java/xyz/mcutils/backend/controller/PlayerController.java b/src/main/java/xyz/mcutils/backend/controller/PlayerController.java index 7279200..3d2c189 100644 --- a/src/main/java/xyz/mcutils/backend/controller/PlayerController.java +++ b/src/main/java/xyz/mcutils/backend/controller/PlayerController.java @@ -16,8 +16,8 @@ import xyz.mcutils.backend.service.PlayerService; import java.util.concurrent.TimeUnit; @RestController -@Tag(name = "Player Controller", description = "The Player Controller is used to get information about a player.") @RequestMapping(value = "/player/") +@Tag(name = "Player Controller", description = "The Player Controller is used to get information about a player.") public class PlayerController { private final PlayerService playerService; diff --git a/src/main/java/xyz/mcutils/backend/controller/ServerController.java b/src/main/java/xyz/mcutils/backend/controller/ServerController.java index acc6aa0..c74f72d 100644 --- a/src/main/java/xyz/mcutils/backend/controller/ServerController.java +++ b/src/main/java/xyz/mcutils/backend/controller/ServerController.java @@ -16,8 +16,8 @@ import java.util.Map; import java.util.concurrent.TimeUnit; @RestController -@Tag(name = "Server Controller", description = "The Server Controller is used to get information about a server.") @RequestMapping(value = "/server/") +@Tag(name = "Server Controller", description = "The Server Controller is used to get information about a server.") public class ServerController { private final ServerService serverService; diff --git a/src/main/java/xyz/mcutils/backend/repository/redis/EndpointStatusRepository.java b/src/main/java/xyz/mcutils/backend/repository/redis/EndpointStatusRepository.java deleted file mode 100644 index 3dcb70c..0000000 --- a/src/main/java/xyz/mcutils/backend/repository/redis/EndpointStatusRepository.java +++ /dev/null @@ -1,11 +0,0 @@ -package xyz.mcutils.backend.repository.redis; - -import org.springframework.data.repository.CrudRepository; -import xyz.mcutils.backend.model.cache.CachedEndpointStatus; - -/** - * A cache repository for {@link CachedEndpointStatus}'s. - * - * @author Braydon - */ -public interface EndpointStatusRepository extends CrudRepository { } \ No newline at end of file diff --git a/src/main/java/xyz/mcutils/backend/service/MojangService.java b/src/main/java/xyz/mcutils/backend/service/MojangService.java index 074ce36..0802951 100644 --- a/src/main/java/xyz/mcutils/backend/service/MojangService.java +++ b/src/main/java/xyz/mcutils/backend/service/MojangService.java @@ -9,16 +9,12 @@ import lombok.Getter; import lombok.SneakyThrows; import lombok.extern.log4j.Log4j2; import net.jodah.expiringmap.ExpirationPolicy; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import xyz.mcutils.backend.common.AppConfig; import xyz.mcutils.backend.common.ExpiringSet; import xyz.mcutils.backend.common.MojangServer; import xyz.mcutils.backend.common.WebRequest; -import xyz.mcutils.backend.model.cache.CachedEndpointStatus; import xyz.mcutils.backend.model.token.MojangProfileToken; import xyz.mcutils.backend.model.token.MojangUsernameToUuidToken; -import xyz.mcutils.backend.repository.redis.EndpointStatusRepository; import java.io.IOException; import java.io.InputStream; @@ -27,7 +23,9 @@ import java.nio.charset.StandardCharsets; import java.util.*; import java.util.concurrent.TimeUnit; -@Service @Log4j2(topic = "Mojang Service") @Getter +@Service +@Log4j2(topic = "Mojang Service") +@Getter public class MojangService { /** @@ -48,8 +46,10 @@ public class MojangService { */ private static final long FETCH_BLOCKED_SERVERS_INTERVAL = TimeUnit.HOURS.toMillis(1L); - @Autowired - private EndpointStatusRepository mojangEndpointStatusRepository; + /** + * The interval to fetch the Mojang server status. + */ + private static final long FETCH_MOJANG_SERVERS_STATUS_INTERVAL = TimeUnit.MINUTES.toMillis(1L); /** * A list of banned server hashes provided by Mojang. @@ -69,6 +69,11 @@ public class MojangService { */ private final ExpiringSet blockedServersCache = new ExpiringSet<>(ExpirationPolicy.CREATED, 10L, TimeUnit.MINUTES); + /** + * The status of the Mojang API. + */ + private final List> mojangServerStatus = new ArrayList<>(); + public MojangService() { new Timer().scheduleAtFixedRate(new TimerTask() { @Override @@ -76,6 +81,33 @@ public class MojangService { fetchBlockedServers(); } }, 0L, FETCH_BLOCKED_SERVERS_INTERVAL); + + new Timer().scheduleAtFixedRate(new TimerTask() { + @Override + public void run() { + log.info("Fetching Mojang Server status..."); + Map mojangServers = new HashMap<>(); + Arrays.stream(MojangServer.values()).parallel().forEach(server -> { + log.info("Pinging {}...", server.getEndpoint()); + MojangServer.Status status = server.getStatus(); // Retrieve the server status + log.info("Retrieved status of {}: {}", server.getEndpoint(), status.name()); + mojangServers.put(server, status); // Cache the server status + }); + + mojangServerStatus.clear(); + for (Map.Entry entry : mojangServers.entrySet()) { + MojangServer server = entry.getKey(); + + Map serverStatus = new HashMap<>(); + serverStatus.put("name", server.getName()); + serverStatus.put("endpoint", server.getEndpoint()); + serverStatus.put("status", entry.getValue().name()); + mojangServerStatus.add(serverStatus); + } + + log.info("Fetched Mojang Server status for {} endpoints", mojangServers.size()); + } + }, 0L, FETCH_MOJANG_SERVERS_STATUS_INTERVAL); } /** @@ -166,37 +198,6 @@ public class MojangService { return blocked; } - /** - * Gets the status of the Mojang APIs. - * - * @return the status - */ - public CachedEndpointStatus getMojangApiStatus() { - log.info("Getting Mojang API status"); - Optional endpointStatus = mojangEndpointStatusRepository.findById("mojang-servers-status"); - if (endpointStatus.isPresent() && AppConfig.isProduction()) { - log.info("Got cached Mojang API status"); - return endpointStatus.get(); - } - - Map mojangServers = new HashMap<>(); - Arrays.stream(MojangServer.values()).parallel().forEach(server -> { - log.info("Pinging {}...", server.getEndpoint()); - MojangServer.Status status = server.getStatus(); // Retrieve the server status - log.info("Retrieved status of {}: {}", server.getEndpoint(), status.name()); - mojangServers.put(server, status); // Cache the server status - }); - - log.info("Fetched Mojang API status for {} endpoints", mojangServers.size()); - CachedEndpointStatus status = new CachedEndpointStatus( - "mojang-servers-status", - mojangServers - ); - mojangEndpointStatusRepository.save(status); - status.getCache().setCached(false); - return status; - } - /** * Gets the Session Server profile of the * player with the given UUID.