2026-09-06 18:22:00 +03:00
|
|
|
package online.astradev.packs;
|
|
|
|
|
|
2026-09-19 16:34:09 +03:00
|
|
|
import com.sun.net.httpserver.HttpHandler;
|
|
|
|
|
|
2026-09-06 18:36:18 +03:00
|
|
|
import net.minecraft.commands.CommandSourceStack;
|
|
|
|
|
import net.minecraft.network.Connection;
|
|
|
|
|
import net.minecraft.network.chat.Component;
|
|
|
|
|
import net.minecraft.network.protocol.common.ServerboundResourcePackPacket;
|
|
|
|
|
import net.minecraft.server.MinecraftServer;
|
|
|
|
|
import net.minecraft.server.level.ServerPlayer;
|
|
|
|
|
import net.minecraft.server.network.ServerCommonPacketListenerImpl;
|
|
|
|
|
import net.minecraft.server.network.ServerGamePacketListenerImpl;
|
|
|
|
|
|
2026-09-13 21:55:44 +03:00
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.nio.file.Files;
|
|
|
|
|
import java.nio.file.Path;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.UUID;
|
2026-09-24 19:28:22 +03:00
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
|
import java.util.concurrent.Callable;
|
2026-09-13 21:55:44 +03:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2026-09-24 19:28:22 +03:00
|
|
|
import java.util.concurrent.Executor;
|
2026-09-13 21:55:44 +03:00
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
|
import java.util.concurrent.Executors;
|
2026-09-24 19:28:22 +03:00
|
|
|
import java.util.function.Consumer;
|
2026-09-13 21:55:44 +03:00
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
public final class PackRuntime implements AutoCloseable {
|
|
|
|
|
public static volatile PackRuntime INSTANCE;
|
2026-09-06 18:36:18 +03:00
|
|
|
|
|
|
|
|
private static final String RESOURCE_VERSION = "[88,0]";
|
|
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
private final MinecraftServer server;
|
|
|
|
|
private final Path root;
|
|
|
|
|
private final Path configPath;
|
2026-09-06 18:36:18 +03:00
|
|
|
private final PackHost host;
|
2026-09-13 21:55:44 +03:00
|
|
|
private final Map<ServerCommonPacketListenerImpl, PackSession> sessions =
|
|
|
|
|
new ConcurrentHashMap<>();
|
2026-09-06 18:36:18 +03:00
|
|
|
private final Map<UUID, String> pendingWelcome = new ConcurrentHashMap<>();
|
2026-09-24 19:28:22 +03:00
|
|
|
private final ExecutorService worker;
|
|
|
|
|
private final Executor callbacks;
|
2026-09-06 18:36:18 +03:00
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
private volatile PackConfig config;
|
|
|
|
|
private volatile PackBuilder.Pack current;
|
|
|
|
|
private volatile boolean closed;
|
2026-09-06 18:36:18 +03:00
|
|
|
// Only the server thread changes this flag, including publication and failure callbacks.
|
2026-09-24 19:28:22 +03:00
|
|
|
private volatile boolean building;
|
|
|
|
|
private CompletableFuture<String> activeBuild;
|
|
|
|
|
private BuildInputs activeInputs;
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
public PackRuntime(MinecraftServer server, Path root, Path configPath, PackConfig config) {
|
2026-09-24 19:28:22 +03:00
|
|
|
this(server, root, configPath, config,
|
|
|
|
|
Executors.newSingleThreadExecutor(Thread.ofPlatform().daemon().name("AstraPacks-build").factory()),
|
|
|
|
|
server::execute);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PackRuntime(MinecraftServer server, Path root, Path configPath, PackConfig config,
|
|
|
|
|
ExecutorService worker, Executor callbacks) {
|
2026-09-06 18:28:23 +03:00
|
|
|
this.server = server;
|
|
|
|
|
this.root = root;
|
|
|
|
|
this.configPath = configPath;
|
|
|
|
|
this.config = config;
|
2026-09-06 18:22:00 +03:00
|
|
|
this.host = new PackHost(config.hosting);
|
2026-09-24 19:28:22 +03:00
|
|
|
this.worker = worker;
|
|
|
|
|
this.callbacks = callbacks;
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 19:32:43 +03:00
|
|
|
public void start() throws IOException {
|
2026-09-06 18:36:18 +03:00
|
|
|
if (!config.enabled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (server.getServerResourcePack().isPresent()) {
|
2026-09-13 21:55:44 +03:00
|
|
|
throw new IOException(
|
|
|
|
|
"AstraPacks must be the only pack sender: clear resource-pack in"
|
|
|
|
|
+ " server.properties");
|
2026-09-06 18:36:18 +03:00
|
|
|
}
|
|
|
|
|
|
2026-09-06 19:20:55 +03:00
|
|
|
Files.createDirectories(config.contents(root));
|
2026-09-06 18:36:18 +03:00
|
|
|
PackBuilder.Pack pack = loadStartupPack();
|
|
|
|
|
if (pack != null) {
|
2026-09-06 19:20:55 +03:00
|
|
|
host.publish(pack);
|
|
|
|
|
current = pack;
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
2026-09-06 18:36:18 +03:00
|
|
|
host.start();
|
2026-09-06 19:20:55 +03:00
|
|
|
if (config.hosting.publicAddress.isBlank()) {
|
2026-09-13 21:55:44 +03:00
|
|
|
AstraPacks.LOG.error(
|
|
|
|
|
"Set hosting.publicAddress to the public HTTP address (port {}). Pack delivery"
|
|
|
|
|
+ " is unavailable until configured.",
|
|
|
|
|
config.hosting.port);
|
2026-09-06 19:20:55 +03:00
|
|
|
}
|
2026-09-13 21:55:44 +03:00
|
|
|
AstraPacks.LOG.info(
|
|
|
|
|
"AstraPacks: HTTP port={}, required={}, pack={}",
|
|
|
|
|
config.hosting.port,
|
|
|
|
|
config.delivery.required,
|
|
|
|
|
current == null ? "UNAVAILABLE" : current.path());
|
2026-09-06 18:36:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private PackBuilder.Pack loadStartupPack() {
|
|
|
|
|
if (config.build.onStartup) {
|
2026-09-06 18:28:23 +03:00
|
|
|
try {
|
2026-09-06 18:36:18 +03:00
|
|
|
return new PackBuilder(config).build(root, RESOURCE_VERSION);
|
2026-09-06 18:28:23 +03:00
|
|
|
} catch (IOException e) {
|
2026-09-13 21:55:44 +03:00
|
|
|
AstraPacks.LOG.error(
|
|
|
|
|
"Resource-pack build failed; trying the last successful build", e);
|
2026-09-06 18:28:23 +03:00
|
|
|
}
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
2026-09-06 18:36:18 +03:00
|
|
|
try {
|
2026-09-06 19:20:55 +03:00
|
|
|
return PackBuilder.latest(root, config);
|
2026-09-06 18:36:18 +03:00
|
|
|
} catch (IOException e) {
|
|
|
|
|
AstraPacks.LOG.error("No usable pack. Required-pack logins will be refused.", e);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
|
|
|
|
public PackConfig config() {
|
|
|
|
|
return config;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PackBuilder.Pack current() {
|
|
|
|
|
return current;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
public PackSession session(ServerCommonPacketListenerImpl listener) {
|
|
|
|
|
PackSession session = new PackSession(this, listener);
|
2026-09-06 18:36:18 +03:00
|
|
|
PackSession previous = sessions.put(listener, session);
|
|
|
|
|
if (previous != null) {
|
|
|
|
|
previous.removeFromClient();
|
|
|
|
|
}
|
2026-09-06 18:22:00 +03:00
|
|
|
return session;
|
|
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-13 21:55:44 +03:00
|
|
|
public boolean intercept(
|
|
|
|
|
ServerCommonPacketListenerImpl listener, ServerboundResourcePackPacket packet) {
|
2026-09-06 18:36:18 +03:00
|
|
|
if (!config.enabled) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// Consume stale replies too: vanilla treats any terminal UUID as completion of its task.
|
2026-09-13 21:55:44 +03:00
|
|
|
server.execute(
|
|
|
|
|
() -> {
|
|
|
|
|
PackSession session = sessions.get(listener);
|
|
|
|
|
if (session != null) {
|
|
|
|
|
session.response(packet);
|
|
|
|
|
}
|
|
|
|
|
});
|
2026-09-06 18:22:00 +03:00
|
|
|
return true;
|
|
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
public void tick() {
|
2026-09-06 18:36:18 +03:00
|
|
|
var iterator = sessions.entrySet().iterator();
|
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
|
var entry = iterator.next();
|
|
|
|
|
ServerCommonPacketListenerImpl listener = entry.getKey();
|
2026-09-06 19:20:55 +03:00
|
|
|
Connection connection = listener.getConnection();
|
2026-09-06 18:36:18 +03:00
|
|
|
if (connection.isConnected()) {
|
|
|
|
|
entry.getValue().tick();
|
|
|
|
|
} else {
|
|
|
|
|
pendingWelcome.remove(listener.getOwner().id());
|
|
|
|
|
iterator.remove();
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
public void loaded(ServerCommonPacketListenerImpl listener, String message) {
|
2026-09-06 18:36:18 +03:00
|
|
|
if (listener instanceof ServerGamePacketListenerImpl game) {
|
|
|
|
|
if (!message.isBlank()) {
|
2026-09-06 18:28:23 +03:00
|
|
|
game.player.sendSystemMessage(PackText.of(message, listener.getOwner().name()));
|
2026-09-06 18:36:18 +03:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
pendingWelcome.put(listener.getOwner().id(), message);
|
|
|
|
|
}
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 18:36:18 +03:00
|
|
|
public void welcome(ServerPlayer player) {
|
|
|
|
|
String message = pendingWelcome.remove(player.getUUID());
|
|
|
|
|
if (message != null && !message.isBlank()) {
|
|
|
|
|
player.sendSystemMessage(PackText.of(message, player.getGameProfile().name()));
|
|
|
|
|
}
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 19:20:55 +03:00
|
|
|
public String url(PackBuilder.Pack pack) {
|
|
|
|
|
String address = config.hosting.publicAddress;
|
|
|
|
|
if (address.isBlank()) {
|
2026-09-13 21:55:44 +03:00
|
|
|
throw new IllegalStateException(
|
|
|
|
|
"Set hosting.publicAddress before sending resource packs");
|
2026-09-06 18:36:18 +03:00
|
|
|
}
|
2026-09-06 19:20:55 +03:00
|
|
|
return address.replaceAll("/+$", "") + host.issuePath(pack);
|
2026-09-06 18:36:18 +03:00
|
|
|
}
|
|
|
|
|
|
2026-09-19 16:34:09 +03:00
|
|
|
/** Mounts an editor or other web endpoint on AstraPacks' HTTP origin. */
|
|
|
|
|
public AutoCloseable mountWebContext(String prefix, HttpHandler handler) {
|
|
|
|
|
if (closed) {
|
|
|
|
|
throw new IllegalStateException("AstraPacks is closed");
|
|
|
|
|
}
|
|
|
|
|
return host.mountWebContext(prefix, handler);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String publicUrl(String path) {
|
|
|
|
|
return host.publicUrl(config.hosting.publicAddress, path);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-06 18:36:18 +03:00
|
|
|
public void build(CommandSourceStack source) {
|
2026-09-24 19:28:22 +03:00
|
|
|
buildAsync(source);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Completes after the archive becomes the server's current pack, not after client acceptance. */
|
|
|
|
|
public CompletableFuture<String> buildAsync(CommandSourceStack source) {
|
|
|
|
|
return buildAsync(source, () -> () -> {});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Reserves the build before changing shared inputs; the receipt restores them on failure. */
|
|
|
|
|
public CompletableFuture<String> buildAsync(CommandSourceStack source, Callable<AutoCloseable> prepareInputs) {
|
|
|
|
|
return requestBuild(false, message -> source.sendSuccess(() -> Component.literal(message), false),
|
|
|
|
|
message -> source.sendFailure(Component.literal(message)), prepareInputs);
|
2026-09-06 18:36:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void reload(CommandSourceStack source) {
|
2026-09-24 19:28:22 +03:00
|
|
|
requestBuild(true, message -> source.sendSuccess(() -> Component.literal(message), false),
|
|
|
|
|
message -> source.sendFailure(Component.literal(message)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
synchronized CompletableFuture<String> requestBuild(boolean reload, Consumer<String> success, Consumer<String> failure) {
|
|
|
|
|
return requestBuild(reload, success, failure, () -> () -> {});
|
2026-09-06 18:36:18 +03:00
|
|
|
}
|
|
|
|
|
|
2026-09-24 19:28:22 +03:00
|
|
|
synchronized CompletableFuture<String> requestBuild(boolean reload, Consumer<String> success,
|
|
|
|
|
Consumer<String> failure, Callable<AutoCloseable> prepareInputs) {
|
2026-09-06 18:28:23 +03:00
|
|
|
if (closed) {
|
2026-09-24 19:28:22 +03:00
|
|
|
return rejected("AstraPacks остановлен.", failure);
|
2026-09-06 18:28:23 +03:00
|
|
|
}
|
2026-09-24 19:28:22 +03:00
|
|
|
if (!config.enabled) return rejected("AstraPacks отключён: публикация ресурспака недоступна.", failure);
|
2026-09-06 18:36:18 +03:00
|
|
|
if (building) {
|
2026-09-24 19:28:22 +03:00
|
|
|
return rejected("Сборка AstraPacks уже выполняется.", failure);
|
2026-09-06 18:28:23 +03:00
|
|
|
}
|
2026-09-06 18:36:18 +03:00
|
|
|
|
2026-09-24 19:28:22 +03:00
|
|
|
var inputs = new BuildInputs(prepareInputs);
|
2026-09-06 18:36:18 +03:00
|
|
|
building = true;
|
2026-09-24 19:28:22 +03:00
|
|
|
var result = new CompletableFuture<String>();
|
|
|
|
|
activeBuild = result;
|
|
|
|
|
activeInputs = inputs;
|
|
|
|
|
try {
|
|
|
|
|
success.accept("AstraPacks: проверка и подготовка ресурспака…");
|
|
|
|
|
worker.execute(() -> preparePack(result, inputs, reload, success, failure));
|
|
|
|
|
} catch (RuntimeException error) {
|
|
|
|
|
failBuild(result, error, failure);
|
|
|
|
|
}
|
|
|
|
|
return result.copy();
|
2026-09-06 18:28:23 +03:00
|
|
|
}
|
|
|
|
|
|
2026-09-24 19:28:22 +03:00
|
|
|
private static CompletableFuture<String> rejected(String message, Consumer<String> failure) {
|
|
|
|
|
failure.accept(message);
|
|
|
|
|
return CompletableFuture.failedFuture(new IllegalStateException(message));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void preparePack(CompletableFuture<String> result, BuildInputs inputs, boolean reload,
|
|
|
|
|
Consumer<String> success, Consumer<String> failure) {
|
|
|
|
|
PackBuilder.Prepared prepared = null;
|
2026-09-06 18:28:23 +03:00
|
|
|
try {
|
2026-09-24 19:28:22 +03:00
|
|
|
if (closed || result.isDone()) return;
|
2026-09-06 18:28:23 +03:00
|
|
|
PackConfig next = reload ? PackConfig.load(configPath) : config;
|
2026-09-13 21:55:44 +03:00
|
|
|
if (next.enabled != config.enabled
|
|
|
|
|
|| !next.hosting.sameSettings(config.hosting)
|
2026-09-06 19:20:55 +03:00
|
|
|
|| !next.build.storageDirectory.equals(config.build.storageDirectory)) {
|
2026-09-13 21:55:44 +03:00
|
|
|
throw new IOException(
|
|
|
|
|
"Изменение enabled, каталога хранения или настроек HTTP-сервера требует"
|
|
|
|
|
+ " перезапуска.");
|
2026-09-06 18:28:23 +03:00
|
|
|
}
|
2026-09-24 19:28:22 +03:00
|
|
|
inputs.prepare();
|
|
|
|
|
prepared = new PackBuilder(next).prepare(root, RESOURCE_VERSION);
|
|
|
|
|
var candidate = prepared;
|
|
|
|
|
callbacks.execute(() -> publishPack(result, next, candidate, success, failure));
|
2026-09-06 18:28:23 +03:00
|
|
|
} catch (Exception e) {
|
2026-09-24 19:28:22 +03:00
|
|
|
discard(prepared, e);
|
|
|
|
|
Exception error = rollbackInputs(inputs, e);
|
|
|
|
|
AstraPacks.LOG.error("Pack build/publication failed; active pack retained", error);
|
|
|
|
|
try { callbacks.execute(() -> failBuild(result, error, failure)); }
|
|
|
|
|
catch (RuntimeException unavailable) {
|
|
|
|
|
error.addSuppressed(unavailable);
|
|
|
|
|
failBuild(result, error, ignored -> {});
|
|
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-24 19:28:22 +03:00
|
|
|
private synchronized void failBuild(CompletableFuture<String> result, Exception error, Consumer<String> failure) {
|
|
|
|
|
if (activeBuild != result) return;
|
|
|
|
|
error = rollbackInputs(activeInputs, error);
|
2026-09-13 22:17:59 +03:00
|
|
|
building = false;
|
2026-09-24 19:28:22 +03:00
|
|
|
activeBuild = null;
|
|
|
|
|
activeInputs = null;
|
|
|
|
|
result.completeExceptionally(error);
|
2026-09-13 22:17:59 +03:00
|
|
|
if (closed) return;
|
|
|
|
|
String message = "AstraPacks: " + error.getMessage() + " Рабочий пак сохранён.";
|
2026-09-24 19:28:22 +03:00
|
|
|
failure.accept(message);
|
2026-09-13 22:17:59 +03:00
|
|
|
}
|
|
|
|
|
|
2026-09-24 19:28:22 +03:00
|
|
|
private synchronized void publishPack(CompletableFuture<String> result, PackConfig next, PackBuilder.Prepared prepared,
|
|
|
|
|
Consumer<String> success, Consumer<String> failure) {
|
|
|
|
|
if (closed || activeBuild != result) {
|
|
|
|
|
discard(prepared, null);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var pack = prepared.pack();
|
|
|
|
|
try {
|
|
|
|
|
host.publish(pack);
|
|
|
|
|
new PackBuilder(next).publish(root, prepared);
|
|
|
|
|
activeInputs.commit();
|
|
|
|
|
config = next;
|
|
|
|
|
current = pack;
|
|
|
|
|
} catch (IOException | RuntimeException error) {
|
|
|
|
|
discard(prepared, error);
|
|
|
|
|
failBuild(result, error, failure);
|
2026-09-06 18:28:23 +03:00
|
|
|
return;
|
|
|
|
|
}
|
2026-09-13 21:55:44 +03:00
|
|
|
String message =
|
|
|
|
|
"AstraPacks: готово, "
|
|
|
|
|
+ pack.files()
|
|
|
|
|
+ " файлов, "
|
|
|
|
|
+ pack.bytes()
|
|
|
|
|
+ " байт. SHA-1: "
|
|
|
|
|
+ pack.sha1();
|
2026-09-24 19:28:22 +03:00
|
|
|
building = false;
|
|
|
|
|
activeBuild = null;
|
|
|
|
|
activeInputs = null;
|
|
|
|
|
result.complete(message + ". Пак опубликован; загрузка игроками проверяется отдельно.");
|
|
|
|
|
try { success.accept(message); }
|
|
|
|
|
catch (RuntimeException notification) { AstraPacks.LOG.warn("Pack published, but build notification failed", notification); }
|
2026-09-06 18:28:23 +03:00
|
|
|
if (config.enabled && config.build.resendAfterBuild) {
|
2026-09-24 19:28:22 +03:00
|
|
|
try {
|
|
|
|
|
for (ServerPlayer player : server.getPlayerList().getPlayers()) {
|
|
|
|
|
try { session(player.connection).start(player.connection::send); }
|
|
|
|
|
catch (RuntimeException delivery) { AstraPacks.LOG.warn("Pack published, but delivery failed for {}", player.getUUID(), delivery); }
|
|
|
|
|
}
|
|
|
|
|
} catch (RuntimeException delivery) { AstraPacks.LOG.warn("Pack published, but starting delivery failed", delivery); }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void discard(PackBuilder.Prepared prepared, Exception failure) {
|
|
|
|
|
try { PackBuilder.discard(prepared); }
|
|
|
|
|
catch (IOException cleanup) {
|
|
|
|
|
if (failure != null) failure.addSuppressed(cleanup);
|
|
|
|
|
else AstraPacks.LOG.warn("Cannot remove unpublished pack candidate", cleanup);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Exception rollbackInputs(BuildInputs inputs, Exception failure) {
|
|
|
|
|
if (inputs == null) return failure;
|
|
|
|
|
try { inputs.rollback(); return failure; }
|
|
|
|
|
catch (Exception rollback) {
|
|
|
|
|
var combined = new IOException(failure.getMessage() + "; не удалось восстановить исходные ресурсы: " + rollback.getMessage(), failure);
|
|
|
|
|
combined.addSuppressed(rollback);
|
|
|
|
|
return combined;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Close waits for an in-progress export before restoring its files; publication commits once. */
|
|
|
|
|
private static final class BuildInputs {
|
|
|
|
|
private final Callable<AutoCloseable> prepare;
|
|
|
|
|
private AutoCloseable receipt;
|
|
|
|
|
private boolean finished;
|
|
|
|
|
|
|
|
|
|
BuildInputs(Callable<AutoCloseable> prepare) { this.prepare = java.util.Objects.requireNonNull(prepare); }
|
|
|
|
|
synchronized void prepare() throws Exception {
|
|
|
|
|
if (finished) throw new IllegalStateException("Resource input preparation was stopped");
|
|
|
|
|
receipt = java.util.Objects.requireNonNull(prepare.call(), "Input preparation must return a rollback receipt");
|
|
|
|
|
}
|
|
|
|
|
synchronized void commit() { finished = true; receipt = null; }
|
|
|
|
|
synchronized void rollback() throws Exception {
|
|
|
|
|
if (finished) return;
|
|
|
|
|
finished = true;
|
|
|
|
|
if (receipt != null) receipt.close();
|
|
|
|
|
receipt = null;
|
2026-09-06 18:28:23 +03:00
|
|
|
}
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
|
2026-09-06 18:22:00 +03:00
|
|
|
public String status() {
|
2026-09-13 21:55:44 +03:00
|
|
|
String pack =
|
|
|
|
|
current == null
|
|
|
|
|
? "UNAVAILABLE"
|
|
|
|
|
: current.path() + " (" + current.files() + " files)";
|
|
|
|
|
String address =
|
|
|
|
|
config.hosting.publicAddress.isBlank() ? "NOT SET" : config.hosting.publicAddress;
|
|
|
|
|
return "AstraPacks HTTP:"
|
|
|
|
|
+ config.hosting.port
|
|
|
|
|
+ ", address="
|
|
|
|
|
+ address
|
|
|
|
|
+ ", required="
|
|
|
|
|
+ config.delivery.required
|
|
|
|
|
+ ", build="
|
|
|
|
|
+ building
|
|
|
|
|
+ ", pack="
|
|
|
|
|
+ pack;
|
2026-09-06 18:28:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2026-09-24 19:28:22 +03:00
|
|
|
public synchronized void close() {
|
2026-09-06 18:28:23 +03:00
|
|
|
closed = true;
|
2026-09-24 19:28:22 +03:00
|
|
|
building = false;
|
|
|
|
|
if (activeBuild != null) {
|
|
|
|
|
var failure = rollbackInputs(activeInputs, new IllegalStateException("AstraPacks остановлен до завершения публикации."));
|
|
|
|
|
activeBuild.completeExceptionally(failure);
|
|
|
|
|
activeBuild = null;
|
|
|
|
|
activeInputs = null;
|
|
|
|
|
}
|
2026-09-06 18:28:23 +03:00
|
|
|
worker.shutdownNow();
|
|
|
|
|
host.close();
|
|
|
|
|
sessions.clear();
|
2026-09-06 18:36:18 +03:00
|
|
|
pendingWelcome.clear();
|
2026-09-06 18:22:00 +03:00
|
|
|
}
|
|
|
|
|
}
|