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,
plugin.getDataFolder().toPath(),
getDependencyResources(),
getClasspathAppender(),
false
getClasspathAppender()
);
}

View File

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

View File

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

View File

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

View File

@ -87,19 +87,23 @@ public class DependencyLoader {
return dependencyManager;
}
public IsolatedClassLoader loadIntoIsolated() throws IOException {
public IsolatedClassLoader intoIsolated() throws IOException {
IsolatedClassLoader classLoader = new IsolatedClassLoader();
download(classLoader).join();
downloadRelocateAndLoad().join();
return classLoader;
}
public CompletableFuture<Void> download() {
return download(classpathAppender);
public CompletableFuture<Void> downloadRelocateAndLoad() {
return download().thenCompose(v -> relocateAndLoad(true));
}
private CompletableFuture<Void> download(ClasspathAppender appender) {
return dependencyManager.downloadAll(executor, REPOSITORIES)
.thenCompose(v -> dependencyManager.relocateAll(executor))
.thenCompose(v -> dependencyManager.loadAll(executor, appender));
public CompletableFuture<Void> download() {
return dependencyManager.downloadAll(executor, REPOSITORIES);
}
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
public void initialize() {
try {
classLoader = discordSRV.dependencyManager().h2().loadIntoIsolated();
classLoader = discordSRV.dependencyManager().h2().intoIsolated();
} catch (IOException e) {
throw new StorageException(e);
}

View File

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

View File

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

View File

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