Skip to content

Commit

Permalink
Fixed offline servers sometimes not being marked as offline on velocity
Browse files Browse the repository at this point in the history
  • Loading branch information
ajgeiss0702 committed Oct 20, 2022
1 parent 11dcda4 commit d78c795
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
4 changes: 4 additions & 0 deletions api/src/main/java/us/ajg0702/queue/api/AjQueueAPI.java
Expand Up @@ -6,6 +6,8 @@
import us.ajg0702.utils.common.Config;
import us.ajg0702.utils.common.Messages;

import java.util.concurrent.ExecutorService;

public abstract class AjQueueAPI {

public static AjQueueAPI INSTANCE;
Expand Down Expand Up @@ -106,4 +108,6 @@ public static AjQueueAPI getInstance() {
* Tells ajQueue to shut down.
*/
public abstract void shutdown();

public abstract ExecutorService getServersUpdateExecutor();
}
6 changes: 6 additions & 0 deletions common/src/main/java/us/ajg0702/queue/common/QueueMain.java
Expand Up @@ -13,6 +13,7 @@

import java.io.File;
import java.util.LinkedHashMap;
import java.util.concurrent.ExecutorService;
import java.util.regex.Pattern;

public class QueueMain extends AjQueueAPI {
Expand Down Expand Up @@ -123,6 +124,11 @@ public void shutdown() {
updater.shutdown();
}

@Override
public ExecutorService getServersUpdateExecutor() {
return taskManager.getServersUpdateExecutor();
}


private final File dataFolder;

Expand Down
Expand Up @@ -52,16 +52,7 @@ public CompletableFuture<AdaptedServerPing> ping(boolean debug, QueueLogger logg

handle.ping((pp, error) -> {
if(error != null) {

long lastOnline = lastSuccessfullPing == null ? 0 : lastSuccessfullPing.getFetchedTime();
offlineTime = (int) Math.min(sent - lastOnline, Integer.MAX_VALUE);

lastOffline = sent;

future.completeExceptionally(error);
lastPing = null;
if(debug) logger.info("[pinger] [" + getName() + "] offline:", error);
return;
markOffline(debug, logger, future, sent, error);
}

offlineTime = 0;
Expand All @@ -80,6 +71,17 @@ public CompletableFuture<AdaptedServerPing> ping(boolean debug, QueueLogger logg
return future;
}

private void markOffline(boolean debug, QueueLogger logger, CompletableFuture<AdaptedServerPing> future, long sent, Throwable e) {
long lastOnline = lastSuccessfullPing == null ? 0 : lastSuccessfullPing.getFetchedTime();
offlineTime = (int) Math.min(sent - lastOnline, Integer.MAX_VALUE);

lastOffline = sent;

future.completeExceptionally(e);
lastPing = null;
if(debug) logger.info("[pinger] [" + getName() + "] offline:", e);
}

@Override
public Optional<AdaptedServerPing> getLastPing() {
return Optional.ofNullable(lastPing);
Expand Down
Expand Up @@ -53,19 +53,12 @@ public CompletableFuture<AdaptedServerPing> ping(boolean debug, QueueLogger logg
if(debug) logger.info("[pinger] [" + getName() + "] sending ping");

serverPing.thenRunAsync(() -> {
System.out.println("thenRunAsync " + getName());
VelocityServerPing ping;
try {
ping = new VelocityServerPing(serverPing.get(), sent);
} catch (Throwable e) {

long lastOnline = lastSuccessfullPing == null ? 0 : lastSuccessfullPing.getFetchedTime();
offlineTime = (int) Math.min(sent - lastOnline, Integer.MAX_VALUE);

lastOffline = sent;

future.completeExceptionally(e);
lastPing = null;
if(debug) logger.info("[pinger] [" + getName() + "] offline:", e);
markOffline(debug, logger, future, sent, e);
return;
}

Expand All @@ -79,10 +72,24 @@ public CompletableFuture<AdaptedServerPing> ping(boolean debug, QueueLogger logg

future.complete(ping);
lastPing = ping;
}).exceptionally(e -> {
markOffline(debug, logger, future, sent, e);
return null;
});
return future;
}

private void markOffline(boolean debug, QueueLogger logger, CompletableFuture<AdaptedServerPing> future, long sent, Throwable e) {
long lastOnline = lastSuccessfullPing == null ? 0 : lastSuccessfullPing.getFetchedTime();
offlineTime = (int) Math.min(sent - lastOnline, Integer.MAX_VALUE);

lastOffline = sent;

future.completeExceptionally(e);
lastPing = null;
if(debug) logger.info("[pinger] [" + getName() + "] offline:", e);
}

@Override
public Optional<AdaptedServerPing> getLastPing() {
return Optional.ofNullable(lastPing);
Expand Down

0 comments on commit d78c795

Please sign in to comment.