Improved some behavior and logging.

This commit is contained in:
Andrew Lalis 2021-06-30 13:19:32 +02:00
parent b72a5a8b7a
commit a9e032119c
4 changed files with 13 additions and 5 deletions

View File

@ -2,6 +2,7 @@ package nl.andrewlalis.aos_server_registry.data;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Logger;
/**
* Scheduled task that runs once in a while and removes servers from the
@ -9,6 +10,8 @@ import java.sql.SQLException;
*/
public class ServerDataPruner implements Runnable {
public static final int INTERVAL_MINUTES = 5;
private static final Logger log = Logger.getLogger(ServerDataPruner.class.getName());
@Override
public void run() {
try {
@ -22,7 +25,7 @@ public class ServerDataPruner implements Runnable {
int rowCount = stmt.executeUpdate();
stmt.close();
if (rowCount > 0) {
System.out.println("Removed " + rowCount + " servers from registry due to inactivity.");
log.info("Removed " + rowCount + " servers from registry due to inactivity.");
}
} catch (SQLException e) {
e.printStackTrace();

View File

@ -18,8 +18,11 @@ import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
public class ServerInfoServlet extends HttpServlet {
private static final Logger log = Logger.getLogger(ServerInfoServlet.class.getName());
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
int page = Requests.getIntParam(req, "page", 0, i -> i >= 0);
@ -120,7 +123,7 @@ public class ServerInfoServlet extends HttpServlet {
int rowCount = createStmt.executeUpdate();
createStmt.close();
if (rowCount != 1) throw new SQLException("Could not insert new server.");
System.out.println("Registered new server " + info.name() + " @ " + info.address());
log.info("Registered new server " + info.name() + " @ " + info.address());
} else {
PreparedStatement updateStmt = con.prepareStatement("""
UPDATE servers SET description = ?, location = ?, max_players = ?, current_players = ?
@ -135,7 +138,7 @@ public class ServerInfoServlet extends HttpServlet {
int rowCount = updateStmt.executeUpdate();
updateStmt.close();
if (rowCount != 1) throw new SQLException("Could not update server.");
System.out.println("Updated server information for " + info.name() + " @ " + info.address());
log.info("Updated server information for " + info.name() + " @ " + info.address());
}
}
@ -155,6 +158,7 @@ public class ServerInfoServlet extends HttpServlet {
Responses.notFound(resp);
} else {
Responses.ok(resp);
log.info("Status updated for " + status.name() + " @ " + status.address());
}
} catch (SQLException e) {
e.printStackTrace();

View File

@ -73,8 +73,8 @@ public class RegistryManager {
.header("Content-Type", "application/json")
.build();
this.httpClient.sendAsync(request, responseInfo -> {
if (responseInfo.statusCode() == 404) {
System.out.println("Received 404 when sending registry update. Re-sending registry info...");
if (responseInfo.statusCode() != 200) {
System.out.println("Received non-OK status when sending registry update. Re-sending registry info...");
this.sendInfo();
}
return null;

View File

@ -14,3 +14,4 @@ help Shows this help message.
list Show a list of all connected players.
kick <p> Kick a player with the given id or name. If more than one player
exists with a given name, you need to use their unique id.
guns Lists all available guns that players may use in this server.