Skip to content

Commit

Permalink
shutdown server gracefully first
Browse files Browse the repository at this point in the history
related to #16
  • Loading branch information
burdoto committed Aug 26, 2023
1 parent 10c1775 commit a990f73
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/agent/main/java/org/comroid/mcsd/agent/AgentRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,12 @@ public String shutdown(ConsoleController.Connection con) {
.map(this::process)
.anyMatch(srv -> !srv.getCurrentBackup().get().isDone() || srv.getUpdateRunning().get()))
throw new Command.MildError("Unable to shutdown while a backup or update is running");
CompletableFuture.allOf(Streams.of(servers.findAll())
.map(this::process)
.filter(proc -> proc.getState() == ServerProcess.State.Running)
.map(proc -> proc.shutdown("Host shutdown", 10))
.toArray(CompletableFuture[]::new))
.join();
System.exit(0);
return "shutting down";
}
Expand Down
3 changes: 3 additions & 0 deletions src/agent/main/java/org/comroid/mcsd/agent/ServerProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ public boolean runUpdate(String... args) {
@SneakyThrows
public CompletableFuture<?> shutdown(final String reason, final int warnSeconds) {
return CompletableFuture.supplyAsync(() -> {
pushStatus(Status.Shutting_Down.new Message(reason));
final var msg = (IntFunction<String>) t -> "say Server will shut down in %d seconds (%s)".formatted(t, reason);
int time = warnSeconds;

Expand Down Expand Up @@ -324,6 +325,8 @@ public void closeSelf() {
if (process == null || getState() != State.Running)
return;

pushStatus(Status.Offline);

// try shut down gracefully
in.println("stop");
process.onExit().orTimeout(30, TimeUnit.SECONDS).join();
Expand Down
17 changes: 9 additions & 8 deletions src/api/main/java/org/comroid/mcsd/api/model/Status.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@

@Getter
public enum Status implements IntegerAttribute, IStatusMessage {
Unknown ("❔", McFormatCode.Dark_Gray),
Offline ("❌", McFormatCode.Red),
Starting ("⏯️", McFormatCode.Aqua),
Maintenance ("\uD83D\uDD27", McFormatCode.Yellow),
Backing_Up ("\uD83D\uDCBE", McFormatCode.Green),
Updating ("\uD83D\uDD04️", McFormatCode.Light_Purple),
In_Trouble ("⚠️", McFormatCode.Gold),
Online ("✅", McFormatCode.Dark_Green);
Unknown ("❔", McFormatCode.Dark_Gray),
Offline ("❌", McFormatCode.Dark_Red),
Starting ("⏯️", McFormatCode.Aqua),
Maintenance ("\uD83D\uDD27", McFormatCode.Yellow),
Running_Backup ("\uD83D\uDCBE", McFormatCode.Green),
Updating ("\uD83D\uDD04️", McFormatCode.Light_Purple),
In_Trouble ("⚠️", McFormatCode.Gold),
Online ("✅", McFormatCode.Dark_Green),
Shutting_Down ("\uD83D\uDED1", McFormatCode.Red);

private final String emoji;
private final Color color;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public ConnectionHandler(Socket socket) {
.setEndlMode(DelegateStream.EndlMode.OnNewLine)
.subscribe(str -> {
var packet = parsePacket(str).setReceived(true);
publish(packet.getTopic(), packet, (long)packet.getOpCode().getAsInt());
publish(packet.getTopic(), (long)packet.getOpCode().getAsInt(), packet);
}).activate(executor),
// Tx
listen().setPredicate(e -> !Objects.requireNonNull(e.getData()).isReceived())
Expand Down

0 comments on commit a990f73

Please sign in to comment.