port update checker to CompletableFuture

This commit is contained in:
FalsePattern committed 2022-06-19 22:57:11 +02:00
1 parent 454e62c348
commit 06833a3607
5 files changed
+144 -137

No files matched your search

@@ -6,21 +6,17 @@ import com.falsepattern.lib.internal.FalsePatternLib;
import com.falsepattern.lib.internal.LibraryConfig;
import com.falsepattern.lib.updates.ModUpdateInfo;
import com.falsepattern.lib.updates.UpdateChecker;
import com.falsepattern.lib.util.AsyncUtil;
import cpw.mods.fml.common.event.FMLConstructionEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import lombok.val;
import lombok.var;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.CompletableFuture;
public class CommonProxy {
protected Future<List<ModUpdateInfo>> updatesFuture;
protected CompletableFuture<List<ModUpdateInfo>> updatesFuture;
public void preInit(FMLPreInitializationEvent e) {
ConfigurationManager.registerBus();
@@ -31,37 +27,22 @@ public class CommonProxy {
}
if (LibraryConfig.ENABLE_UPDATE_CHECKER) {
FalsePatternLib.getLog().info("Launching asynchronous update check.");
val updateCheckFuture = UpdateChecker.fetchUpdatesAsync(FalsePatternLib.UPDATE_URL);
updatesFuture = AsyncUtil.asyncWorker.submit(new Callable<List<ModUpdateInfo>>() {
@Override
public List<ModUpdateInfo> call() {
//Deadlock avoidance
if (updateCheckFuture.isCancelled()) {
updatesFuture = null;
return null;
}
if (!updateCheckFuture.isDone()) {
updatesFuture = AsyncUtil.asyncWorker.submit(this);
return null;
}
try {
var updates = updateCheckFuture.get();
if (updates != null && updates.size() > 0) {
for (val update : updates) {
update.log(FalsePatternLib.getLog());
}
} else if (updates == null) {
FalsePatternLib.getLog().warn("Unknown error while checking updates.");
} else {
FalsePatternLib.getLog().info("All checked mods up to date!");
updates = null;
}
return updates;
} catch (InterruptedException | ExecutionException ex) {
FalsePatternLib.getLog().warn("Error while checking updates", ex);
}
return null;
updatesFuture = UpdateChecker.fetchUpdatesAsync(FalsePatternLib.UPDATE_URL).thenApplyAsync(updates -> {
if (updates == null) {
updates = Collections.emptyList();
}
if (updates.isEmpty()) {
FalsePatternLib.getLog().info("No updates found.");
} else {
FalsePatternLib.getLog().info("Found {} updates.", updates.size());
for (val update : updates) {
update.log(FalsePatternLib.getLog());
}
}
return updates;
}).exceptionally(ex -> {
FalsePatternLib.getLog().error("Failed to check for updates!", ex);
return Collections.emptyList();
});
}
}