Single threaded dependency downloading, don't run enable if dependency downloading fails

This commit is contained in:
Vankka 2023-12-19 00:36:53 +02:00
parent cf7ee4a914
commit 192ce5e152
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
4 changed files with 28 additions and 26 deletions

View File

@ -76,12 +76,18 @@ public class DiscordSRVBukkitBootstrap extends BukkitBootstrap implements IBoots
} catch (ClassNotFoundException ignored) {} } catch (ClassNotFoundException ignored) {}
if (isFolia) { if (isFolia) {
discordSRV.invokeServerStarted(); if (discordSRV != null) {
discordSRV.invokeServerStarted();
}
return; return;
} }
// Run a task on the main thread 1 tick later, so essentially when the server has finished booting // Run a task on the main thread 1 tick later, so essentially when the server has finished booting
getPlugin().getServer().getScheduler().runTaskLater(getPlugin(), () -> discordSRV.invokeServerStarted(), 1L); getPlugin().getServer().getScheduler().runTaskLater(getPlugin(), () -> {
if (discordSRV != null) {
discordSRV.invokeServerStarted();
}
}, 1L);
} }
@Override @Override

View File

@ -205,9 +205,7 @@ public abstract class AbstractDiscordSRV<
.addInterceptor(chain -> { .addInterceptor(chain -> {
Request original = chain.request(); Request original = chain.request();
String host = original.url().host(); String host = original.url().host();
boolean isDiscord = host.endsWith("discord.com") boolean isDiscord = host.matches("(.*\\.|^)(?:discord\\.(?:com|gg)|(discordapp\\.com))");
|| host.endsWith("discordapp.com")
|| host.endsWith("discord.gg");
String userAgent = isDiscord String userAgent = isDiscord
? "DiscordBot (https://github.com/DiscordSRV/DiscordSRV, " + versionInfo().version() + ")" ? "DiscordBot (https://github.com/DiscordSRV/DiscordSRV, " + versionInfo().version() + ")"

View File

@ -22,7 +22,6 @@ import com.discordsrv.api.DiscordSRVApi;
import com.discordsrv.common.DiscordSRV; import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.dependency.DependencyLoader; import com.discordsrv.common.dependency.DependencyLoader;
import com.discordsrv.common.logging.Logger; import com.discordsrv.common.logging.Logger;
import com.discordsrv.common.scheduler.threadfactory.CountingForkJoinWorkerThreadFactory;
import dev.vankka.dependencydownload.classpath.ClasspathAppender; import dev.vankka.dependencydownload.classpath.ClasspathAppender;
import java.io.IOException; import java.io.IOException;
@ -33,13 +32,14 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Supplier; import java.util.function.Supplier;
public class LifecycleManager { public class LifecycleManager {
private final Logger logger; private final Logger logger;
private final ForkJoinPool taskPool; private final ExecutorService taskPool;
private final DependencyLoader dependencyLoader; private final DependencyLoader dependencyLoader;
private final CompletableFuture<?> completableFuture; private final CompletableFuture<?> completableFuture;
@ -50,12 +50,7 @@ public class LifecycleManager {
ClasspathAppender classpathAppender ClasspathAppender classpathAppender
) throws IOException { ) throws IOException {
this.logger = logger; this.logger = logger;
this.taskPool = new ForkJoinPool( this.taskPool = Executors.newSingleThreadExecutor(runnable -> new Thread(runnable, "DiscordSRV Initialization"));
Runtime.getRuntime().availableProcessors(),
new CountingForkJoinWorkerThreadFactory("DiscordSRV Initialization #%s"),
null,
false
);
List<String> resourcePaths = new ArrayList<>(Collections.singletonList( List<String> resourcePaths = new ArrayList<>(Collections.singletonList(
"dependencies/runtimeDownload-common.txt" "dependencies/runtimeDownload-common.txt"
@ -74,22 +69,24 @@ public class LifecycleManager {
} }
public void loadAndEnable(Supplier<DiscordSRV> discordSRVSupplier) { public void loadAndEnable(Supplier<DiscordSRV> discordSRVSupplier) {
load(); if (load()) {
enable(discordSRVSupplier); enable(discordSRVSupplier);
}
} }
public void load() { private boolean load() {
try { try {
completableFuture.get(); completableFuture.get();
} catch (ExecutionException | InterruptedException e) { return true;
logger.error("Failed to download dependencies", e); } catch (InterruptedException ignored) {
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
logger.error("Failed to download dependencies", e.getCause());
} }
return false;
} }
public void enable(Supplier<DiscordSRV> discordSRVSupplier) { private void enable(Supplier<DiscordSRV> discordSRVSupplier) {
if (!completableFuture.isDone()) {
return;
}
discordSRVSupplier.get().runEnable(); discordSRVSupplier.get().runEnable();
} }
@ -114,7 +111,9 @@ public class LifecycleManager {
discordSRV.invokeDisable().get(/*15, TimeUnit.SECONDS*/); discordSRV.invokeDisable().get(/*15, TimeUnit.SECONDS*/);
} catch (InterruptedException/* | TimeoutException*/ e) { } catch (InterruptedException/* | TimeoutException*/ e) {
logger.warning("Timed out/interrupted shutting down DiscordSRV"); logger.warning("Timed out/interrupted shutting down DiscordSRV");
} catch (ExecutionException ignored) {} } catch (ExecutionException e) {
logger.error("Failed to disable", e.getCause());
}
} }
public DependencyLoader getDependencyLoader() { public DependencyLoader getDependencyLoader() {

View File

@ -65,7 +65,7 @@ public class DiscordSRVSpongeBootstrap extends AbstractBootstrap implements ISpo
@Override @Override
public void onConstruct() { public void onConstruct() {
lifecycleManager.load(); lifecycleManager.loadAndEnable(() -> this.discordSRV = new SpongeDiscordSRV(this));
this.commandHandler = new SpongeCommandHandler(() -> discordSRV, pluginContainer); this.commandHandler = new SpongeCommandHandler(() -> discordSRV, pluginContainer);
game.eventManager().registerListeners(pluginContainer, commandHandler); game.eventManager().registerListeners(pluginContainer, commandHandler);
@ -73,7 +73,6 @@ public class DiscordSRVSpongeBootstrap extends AbstractBootstrap implements ISpo
@Override @Override
public void onStarted() { public void onStarted() {
lifecycleManager.enable(() -> this.discordSRV = new SpongeDiscordSRV(this));
if (discordSRV != null) { if (discordSRV != null) {
discordSRV.invokeServerStarted(); discordSRV.invokeServerStarted();
} }