Fix logging on future cancelling

This commit is contained in:
creeper123123321 2020-04-30 10:15:53 -03:00
parent 253a4452c6
commit 35b9d8b07c

View File

@ -28,6 +28,8 @@ import com.github.creeper123123321.viafabric.ViaFabric;
import com.github.creeper123123321.viafabric.commands.NMSCommandSender; import com.github.creeper123123321.viafabric.commands.NMSCommandSender;
import com.github.creeper123123321.viafabric.commands.UserCommandSender; import com.github.creeper123123321.viafabric.commands.UserCommandSender;
import com.github.creeper123123321.viafabric.util.FutureTaskId; import com.github.creeper123123321.viafabric.util.FutureTaskId;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.FabricLoader;
@ -62,6 +64,7 @@ import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.function.Supplier; import java.util.function.Supplier;
@ -125,7 +128,9 @@ public class VRPlatform implements ViaPlatform<UUID> {
return new FutureTaskId(CompletableFuture return new FutureTaskId(CompletableFuture
.runAsync(runnable, ViaFabric.ASYNC_EXECUTOR) .runAsync(runnable, ViaFabric.ASYNC_EXECUTOR)
.exceptionally(throwable -> { .exceptionally(throwable -> {
throwable.printStackTrace(); if (!(throwable instanceof CancellationException)) {
throwable.printStackTrace();
}
return null; return null;
}) })
); );
@ -141,17 +146,15 @@ public class VRPlatform implements ViaPlatform<UUID> {
} }
private TaskId runServerSync(Runnable runnable) { private TaskId runServerSync(Runnable runnable) {
// Kick task needs to be on main thread // Kick task needs to be on main thread, it does already have error logger
return new FutureTaskId(CompletableFuture.runAsync(runnable, getServer())); return new FutureTaskId(CompletableFuture.runAsync(runnable, getServer()));
} }
private TaskId runEventLoop(Runnable runnable) { private TaskId runEventLoop(Runnable runnable) {
return new FutureTaskId( return new FutureTaskId(
CompletableFuture.runAsync(runnable, ViaFabric.EVENT_LOOP) ViaFabric.EVENT_LOOP
.exceptionally(throwable -> { .submit(runnable)
throwable.printStackTrace(); .addListener(errorLogger())
return null;
})
); );
} }
@ -161,11 +164,7 @@ public class VRPlatform implements ViaPlatform<UUID> {
return new FutureTaskId( return new FutureTaskId(
ViaFabric.EVENT_LOOP ViaFabric.EVENT_LOOP
.schedule(runnable, ticks * 50, TimeUnit.MILLISECONDS) .schedule(runnable, ticks * 50, TimeUnit.MILLISECONDS)
.addListener(future -> { .addListener(errorLogger())
if (!future.isSuccess()) {
future.cause().printStackTrace();
}
})
); );
} }
@ -175,14 +174,18 @@ public class VRPlatform implements ViaPlatform<UUID> {
return new FutureTaskId( return new FutureTaskId(
ViaFabric.EVENT_LOOP ViaFabric.EVENT_LOOP
.scheduleAtFixedRate(runnable, 0, ticks * 50, TimeUnit.MILLISECONDS) .scheduleAtFixedRate(runnable, 0, ticks * 50, TimeUnit.MILLISECONDS)
.addListener(future -> { .addListener(errorLogger())
if (!future.isSuccess()) {
future.cause().printStackTrace();
}
})
); );
} }
private <T extends Future<?>> GenericFutureListener<T> errorLogger() {
return future -> {
if (!future.isCancelled() && future.cause() != null) {
future.cause().printStackTrace();
}
};
}
@Override @Override
public void cancelTask(TaskId taskId) { public void cancelTask(TaskId taskId) {
if (taskId instanceof FutureTaskId) { if (taskId instanceof FutureTaskId) {