Start downloading dependencies on construct, relocate and load on enable

This commit is contained in:
Vankka 2024-06-16 14:40:45 +03:00
parent 8078f427a4
commit 26c6a909da
No known key found for this signature in database
GPG Key ID: 62E48025ED4E7EBB
9 changed files with 30 additions and 36 deletions

View File

@ -47,8 +47,7 @@ public class DiscordSRVBukkitBootstrap extends BukkitBootstrap implements IBoots
logger, logger,
plugin.getDataFolder().toPath(), plugin.getDataFolder().toPath(),
getDependencyResources(), getDependencyResources(),
getClasspathAppender(), getClasspathAppender()
false
); );
} }

View File

@ -44,8 +44,7 @@ public class DiscordSRVBungeeBootstrap extends BungeeBootstrap implements IBoots
logger, logger,
plugin.getDataFolder().toPath(), plugin.getDataFolder().toPath(),
new String[] {"dependencies/runtimeDownload-bungee.txt"}, new String[] {"dependencies/runtimeDownload-bungee.txt"},
getClasspathAppender(), getClasspathAppender()
true
); );
} }

View File

@ -691,7 +691,7 @@ public abstract class AbstractDiscordSRV<
+ "but linked-accounts.provider is set to \"minecraftauth\". Linked accounts will be disabled"); + "but linked-accounts.provider is set to \"minecraftauth\". Linked accounts will be disabled");
break; break;
} }
dependencyManager.mcAuthLib().download().get(); dependencyManager.mcAuthLib().downloadRelocateAndLoad().get();
linkProvider = new MinecraftAuthenticationLinker(this); linkProvider = new MinecraftAuthenticationLinker(this);
logger().info("Using minecraftauth.me for linked accounts"); logger().info("Using minecraftauth.me for linked accounts");
break; break;
@ -725,7 +725,7 @@ public abstract class AbstractDiscordSRV<
logger().warning("Data will not persist across server restarts."); logger().warning("Data will not persist across server restarts.");
} }
if (storageType.hikari()) { if (storageType.hikari()) {
dependencyManager().hikari().download().get(); dependencyManager().hikari().downloadRelocateAndLoad().get();
} }
storage = storageType.storageFunction().apply(this); storage = storageType.storageFunction().apply(this);
storage.initialize(); storage.initialize();

View File

@ -41,17 +41,16 @@ public class LifecycleManager {
private final Logger logger; private final Logger logger;
private final ExecutorService taskPool; private final ExecutorService taskPool;
private final DependencyLoader dependencyLoader; private final DependencyLoader dependencyLoader;
private CompletableFuture<?> completableFuture; private final CompletableFuture<?> completableFuture;
public LifecycleManager( public LifecycleManager(
Logger logger, Logger logger,
Path dataDirectory, Path dataDirectory,
String[] dependencyResources, String[] dependencyResources,
ClasspathAppender classpathAppender, ClasspathAppender classpathAppender
boolean useExecutor
) throws IOException { ) throws IOException {
this.logger = logger; this.logger = logger;
this.taskPool = useExecutor ? Executors.newSingleThreadExecutor(runnable -> new Thread(runnable, "DiscordSRV Initialization")) : null; this.taskPool = Executors.newSingleThreadExecutor(runnable -> new Thread(runnable, "DiscordSRV Initialization"));
List<String> resourcePaths = new ArrayList<>(Collections.singletonList( List<String> resourcePaths = new ArrayList<>(Collections.singletonList(
"dependencies/runtimeDownload-common.txt" "dependencies/runtimeDownload-common.txt"
@ -64,35 +63,29 @@ public class LifecycleManager {
classpathAppender, classpathAppender,
resourcePaths.toArray(new String[0]) resourcePaths.toArray(new String[0])
); );
this.completableFuture = dependencyLoader.download();
this.completableFuture.whenComplete((v, t) -> taskPool.shutdownNow());
} }
public void loadAndEnable(Supplier<DiscordSRV> discordSRVSupplier) { public void loadAndEnable(Supplier<DiscordSRV> discordSRVSupplier) {
if (load()) { if (relocateAndLoad()) {
enable(discordSRVSupplier); discordSRVSupplier.get().runEnable();
} }
} }
private boolean load() { private boolean relocateAndLoad() {
try { try {
this.completableFuture = dependencyLoader.download();
if (taskPool != null) {
completableFuture.whenComplete((v, t) -> taskPool.shutdown());
}
completableFuture.get(); completableFuture.get();
dependencyLoader.relocateAndLoad(false).get();
return true; return true;
} catch (InterruptedException ignored) { } catch (InterruptedException ignored) {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} catch (ExecutionException e) { } catch (ExecutionException e) {
logger.error("Failed to download dependencies", e.getCause()); logger.error("Failed to download, relocate or load dependencies", e.getCause());
} }
return false; return false;
} }
private void enable(Supplier<DiscordSRV> discordSRVSupplier) {
discordSRVSupplier.get().runEnable();
}
public void reload(DiscordSRV discordSRV) { public void reload(DiscordSRV discordSRV) {
if (discordSRV == null) { if (discordSRV == null) {
return; return;

View File

@ -87,19 +87,23 @@ public class DependencyLoader {
return dependencyManager; return dependencyManager;
} }
public IsolatedClassLoader loadIntoIsolated() throws IOException { public IsolatedClassLoader intoIsolated() throws IOException {
IsolatedClassLoader classLoader = new IsolatedClassLoader(); IsolatedClassLoader classLoader = new IsolatedClassLoader();
download(classLoader).join(); downloadRelocateAndLoad().join();
return classLoader; return classLoader;
} }
public CompletableFuture<Void> download() { public CompletableFuture<Void> downloadRelocateAndLoad() {
return download(classpathAppender); return download().thenCompose(v -> relocateAndLoad(true));
} }
private CompletableFuture<Void> download(ClasspathAppender appender) { public CompletableFuture<Void> download() {
return dependencyManager.downloadAll(executor, REPOSITORIES) return dependencyManager.downloadAll(executor, REPOSITORIES);
.thenCompose(v -> dependencyManager.relocateAll(executor)) }
.thenCompose(v -> dependencyManager.loadAll(executor, appender));
public CompletableFuture<Void> relocateAndLoad(boolean useExecutor) {
Executor executorToUse = useExecutor ? executor : null;
return dependencyManager.relocateAll(executorToUse)
.thenCompose(v -> dependencyManager.loadAll(executorToUse, classpathAppender));
} }
} }

View File

@ -43,7 +43,7 @@ public class H2Storage extends SQLStorage {
@Override @Override
public void initialize() { public void initialize() {
try { try {
classLoader = discordSRV.dependencyManager().h2().loadIntoIsolated(); classLoader = discordSRV.dependencyManager().h2().intoIsolated();
} catch (IOException e) { } catch (IOException e) {
throw new StorageException(e); throw new StorageException(e);
} }

View File

@ -23,7 +23,7 @@ public class MariaDBStorage extends HikariStorage {
@Override @Override
public void initialize() { public void initialize() {
try { try {
discordSRV.dependencyManager().mariadb().download().join(); discordSRV.dependencyManager().mariadb().downloadRelocateAndLoad().join();
super.initialize(); super.initialize();
} catch (IOException e) { } catch (IOException e) {
throw new StorageException(e); throw new StorageException(e);

View File

@ -79,7 +79,7 @@ public class MySQLStorage extends HikariStorage {
@Override @Override
public void initialize() { public void initialize() {
try { try {
initializeWithContext(classLoader = discordSRV.dependencyManager().mysql().loadIntoIsolated()); initializeWithContext(classLoader = discordSRV.dependencyManager().mysql().intoIsolated());
} catch (IOException e) { } catch (IOException e) {
throw new StorageException(e); throw new StorageException(e);
} }

View File

@ -64,8 +64,7 @@ public class DiscordSRVVelocityBootstrap implements IBootstrap {
this.logger, this.logger,
dataDirectory, dataDirectory,
new String[] {"dependencies/runtimeDownload-velocity.txt"}, new String[] {"dependencies/runtimeDownload-velocity.txt"},
classpathAppender, classpathAppender
true
); );
this.proxyServer = proxyServer; this.proxyServer = proxyServer;
this.pluginContainer = pluginContainer; this.pluginContainer = pluginContainer;