mirror of
https://github.com/DiscordSRV/Ascension.git
synced 2024-11-21 11:45:25 +01:00
Start downloading dependencies on construct, relocate and load on enable
This commit is contained in:
parent
8078f427a4
commit
26c6a909da
@ -47,8 +47,7 @@ public class DiscordSRVBukkitBootstrap extends BukkitBootstrap implements IBoots
|
||||
logger,
|
||||
plugin.getDataFolder().toPath(),
|
||||
getDependencyResources(),
|
||||
getClasspathAppender(),
|
||||
false
|
||||
getClasspathAppender()
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -44,8 +44,7 @@ public class DiscordSRVBungeeBootstrap extends BungeeBootstrap implements IBoots
|
||||
logger,
|
||||
plugin.getDataFolder().toPath(),
|
||||
new String[] {"dependencies/runtimeDownload-bungee.txt"},
|
||||
getClasspathAppender(),
|
||||
true
|
||||
getClasspathAppender()
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
|
@ -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;
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user