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) {}
if (isFolia) {
discordSRV.invokeServerStarted();
if (discordSRV != null) {
discordSRV.invokeServerStarted();
}
return;
}
// 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

View File

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

View File

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