This commit is contained in:
kento2 2026-05-31 23:06:21 +02:00
parent d3e20606d9
commit 5a255aeac0
16 changed files with 128 additions and 74 deletions

View file

@ -1,4 +1,4 @@
package de.kentoj.scrowlib;
package de.kentoj.scrowlib.messaging;
import io.nats.client.Message;
import lombok.AccessLevel;

View file

@ -1,4 +1,4 @@
package de.kentoj.scrowlib;
package de.kentoj.scrowlib.messaging;
import io.nats.client.Connection;
import io.nats.client.Dispatcher;

View file

@ -0,0 +1,72 @@
package de.kentoj.scrowlib.servermanager;
import com.google.common.base.Preconditions;
import com.google.common.io.Files;
import de.kentoj.scrowlib.messaging.DocumentRepository;
import de.kentoj.scrowlib.messaging.NatsRepository;
import io.nats.client.Connection;
import org.bson.Document;
import org.jetbrains.annotations.NotNull;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
public class CrownServerManager implements ServerManager {
private final NatsRepository natsRepo;
public CrownServerManager(Connection nats) {
this.natsRepo = new NatsRepository(nats);
}
@Override
public Mono<@NotNull ServerInstance> deployServer(String template) {
var payload = DocumentRepository.toBytes(new Document("template", template));
return natsRepo.request("crown.deploy-server", payload)
.map(DocumentRepository::fromMessage)
.map(doc -> new ServerInstance(
doc.getString("handle"),
doc.getInteger("port")
));
}
@Override
public Flux<@NotNull ServerInstance> getRunningServers() {
var payload = DocumentRepository.toBytes(new Document());
return natsRepo.request("crown.list-servers", payload)
.map(DocumentRepository::fromMessage)
.map(doc ->
doc.getList("instances", Document.class)
)
.flatMapIterable(l -> l)
.map(doc -> new ServerInstance(
doc.getString("handle"),
doc.getInteger("port"),
doc.getString("state")
));
}
@Override
public Mono<@NotNull Void> destroyServer(String handle) {
var payload = DocumentRepository.toBytes(new Document("handle", handle));
return natsRepo.request("crown.destroy-server", payload)
.then();
}
@Override
public Mono<@NotNull String> getState(String handle) {
var payload = DocumentRepository.toBytes(new Document("handle", handle));
return natsRepo.request("crown.get-state", payload)
.map(DocumentRepository::fromMessage)
.map(doc -> doc.getString("state"));
}
@Override
public Mono<@NotNull Void> setState(String handle, String state) {
Preconditions.checkArgument(!state.contains(" "), "state may not contain spaces");
var payload = DocumentRepository.toBytes(new Document()
.append("handle", handle)
.append("state", state));
return natsRepo.request("crown.set-state", payload)
.then();
}
}

View file

@ -0,0 +1,16 @@
package de.kentoj.scrowlib.servermanager;
import lombok.AllArgsConstructor;
import lombok.Value;
@Value
@AllArgsConstructor
public class ServerInstance {
String handle;
int port;
String state;
public ServerInstance(String handle, int port) {
this(handle, port, "STARTING");
}
}

View file

@ -0,0 +1,27 @@
package de.kentoj.scrowlib.servermanager;
import org.jetbrains.annotations.NotNull;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
public interface ServerManager {
/**
* @return Mono with handle of deployed server
*/
Mono<@NotNull ServerInstance> deployServer(String template);
/**
* @return Flux with handles of running servers
*/
Flux<@NotNull ServerInstance> getRunningServers();
/**
* Attempts a clean shutdown, if that doesn't succeed, kills the server.
*/
Mono<@NotNull Void> destroyServer(String handle);
Mono<@NotNull String> getState(String handle);
Mono<@NotNull Void> setState(String handle, String state);
}