1
0
mirror of https://github.com/lucko/LuckPerms.git synced 2025-09-01 02:21:43 +02:00

Add healthcheck functionality to API

This commit is contained in:
Luck
2023-10-22 20:17:37 +01:00
parent 822c796e1c
commit 3707d2f03c
27 changed files with 321 additions and 267 deletions

View File

@@ -26,7 +26,6 @@
package me.lucko.luckperms.standalone.app;
import me.lucko.luckperms.standalone.app.integration.CommandExecutor;
import me.lucko.luckperms.standalone.app.integration.HealthReporter;
import me.lucko.luckperms.standalone.app.integration.ShutdownCallback;
import me.lucko.luckperms.standalone.app.utils.DockerCommandSocket;
import me.lucko.luckperms.standalone.app.utils.HeartbeatHttpServer;
@@ -54,8 +53,6 @@ public class LuckPermsApplication implements AutoCloseable {
private LuckPerms luckPermsApi;
/** A command executor interface to run LuckPerms commands */
private CommandExecutor commandExecutor;
/** An interface that can poll the health of the application */
private HealthReporter healthReporter;
/** If the application is running */
private final AtomicBoolean running = new AtomicBoolean(true);
@@ -78,7 +75,7 @@ public class LuckPermsApplication implements AutoCloseable {
List<String> arguments = Arrays.asList(args);
if (arguments.contains("--docker")) {
this.dockerCommandSocket = DockerCommandSocket.createAndStart("/opt/luckperms/luckperms.sock", terminal);
this.heartbeatHttpServer = HeartbeatHttpServer.createAndStart(3001, this.healthReporter);
this.heartbeatHttpServer = HeartbeatHttpServer.createAndStart(3001, () -> this.luckPermsApi.runHealthCheck());
}
terminal.start(); // blocking
@@ -123,11 +120,6 @@ public class LuckPermsApplication implements AutoCloseable {
this.commandExecutor = commandExecutor;
}
// called before start()
public void setHealthReporter(HealthReporter healthReporter) {
this.healthReporter = healthReporter;
}
public LuckPerms getApi() {
return this.luckPermsApi;
}
@@ -136,10 +128,6 @@ public class LuckPermsApplication implements AutoCloseable {
return this.commandExecutor;
}
public HealthReporter getHealthReporter() {
return this.healthReporter;
}
public String getVersion() {
return "@version@";
}

View File

@@ -1,77 +0,0 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.luckperms.standalone.app.integration;
import com.google.gson.Gson;
import java.util.Map;
/**
* An interface able to provide information about the application/plugin health.
*/
public interface HealthReporter {
/**
* Polls the current health status.
*
* @return the health status
*/
Health poll();
final class Health {
private static final Gson GSON = new Gson();
private final boolean up;
private final Map<String, String> details;
Health(boolean up, Map<String, String> details) {
this.up = up;
this.details = details;
}
public boolean isUp() {
return this.up;
}
public Map<String, String> details() {
return this.details;
}
@Override
public String toString() {
return GSON.toJson(this);
}
public static Health up(Map<String, String> details) {
return new Health(true, details);
}
public static Health down(Map<String, String> details) {
return new Health(false, details);
}
}
}

View File

@@ -29,7 +29,7 @@ import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import me.lucko.luckperms.standalone.app.integration.HealthReporter;
import net.luckperms.api.platform.Health;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -39,6 +39,7 @@ import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Supplier;
/**
* Provides a tiny http server indicating the current status of the app
@@ -52,7 +53,7 @@ public class HeartbeatHttpServer implements HttpHandler, AutoCloseable {
.build()
);
public static HeartbeatHttpServer createAndStart(int port, HealthReporter healthReporter) {
public static HeartbeatHttpServer createAndStart(int port, Supplier<Health> healthReporter) {
HeartbeatHttpServer socket = null;
try {
@@ -65,10 +66,10 @@ public class HeartbeatHttpServer implements HttpHandler, AutoCloseable {
return socket;
}
private final HealthReporter healthReporter;
private final Supplier<Health> healthReporter;
private final HttpServer server;
public HeartbeatHttpServer(HealthReporter healthReporter, int port) throws IOException {
public HeartbeatHttpServer(Supplier<Health> healthReporter, int port) throws IOException {
this.healthReporter = healthReporter;
this.server = HttpServer.create(new InetSocketAddress(port), 50);
this.server.createContext("/health", this);
@@ -78,10 +79,10 @@ public class HeartbeatHttpServer implements HttpHandler, AutoCloseable {
@Override
public void handle(HttpExchange exchange) throws IOException {
HealthReporter.Health health = this.healthReporter.poll();
Health health = this.healthReporter.get();
byte[] response = health.toString().getBytes(StandardCharsets.UTF_8);
exchange.sendResponseHeaders(health.isUp() ? 200 : 503, response.length);
exchange.sendResponseHeaders(health.isHealthy() ? 200 : 503, response.length);
try (OutputStream responseBody = exchange.getResponseBody()) {
responseBody.write(response);
}

View File

@@ -56,8 +56,6 @@ import java.util.stream.Stream;
public class LPStandalonePlugin extends AbstractLuckPermsPlugin {
private final LPStandaloneBootstrap bootstrap;
private boolean running = false;
private StandaloneSenderFactory senderFactory;
private StandaloneDummyConnectionListener connectionListener;
private StandaloneCommandManager commandManager;
@@ -79,10 +77,6 @@ public class LPStandalonePlugin extends AbstractLuckPermsPlugin {
return this.bootstrap.getLoader();
}
public boolean isRunning() {
return this.running;
}
@Override
protected void setupSenderFactory() {
this.senderFactory = new StandaloneSenderFactory(this);
@@ -149,17 +143,11 @@ public class LPStandalonePlugin extends AbstractLuckPermsPlugin {
@Override
protected void registerApiOnPlatform(LuckPerms api) {
this.bootstrap.getLoader().setApi(api);
this.bootstrap.getLoader().setHealthReporter(new StandaloneHealthReporter(this));
}
@Override
protected void performFinalSetup() {
this.running = true;
}
@Override
protected void removePlatformHooks() {
this.running = false;
}
@Override

View File

@@ -1,69 +0,0 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.luckperms.standalone;
import me.lucko.luckperms.common.locale.TranslationManager;
import me.lucko.luckperms.standalone.app.integration.HealthReporter;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import java.util.Collections;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;
public class StandaloneHealthReporter implements HealthReporter {
private final LPStandalonePlugin plugin;
public StandaloneHealthReporter(LPStandalonePlugin plugin) {
this.plugin = plugin;
}
@Override
public Health poll() {
if (!this.plugin.isRunning()) {
return Health.down(Collections.emptyMap());
}
Map<String, String> meta = this.plugin.getStorage().getMeta().entrySet().stream()
.collect(Collectors.toMap(
e -> render(e.getKey()).toLowerCase(Locale.ROOT),
e -> render(e.getValue())
));
if ("false".equals(meta.get("connected"))) {
return Health.down(Collections.singletonMap("reason", "storage disconnected"));
}
return Health.up(meta);
}
private static String render(Component component) {
return PlainTextComponentSerializer.plainText().serialize(
TranslationManager.render(component)
);
}
}

View File

@@ -29,10 +29,10 @@ import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.model.Group;
import me.lucko.luckperms.common.node.types.Permission;
import me.lucko.luckperms.standalone.app.integration.CommandExecutor;
import me.lucko.luckperms.standalone.app.integration.HealthReporter;
import me.lucko.luckperms.standalone.utils.TestPluginProvider;
import net.luckperms.api.model.data.DataType;
import net.luckperms.api.node.NodeEqualityPredicate;
import net.luckperms.api.platform.Health;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
@@ -52,9 +52,9 @@ public class IntegrationTest {
@Test
public void testLoadEnableDisable(@TempDir Path tempDir) {
TestPluginProvider.use(tempDir, (app, bootstrap, plugin) -> {
HealthReporter.Health health = app.getHealthReporter().poll();
Health health = plugin.runHealthCheck();
assertNotNull(health);
assertTrue(health.isUp());
assertTrue(health.isHealthy());
});
}

View File

@@ -28,12 +28,12 @@ package me.lucko.luckperms.standalone;
import com.google.common.collect.ImmutableMap;
import me.lucko.luckperms.common.actionlog.LoggedAction;
import me.lucko.luckperms.common.messaging.InternalMessagingService;
import me.lucko.luckperms.standalone.app.integration.HealthReporter;
import me.lucko.luckperms.standalone.utils.TestPluginProvider;
import net.luckperms.api.actionlog.Action;
import net.luckperms.api.event.EventBus;
import net.luckperms.api.event.log.LogReceiveEvent;
import net.luckperms.api.event.sync.PreNetworkSyncEvent;
import net.luckperms.api.platform.Health;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
@@ -61,13 +61,13 @@ public class MessagingIntegrationTest {
TestPluginProvider.Plugin pluginB = TestPluginProvider.create(tempDirB, config)) {
// check the plugins are healthy
HealthReporter.Health healthA = pluginA.app().getHealthReporter().poll();
Health healthA = pluginA.plugin().runHealthCheck();
assertNotNull(healthA);
assertTrue(healthA.isUp());
assertTrue(healthA.isHealthy());
HealthReporter.Health healthB = pluginB.app().getHealthReporter().poll();
Health healthB = pluginB.plugin().runHealthCheck();
assertNotNull(healthB);
assertTrue(healthB.isUp());
assertTrue(healthB.isHealthy());
InternalMessagingService messagingServiceA = pluginA.plugin().getMessagingService().orElse(null);
InternalMessagingService messagingServiceB = pluginB.plugin().getMessagingService().orElse(null);

View File

@@ -40,7 +40,6 @@ import me.lucko.luckperms.common.node.types.Permission;
import me.lucko.luckperms.common.node.types.Prefix;
import me.lucko.luckperms.common.storage.misc.NodeEntry;
import me.lucko.luckperms.standalone.app.LuckPermsApplication;
import me.lucko.luckperms.standalone.app.integration.HealthReporter;
import me.lucko.luckperms.standalone.utils.TestPluginBootstrap;
import me.lucko.luckperms.standalone.utils.TestPluginBootstrap.TestPlugin;
import me.lucko.luckperms.standalone.utils.TestPluginProvider;
@@ -50,6 +49,7 @@ import net.luckperms.api.model.data.DataType;
import net.luckperms.api.node.Node;
import net.luckperms.api.node.NodeType;
import net.luckperms.api.node.types.PrefixNode;
import net.luckperms.api.platform.Health;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
@@ -118,9 +118,9 @@ public class StorageIntegrationTest {
private static void testStorage(LuckPermsApplication app, TestPluginBootstrap bootstrap, TestPlugin plugin) {
// check the plugin is healthy
HealthReporter.Health health = app.getHealthReporter().poll();
Health health = plugin.runHealthCheck();
assertNotNull(health);
assertTrue(health.isUp());
assertTrue(health.isHealthy());
// try to create / save a group
Group group = plugin.getStorage().createAndLoadGroup("test", CreationCause.INTERNAL).join();