Skip to content
This repository has been archived by the owner on Mar 11, 2024. It is now read-only.

Commit

Permalink
Server status mismatch fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Raft08 committed Jan 17, 2024
1 parent 87e2da6 commit b642f0b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,27 @@ 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
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));
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit b642f0b

Please sign in to comment.