From 3def65762f9276e3f96a821d15a0e28563289146 Mon Sep 17 00:00:00 2001 From: Nils Gereke Date: Fri, 1 Mar 2024 22:15:08 +0100 Subject: [PATCH 1/2] feat: delete plugins folder when setup server fix: server version cache catch exception and retry every hour --- .../testsuite/server/ServerInstance.java | 33 ++++++++++++------- .../testsuite/server/ServerManager.java | 2 ++ .../server/meta/ServerVersionCache.java | 22 ++++++++----- 3 files changed, 38 insertions(+), 19 deletions(-) diff --git a/imprex-testsuite-core/src/main/java/dev/imprex/testsuite/server/ServerInstance.java b/imprex-testsuite-core/src/main/java/dev/imprex/testsuite/server/ServerInstance.java index 96fbe57..f9372b9 100644 --- a/imprex-testsuite-core/src/main/java/dev/imprex/testsuite/server/ServerInstance.java +++ b/imprex-testsuite-core/src/main/java/dev/imprex/testsuite/server/ServerInstance.java @@ -182,20 +182,27 @@ public CompletableFuture setupServer() { return CompletableFuture.failedFuture(new NullPointerException("No template instance")); } - CompletableFuture future = new CompletableFuture<>(); - + return this.deletePluginFolder() + .thenCompose(__ -> this.uploadServerFiles()); + } + + private CompletableFuture deletePluginFolder() { + return PteroUtil.execute(this.server.retrieveDirectory()) + .thenApply(directory -> directory.getDirectoryByName("plugins")) + .thenCompose(optional -> { + if (optional.isEmpty()) { + return CompletableFuture.completedFuture(null); + } + return PteroUtil.execute(optional.get().delete()); + }); + } + + private CompletableFuture uploadServerFiles() { List fileList = new CopyOnWriteArrayList<>(this.template.getFiles()); int pathPrefix = this.template.getPath().toString().length(); - PteroUtil.updateDirectory(this.server, pathPrefix, fileList).whenComplete((__, error) -> { - if (error != null) { - future.completeExceptionally(error); - } else { - future.complete(null); - } - System.gc(); // TODO test phase (memory issue with pterodactyl4j api) - }); - return future; + return PteroUtil.updateDirectory(server, pathPrefix, fileList) + .thenAccept(__ -> System.gc()); // TODO test phase (memory issue with pterodactyl4j api) } public CompletableFuture executeCommand(String command) { @@ -255,6 +262,10 @@ public CompletableFuture delete() { public void resetInactiveTime() { this.inactiveTime.getAndSet(System.currentTimeMillis() + MAX_INACTIVE_TIME); } + + public boolean setIdleTimeout(boolean state) { + return this.idleTimeout.compareAndSet(!state, state); + } public boolean toggleIdleTimeout() { boolean state = this.idleTimeout.get(); diff --git a/imprex-testsuite-core/src/main/java/dev/imprex/testsuite/server/ServerManager.java b/imprex-testsuite-core/src/main/java/dev/imprex/testsuite/server/ServerManager.java index 1c3773c..0d039e2 100644 --- a/imprex-testsuite-core/src/main/java/dev/imprex/testsuite/server/ServerManager.java +++ b/imprex-testsuite-core/src/main/java/dev/imprex/testsuite/server/ServerManager.java @@ -82,6 +82,8 @@ public void run() { if (instance == null) { instance = new ServerInstance(this, server); + instance.setIdleTimeout(false); // only enable when started with command + this.serverInstances.put(identifier, instance); instance.notifyMessage("Detected server instance"); diff --git a/imprex-testsuite-core/src/main/java/dev/imprex/testsuite/server/meta/ServerVersionCache.java b/imprex-testsuite-core/src/main/java/dev/imprex/testsuite/server/meta/ServerVersionCache.java index ac08490..4d10d8d 100644 --- a/imprex-testsuite-core/src/main/java/dev/imprex/testsuite/server/meta/ServerVersionCache.java +++ b/imprex-testsuite-core/src/main/java/dev/imprex/testsuite/server/meta/ServerVersionCache.java @@ -21,6 +21,8 @@ import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import dev.imprex.testsuite.TestsuiteLogger; + public class ServerVersionCache implements Runnable { private static final Gson GSON = new GsonBuilder() @@ -47,15 +49,19 @@ public void run() { } for (ServerType serverType : ServerType.values()) { - Set versionSet = ServerVersion.fetchVersionList(serverType); - if (versionSet == null) { - return; + try { + Set versionSet = ServerVersion.fetchVersionList(serverType); + if (versionSet == null) { + return; + } + + this.cache.computeIfAbsent(serverType, (key) -> new HashSet<>()).addAll(versionSet); + } catch (Exception e) { + TestsuiteLogger.error(e, "Unable to update version cache for " + serverType.name()); } - - this.cache.computeIfAbsent(serverType, (key) -> new HashSet<>()).addAll(versionSet); } - System.out.println("Version Cache successfully updated!"); + TestsuiteLogger.info("Version cache successfully updated!"); this.needsUpdate = false; this.updateTime = System.currentTimeMillis() + UPDATE_COOLDOWN; @@ -82,7 +88,7 @@ private void load() { this.cache.put(serverType, versions); } } catch (Exception e) { - e.printStackTrace(); + TestsuiteLogger.error(e, "Unable to load version cache file!"); this.needsUpdate = true; } } @@ -109,7 +115,7 @@ private void save() { GSON.toJson(root, bufferedWriter); } } catch (IOException e) { - e.printStackTrace(); + TestsuiteLogger.error(e, "Unable to save version cache file!"); } } From 7cae39031157d02cf439e21f3f3af3c8c7d20840 Mon Sep 17 00:00:00 2001 From: Nils Gereke Date: Fri, 1 Mar 2024 23:30:05 +0100 Subject: [PATCH 2/2] chore: only delete jar files --- .../testsuite/server/ServerInstance.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/imprex-testsuite-core/src/main/java/dev/imprex/testsuite/server/ServerInstance.java b/imprex-testsuite-core/src/main/java/dev/imprex/testsuite/server/ServerInstance.java index f9372b9..32c20a6 100644 --- a/imprex-testsuite-core/src/main/java/dev/imprex/testsuite/server/ServerInstance.java +++ b/imprex-testsuite-core/src/main/java/dev/imprex/testsuite/server/ServerInstance.java @@ -13,6 +13,8 @@ import com.mattmalec.pterodactyl4j.UtilizationState; import com.mattmalec.pterodactyl4j.client.entities.ClientServer; +import com.mattmalec.pterodactyl4j.client.entities.Directory; +import com.mattmalec.pterodactyl4j.client.entities.GenericFile; import com.mattmalec.pterodactyl4j.client.entities.Utilization; import com.mattmalec.pterodactyl4j.client.managers.WebSocketManager; import com.mattmalec.pterodactyl4j.entities.Allocation; @@ -182,18 +184,28 @@ public CompletableFuture setupServer() { return CompletableFuture.failedFuture(new NullPointerException("No template instance")); } - return this.deletePluginFolder() + return this.deletePluginJars() .thenCompose(__ -> this.uploadServerFiles()); } - private CompletableFuture deletePluginFolder() { + private CompletableFuture deletePluginJars() { return PteroUtil.execute(this.server.retrieveDirectory()) .thenApply(directory -> directory.getDirectoryByName("plugins")) .thenCompose(optional -> { if (optional.isEmpty()) { return CompletableFuture.completedFuture(null); } - return PteroUtil.execute(optional.get().delete()); + + Directory directory = optional.get(); + CompletableFuture future = new CompletableFuture(); + + for (GenericFile file : directory.getFiles()) { + if (file.getName().endsWith(".jar")) { + future = future.thenAccept(__ -> PteroUtil.execute(file.delete())); + } + } + + return future; }); }