.
This commit is contained in:
parent
aa6c8a57e3
commit
cc402a71e7
4 changed files with 33 additions and 29 deletions
|
|
@ -2,7 +2,7 @@ TODO
|
||||||
- timeout/error handling with reactive
|
- timeout/error handling with reactive
|
||||||
- unregister server on shutdown
|
- unregister server on shutdown
|
||||||
- infrastructure scripts/glue code w/ daemontools
|
- infrastructure scripts/glue code w/ daemontools
|
||||||
- djb's multilogd for logging
|
- djb's multilogd for logging(?)
|
||||||
|
|
||||||
environment
|
environment
|
||||||
-----------
|
-----------
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
package de.kentoj.scrowlib.servermanager;
|
package de.kentoj.scrowlib.servermanager;
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
import com.google.common.io.Files;
|
|
||||||
import de.kentoj.scrowlib.messaging.DocumentRepository;
|
import de.kentoj.scrowlib.messaging.DocumentRepository;
|
||||||
import de.kentoj.scrowlib.messaging.NatsRepository;
|
import de.kentoj.scrowlib.messaging.NatsRepository;
|
||||||
import io.nats.client.Connection;
|
import io.nats.client.Connection;
|
||||||
|
|
@ -10,8 +9,6 @@ import org.jetbrains.annotations.NotNull;
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
import javax.print.Doc;
|
|
||||||
|
|
||||||
public class CrownServerManager implements ServerManager {
|
public class CrownServerManager implements ServerManager {
|
||||||
|
|
||||||
private final NatsRepository natsRepo;
|
private final NatsRepository natsRepo;
|
||||||
|
|
@ -55,19 +52,15 @@ public class CrownServerManager implements ServerManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Mono<@NotNull String> getState(String handle) {
|
public Mono<@NotNull ServerInstance> getInfo(String handle) {
|
||||||
var payload = DocumentRepository.toBytes(new Document("handle", handle));
|
var payload = DocumentRepository.toBytes(new Document("handle", handle));
|
||||||
return natsRepo.request("crown.get-state", payload)
|
return natsRepo.request("crown.get-info", payload)
|
||||||
.map(DocumentRepository::fromMessage)
|
.map(DocumentRepository::fromMessage)
|
||||||
.map(doc -> doc.getString("state"));
|
.map(doc -> new ServerInstance(
|
||||||
}
|
handle,
|
||||||
|
doc.getInteger("port"),
|
||||||
@Override
|
doc.getString("state")
|
||||||
public Mono<@NotNull Integer> getPort(String handle) {
|
));
|
||||||
var payload = DocumentRepository.toBytes(new Document("handle", handle));
|
|
||||||
return natsRepo.request("crown.get-port", payload)
|
|
||||||
.map(DocumentRepository::fromMessage)
|
|
||||||
.map(doc -> doc.getInteger("port"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -11,19 +11,17 @@ public interface ServerManager {
|
||||||
*/
|
*/
|
||||||
Mono<@NotNull ServerInstance> deployServer(String template);
|
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.
|
* Attempts a clean shutdown, if that doesn't succeed, kills the server.
|
||||||
*/
|
*/
|
||||||
Mono<@NotNull Void> destroyServer(String handle);
|
Mono<@NotNull Void> destroyServer(String handle);
|
||||||
|
|
||||||
Mono<@NotNull String> getState(String handle);
|
/**
|
||||||
|
* @return Flux with handles of running servers
|
||||||
|
*/
|
||||||
|
Flux<@NotNull ServerInstance> getRunningServers();
|
||||||
|
|
||||||
Mono<@NotNull Integer> getPort(String handle);
|
Mono<@NotNull ServerInstance> getInfo(String handle);
|
||||||
|
|
||||||
Mono<@NotNull Void> setState(String handle, String state);
|
Mono<@NotNull Void> setState(String handle, String state);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import de.kentoj.scrowlib.messaging.NatsRepository;
|
||||||
import de.kentoj.scrowlib.servermanager.ServerManager;
|
import de.kentoj.scrowlib.servermanager.ServerManager;
|
||||||
import io.nats.client.Connection;
|
import io.nats.client.Connection;
|
||||||
import lombok.extern.java.Log;
|
import lombok.extern.java.Log;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
|
@ -29,15 +30,27 @@ public class ServerRegisterWatchdog {
|
||||||
.map(DocumentRepository::fromMessage)
|
.map(DocumentRepository::fromMessage)
|
||||||
.flatMap(doc -> {
|
.flatMap(doc -> {
|
||||||
var state = doc.getString("state");
|
var state = doc.getString("state");
|
||||||
if (!"UP".equals(state)) return Mono.empty();
|
|
||||||
|
|
||||||
var handle = doc.getString("handle");
|
var handle = doc.getString("handle");
|
||||||
return serverManager.getPort(handle)
|
return handleStateChange(state, handle);
|
||||||
.map(port -> {
|
|
||||||
log.info("registering " + handle);
|
|
||||||
return server.registerServer(new ServerInfo(handle, new InetSocketAddress(port)));
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
.subscribe();
|
.subscribe();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private @NotNull Mono<@NotNull Void> handleStateChange(String state, String handle) {
|
||||||
|
return switch (state) {
|
||||||
|
case "UP" -> serverManager.getInfo(handle)
|
||||||
|
.doOnNext(instance -> {
|
||||||
|
log.info("registering " + handle);
|
||||||
|
server.registerServer(new ServerInfo(handle, new InetSocketAddress(instance.getPort())));
|
||||||
|
})
|
||||||
|
.then();
|
||||||
|
case "DOWN" -> serverManager.getInfo(handle)
|
||||||
|
.doOnNext(instance -> {
|
||||||
|
log.info("unregistering " + handle);
|
||||||
|
server.unregisterServer(new ServerInfo(handle, new InetSocketAddress(instance.getPort())));
|
||||||
|
})
|
||||||
|
.then();
|
||||||
|
default -> Mono.empty();
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue