Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Record uptime statistics #13

Merged
merged 4 commits into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public Event.Listener<GatewayPacket> gatewayListener(@Autowired GatewayClient ga
public Map<Runnable, Duration> cronjobs() {
return Map.of(
this::$cronWatchdog, MinecraftServerHubConfig.CronRate_Watchdog,
this::$cronUptime, MinecraftServerHubConfig.CronRate_Uptime,
this::$cronBackup, MinecraftServerHubConfig.CronRate_Queue,
this::$cronUpdate, MinecraftServerHubConfig.CronRate_Queue
);
Expand Down Expand Up @@ -134,6 +135,16 @@ public Map<Runnable, Duration> startCronjobs(@Autowired TaskScheduler scheduler,
cronLog.log(Level.FINER, "Watchdog finished");
}

@Synchronized
private void $cronUptime() {
cronLog.log(Level.FINEST, "Running Uptime");
agentRunner.streamServers()
.filter(Server::isEnabled)
.map(agentRunner::process)
.forEach(ServerProcess::pushUptime);
cronLog.log(Level.FINER, "Uptime finished");
}

@SneakyThrows
@Synchronized
private void $cronBackup() {
Expand Down
17 changes: 17 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 @@ -11,9 +11,12 @@
import org.comroid.mcsd.api.model.IStatusMessage;
import org.comroid.mcsd.api.model.Status;
import org.comroid.mcsd.core.entity.Server;
import org.comroid.mcsd.core.entity.ServerUptimeEntry;
import org.comroid.mcsd.core.repo.ServerRepo;
import org.comroid.mcsd.core.repo.ServerUptimeRepo;
import org.comroid.util.*;
import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;

import java.io.*;
Expand Down Expand Up @@ -68,6 +71,20 @@ public void pushStatus(IStatusMessage message) {
bean(Event.Bus.class, "eventBus").publish(server.getId().toString(), message);
}

public void pushUptime() {
server.status()
.thenCombine(getState() == State.Running
? OS.current.getRamUsage(process.pid())
: CompletableFuture.completedFuture(0L),
(stat, ram) -> new ServerUptimeEntry(server,
currentStatus.getStatus(),
stat.getPlayers() != null ? stat.getPlayers().size() : stat.getPlayerCount(),
ram,
currentStatus.getMessage()))
.thenAccept(bean(ServerUptimeRepo.class)::save)
.join();
}

public boolean pushMaintenance(boolean val) { //todo
var is = server.isMaintenance();
runner.getServers().setMaintenance(server.getId(), val);
Expand Down
6 changes: 3 additions & 3 deletions src/api/main/java/org/comroid/mcsd/api/dto/StatusMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
public class StatusMessage {
private final Instant timestamp = Instant.now();
private final @NotNull UUID targetId;
private @With @lombok.Builder.Default Status status = Status.Offline;
private @With @lombok.Builder.Default @Nullable Status rcon = Status.Offline;
private @With @lombok.Builder.Default @Nullable Status ssh = Status.Offline;
private @With @lombok.Builder.Default Status status = Status.Unknown;
private @With @lombok.Builder.Default @Nullable Status rcon = Status.Unknown;
private @With @lombok.Builder.Default @Nullable Status ssh = Status.Unknown;
private @With @lombok.Builder.Default int playerCount = 0;
private @With @lombok.Builder.Default int playerMax = 0;
private @With @Nullable String motd;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
@EnableJpaRepositories(basePackages = "org.comroid.mcsd.core.repo")
public class MinecraftServerHubConfig {
public static final Duration CronRate_Watchdog = Duration.ofSeconds(10);
public static final Duration CronRate_Uptime = Duration.ofMinutes(1);
public static final Duration CronRate_Queue = Duration.ofHours(1);
public static final Logger cronLog = Logger.getLogger("cron");

Expand Down
5 changes: 3 additions & 2 deletions src/core/main/java/org/comroid/mcsd/core/entity/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ public CompletableFuture<StatusMessage> status() {
return null;
return v;
}), "Status cache outdated"))
.exceptionally(t -> {
.exceptionally(t ->
{
log.debug("Unable to get server status from cache ["+t.getMessage()+"], using Query...");
log.trace("Exception was", t);
try (var query = new MCQuery(host, getQueryPort())) {
Expand Down Expand Up @@ -278,7 +279,7 @@ public CompletableFuture<StatusMessage> status() {
statusCache.put(getId(), msg);
return msg;
})
.orTimeout(statusTimeout.toSeconds() + 1, TimeUnit.SECONDS);
.completeOnTimeout(new StatusMessage(getId()), (long) (statusTimeout.toSeconds() * 1.5), TimeUnit.SECONDS);
}

public Optional<ShConnection> shCon() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.comroid.mcsd.core.entity;

import jakarta.persistence.*;
import lombok.*;
import org.comroid.mcsd.api.model.Status;
import org.jetbrains.annotations.Nullable;

import java.time.Instant;

@Data
@Entity
@NoArgsConstructor
public class ServerUptimeEntry {
private static final Object lock = new Object();

private @Id Instant timestamp;
private @ManyToOne Server server;
private Status status;
private int players;
private long ramKB;
private @Nullable String message;

public ServerUptimeEntry(Server server, Status status, int players, long ramKB, @Nullable String message) {
synchronized (lock) {
timestamp = Instant.now();
}
this.server = server;
this.status = status;
this.players = players;
this.ramKB = ramKB;
this.message = message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.comroid.mcsd.core.repo;

import org.comroid.mcsd.core.entity.ServerUptimeEntry;
import org.springframework.data.repository.CrudRepository;

import java.util.UUID;

public interface ServerUptimeRepo extends CrudRepository<ServerUptimeEntry, UUID> {
}