From b642f0bd589eba59005b2c650f889e4174213aee Mon Sep 17 00:00:00 2001 From: RaftDev Date: Wed, 17 Jan 2024 08:21:30 +0100 Subject: [PATCH] Server status mismatch fix --- .../network/api/test/TestModule.java | 2 ++ .../listener/NetworkServerEventListener.java | 19 +++++++++++++++++++ .../listener/ServerLifecycleListener.java | 5 ----- .../server/entities/SystemNetworkServer.java | 12 +++++++++++- .../listener/NetworkServerListener.java | 13 +++++++++++++ 5 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 api/src/test/java/fr/atlasworld/network/api/test/listener/NetworkServerEventListener.java diff --git a/api/src/test/java/fr/atlasworld/network/api/test/TestModule.java b/api/src/test/java/fr/atlasworld/network/api/test/TestModule.java index a13204a..d26fa89 100644 --- a/api/src/test/java/fr/atlasworld/network/api/test/TestModule.java +++ b/api/src/test/java/fr/atlasworld/network/api/test/TestModule.java @@ -2,6 +2,7 @@ import fr.atlasworld.network.api.NetworkModule; import fr.atlasworld.network.api.module.lifecycle.ModuleLoadContext; +import fr.atlasworld.network.api.test.listener.NetworkServerEventListener; import fr.atlasworld.network.api.test.listener.ServerLifecycleListener; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; @@ -14,6 +15,7 @@ public void onLoad(@NotNull ModuleLoadContext ctx) { LOGGER = getLogger(); ctx.getServer().getModuleManager().registerListener(this, new ServerLifecycleListener()); + ctx.getServer().getModuleManager().registerListener(this, new NetworkServerEventListener()); LOGGER.info("I'm running on my own freaking thread ({})! Yeah baby!", getThread()); } diff --git a/api/src/test/java/fr/atlasworld/network/api/test/listener/NetworkServerEventListener.java b/api/src/test/java/fr/atlasworld/network/api/test/listener/NetworkServerEventListener.java new file mode 100644 index 0000000..d2ff306 --- /dev/null +++ b/api/src/test/java/fr/atlasworld/network/api/test/listener/NetworkServerEventListener.java @@ -0,0 +1,19 @@ +package fr.atlasworld.network.api.test.listener; + +import fr.atlasworld.network.api.event.EventHandler; +import fr.atlasworld.network.api.event.EventListener; +import fr.atlasworld.network.api.server.event.ServerInstallationCompletedEvent; +import fr.atlasworld.network.api.server.event.ServerStateChangedEvent; +import fr.atlasworld.network.api.test.TestModule; + +public class NetworkServerEventListener implements EventListener { + @EventHandler + private void onServerStatusChanged(ServerStateChangedEvent event) { + TestModule.LOGGER.info("{} changed status from {} to {}.", event.getServer().getUserReadableName(), event.oldStatus(), event.newStatus()); + } + + @EventHandler + private void onServerInstallComplete(ServerInstallationCompletedEvent event) { + TestModule.LOGGER.info("{} has finished installation.", event.getServer().getUserReadableName()); + } +} diff --git a/api/src/test/java/fr/atlasworld/network/api/test/listener/ServerLifecycleListener.java b/api/src/test/java/fr/atlasworld/network/api/test/listener/ServerLifecycleListener.java index d38b0bd..80f9679 100644 --- a/api/src/test/java/fr/atlasworld/network/api/test/listener/ServerLifecycleListener.java +++ b/api/src/test/java/fr/atlasworld/network/api/test/listener/ServerLifecycleListener.java @@ -44,9 +44,4 @@ private void onSystemStarted(SystemStartedEvent event) { throw new RuntimeException(e); } } - - @EventHandler - private void onServerStatusChanged(ServerStateChangedEvent event) { - TestModule.LOGGER.info("{} changed status to {}.", event.getServer().getUserReadableName(), event.newStatus()); - } } diff --git a/src/main/java/fr/atlasworld/network/core/server/entities/SystemNetworkServer.java b/src/main/java/fr/atlasworld/network/core/server/entities/SystemNetworkServer.java index 27708fb..389bdab 100644 --- a/src/main/java/fr/atlasworld/network/core/server/entities/SystemNetworkServer.java +++ b/src/main/java/fr/atlasworld/network/core/server/entities/SystemNetworkServer.java @@ -79,9 +79,19 @@ public SystemNetworkServer(@NotNull UUID identifier, long remoteId, @NotNull Pub public static ServerStatus asApiStatus(com.mattmalec.pterodactyl4j.ServerStatus status, @Nullable UtilizationState uStatus) { return switch (status) { - case INSTALLING, RESTORING_BACKUP -> ServerStatus.INSTALLING; case INSTALL_FAILED -> ServerStatus.CORRUPTED; case SUSPENDED -> ServerStatus.DISABLED; + case INSTALLING, RESTORING_BACKUP ->{ + if (uStatus == null) + yield ServerStatus.INSTALLING; + + yield switch (uStatus) { + case OFFLINE -> ServerStatus.STOPPED; + case STARTING -> ServerStatus.STARTING; + case RUNNING -> ServerStatus.STARTED; + case STOPPING -> ServerStatus.STOPPING; + }; + } case UNKNOWN -> { if (uStatus == null) yield ServerStatus.UNKNOWN; diff --git a/src/main/java/fr/atlasworld/network/core/server/listener/NetworkServerListener.java b/src/main/java/fr/atlasworld/network/core/server/listener/NetworkServerListener.java index bc7b376..42245e5 100644 --- a/src/main/java/fr/atlasworld/network/core/server/listener/NetworkServerListener.java +++ b/src/main/java/fr/atlasworld/network/core/server/listener/NetworkServerListener.java @@ -40,10 +40,14 @@ public class NetworkServerListener extends ClientSocketListenerAdapter { private final @Nullable ServerBlueprint blueprint; private final UploadCacheIndex cacheIndex; + private boolean installing; + public NetworkServerListener(SystemNetworkServer server, @Nullable ServerBlueprint blueprint, UploadCacheIndex cacheIndex) { this.server = server; this.blueprint = blueprint; this.cacheIndex = cacheIndex; + + this.installing = this.blueprint != null; } @Override @@ -51,6 +55,12 @@ public void onStatusUpdate(StatusUpdateEvent event) { ServerStatus status = SystemNetworkServer.asApiStatus(com.mattmalec.pterodactyl4j.ServerStatus.UNKNOWN, event.getState()); ServerStatus oldStatus = this.server.getStatus(); + if (oldStatus == ServerStatus.INSTALLING && this.installing) + return; + + if (status == oldStatus) + return; + this.server.updateStatus(status); AtlasNetwork.getInstance().getModuleManager().callEvent(new ServerStateChangedEventImpl(this.server, oldStatus, status)); @@ -95,6 +105,9 @@ public void onInstallCompleted(InstallCompletedEvent event) { event.getServer().getFileManager().decompress(remoteFile).execute(); + this.installing = false; + this.server.updateStatus(ServerStatus.STOPPED); + SystemNetworkServer.LOGGER.debug("Server files successfully uploaded."); AtlasNetwork.getInstance().getModuleManager().callEvent(new ServerInstallationCompletedEventImpl(this.server));