Console and caretaker

com.console.Console, com.util.CareTaker

The console

While JBend runs in a terminal you can type commands into it. help lists them.

CommandDoes
statusApplication and JBend versions, uptime, every server with its state and connection counts, the database, live sessions.
threadsThe launcher's threads, in start order.
connections [port|name]Lists open connections: id, address, user, how long connected, bytes in and out. Without an argument, every server.
kick <port|name> <id|all>Closes one connection, or all of a server's. kick 8081 3, kick websocket all.
ping [port|name]Sends pingfromserver to TCP and WebSocket clients and drops those that do not answer pong::iamalive within 5 seconds. Only for clients written to answer it.
ban user|ip <value>, unban ..., bansManage bans. See Accounts.
mysql info | reconnect | <sql>Database details, reconnect, or run a statement and print its rows.
routesThe web server's routes.
debug <0|1|2>Change the logging level while running.
stopCloses every connection and exits.

Server names are the protocol names shown by status: http, websocket, tcp, bridge, proxy.

Commands of your own

Console.register("players", "", "List the players online", args -> {
    for (Player p : game.players())
        Console.output("Game", p.getName());
});

Console.register("announce", "<text>", "Send a message to everyone", args -> {
    String text = String.join(" ", java.util.Arrays.copyOfRange(args, 1, args.length));
    Main.launcher.getWebsocketServer().broadcast("notice:" + text);
});

The arguments are the command name, the usage text shown by help, a description, and the code. args[0] is the command name. Commands run on the console thread. Console.processCommand("status") runs a command from code.

Logging

Console.output("Game", "Round started");      // prints: [Game] Round started

Everything JBend logs goes to standard output in this form, so under systemd it lands in the journal and under nohup or a redirect it lands in the file.

Without a terminal

Started by systemd, or with input redirected from /dev/null, there is nothing to read and the console thread simply ends; the servers keep running. console: with a line off in the config skips it entirely.

The caretaker

caretaker:
interval 30m

A background thread for servers that stay up for weeks. Each interval it runs a trivial query, which keeps the database connection from idling out and reopens it if it already has, and logs one line:

[CareTaker] up 3d 4h 12m 9s, 14 connections, 212 MB heap, 48210 queries (0 failed)

/api/status

The same information as status, as JSON over HTTP, for a status page or a monitor. Add values of your own:

Main.launcher.putStatus("playersOnline", () -> String.valueOf(game.playerCount()));
{"app":"jbend-demo","appVersion":"1.0.0","jbend":"2.0.0","java":"21.0.5","uptimeSeconds":86407,
 "servers":[{"protocol":"HTTP","port":8090,"running":true,"connections":2,"accepted":5310}, ...],
 "database":{"connected":true,"queries":48210,"failed":0},"sessions":12,"playersOnline":"3"}