From 19064e0a602597f3c9f7556de4adf6f55b26e3e4 Mon Sep 17 00:00:00 2001 From: mohammed jasem alaajel Date: Sun, 14 Apr 2024 08:04:08 +0400 Subject: [PATCH] impl: redis clean up task, taken from brains impl. --- .../api/tasks/UUIDCleanupTask.java | 56 +++++++++++++++++++ .../minecraft/redisbungee/RedisBungee.java | 2 +- .../redisbungee/commands/CommandLoader.java | 5 +- .../commands/CommandRedisBungee.java | 33 +++++++++-- .../commands/utils/CommandPlatformHelper.java | 1 + .../utils/StopperUUIDCleanupTask.java | 25 +++++++++ .../RedisBungeeVelocityPlugin.java | 2 +- 7 files changed, 114 insertions(+), 10 deletions(-) create mode 100644 RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/tasks/UUIDCleanupTask.java create mode 100644 RedisBungee-Commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/utils/StopperUUIDCleanupTask.java diff --git a/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/tasks/UUIDCleanupTask.java b/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/tasks/UUIDCleanupTask.java new file mode 100644 index 0000000..6e080c4 --- /dev/null +++ b/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/tasks/UUIDCleanupTask.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2013-present RedisBungee contributors + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * + * http://www.eclipse.org/legal/epl-v10.html + */ + +package com.imaginarycode.minecraft.redisbungee.api.tasks; + +import com.google.gson.Gson; +import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin; +import com.imaginarycode.minecraft.redisbungee.api.util.uuid.CachedUUIDEntry; +import redis.clients.jedis.UnifiedJedis; +import redis.clients.jedis.exceptions.JedisException; + +import java.util.ArrayList; + + +public class UUIDCleanupTask extends RedisTask{ + + private final Gson gson = new Gson(); + private final RedisBungeePlugin plugin; + + public UUIDCleanupTask(RedisBungeePlugin plugin) { + super(plugin); + this.plugin = plugin; + } + + // this code is inspired from https://github.com/minecrafter/redisbungeeclean + @Override + public Void unifiedJedisTask(UnifiedJedis unifiedJedis) { + try { + final long number = unifiedJedis.hlen("uuid-cache"); + plugin.logInfo("Found {} entries", number); + ArrayList fieldsToRemove = new ArrayList<>(); + unifiedJedis.hgetAll("uuid-cache").forEach((field, data) -> { + CachedUUIDEntry cachedUUIDEntry = gson.fromJson(data, CachedUUIDEntry.class); + if (cachedUUIDEntry.expired()) { + fieldsToRemove.add(field); + } + }); + if (!fieldsToRemove.isEmpty()) { + unifiedJedis.hdel("uuid-cache", fieldsToRemove.toArray(new String[0])); + } + plugin.logInfo("deleted {} entries", fieldsToRemove.size()); + } catch (JedisException e) { + plugin.logFatal("There was an error fetching information", e); + } + return null; + } + + +} \ No newline at end of file diff --git a/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java b/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java index 4577ac1..852a87b 100644 --- a/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java +++ b/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java @@ -261,7 +261,7 @@ public class RedisBungee extends Plugin implements RedisBungeePlugin commandManager, RedisBungeeConfiguration configuration) { - commandManager.registerCommand(new CommandRedisBungee()); + public static void initCommands(CommandManager commandManager, RedisBungeePlugin plugin) { + commandManager.registerCommand(new CommandRedisBungee(plugin)); // todo: config options to disable each command } diff --git a/RedisBungee-Commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/CommandRedisBungee.java b/RedisBungee-Commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/CommandRedisBungee.java index 315fa3c..8025030 100644 --- a/RedisBungee-Commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/CommandRedisBungee.java +++ b/RedisBungee-Commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/CommandRedisBungee.java @@ -13,10 +13,13 @@ package com.imaginarycode.minecraft.redisbungee.commands; import co.aikar.commands.CommandIssuer; import co.aikar.commands.annotation.*; import com.imaginarycode.minecraft.redisbungee.Constants; +import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin; import com.imaginarycode.minecraft.redisbungee.commands.utils.AdventureBaseCommand; +import com.imaginarycode.minecraft.redisbungee.commands.utils.StopperUUIDCleanupTask; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.event.ClickEvent; import net.kyori.adventure.text.event.HoverEvent; +import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; @@ -26,10 +29,15 @@ import java.util.Date; @CommandPermission("redisbungee.use") public class CommandRedisBungee extends AdventureBaseCommand { + private final RedisBungeePlugin plugin; + + public CommandRedisBungee(RedisBungeePlugin plugin) { + this.plugin = plugin; + } @Default @Subcommand("info|version|git") - public static void info(CommandIssuer issuer) { + public void info(CommandIssuer issuer) { final String message = """ This proxy is running RedisBungee Limework's fork ======================================== @@ -52,17 +60,30 @@ public class CommandRedisBungee extends AdventureBaseCommand { .hoverEvent(HoverEvent.showText(Component.text("Click me to open: " + Constants.getGithubCommitLink()))) ))); } - + // ......: ...... @HelpCommand - public static void help(CommandIssuer issuer) { + public void help(CommandIssuer issuer) { final String message = """ ======================================== - /rb info: shows version, build date, git commit hash. - /rb help: shows this page. - ......: ...... + /rb info: shows info of this version. + /rb help: shows this page. + /rb clean: cleans up the uuid cache + WARNING... command above could cause performance issues ======================================== run /rb help for more commands"""; sendMessage(issuer, MiniMessage.miniMessage().deserialize(message)); } + @Subcommand("clean") + @Private + public void cleanUp(CommandIssuer issuer) { + if (StopperUUIDCleanupTask.isRunning) { + sendMessage(issuer, + Component.text("cleanup is currently running!").color(NamedTextColor.RED)); + return; + } + sendMessage(issuer, + Component.text("cleanup is Starting, you should see the output status in the proxy console").color(NamedTextColor.GOLD)); + plugin.executeAsync(new StopperUUIDCleanupTask(plugin)); + } } diff --git a/RedisBungee-Commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/utils/CommandPlatformHelper.java b/RedisBungee-Commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/utils/CommandPlatformHelper.java index b5da19a..8f6a62f 100644 --- a/RedisBungee-Commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/utils/CommandPlatformHelper.java +++ b/RedisBungee-Commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/utils/CommandPlatformHelper.java @@ -11,6 +11,7 @@ package com.imaginarycode.minecraft.redisbungee.commands.utils; import co.aikar.commands.CommandIssuer; +import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin; import net.kyori.adventure.text.Component; diff --git a/RedisBungee-Commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/utils/StopperUUIDCleanupTask.java b/RedisBungee-Commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/utils/StopperUUIDCleanupTask.java new file mode 100644 index 0000000..06dbe85 --- /dev/null +++ b/RedisBungee-Commands/src/main/java/com/imaginarycode/minecraft/redisbungee/commands/utils/StopperUUIDCleanupTask.java @@ -0,0 +1,25 @@ +package com.imaginarycode.minecraft.redisbungee.commands.utils; + +import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin; +import com.imaginarycode.minecraft.redisbungee.api.tasks.UUIDCleanupTask; +import redis.clients.jedis.UnifiedJedis; + +public class StopperUUIDCleanupTask extends UUIDCleanupTask { + + public static boolean isRunning = false; + + public StopperUUIDCleanupTask(RedisBungeePlugin plugin) { + super(plugin); + } + + + @Override + public Void unifiedJedisTask(UnifiedJedis unifiedJedis) { + isRunning = true; + try { + super.unifiedJedisTask(unifiedJedis); + } catch (Exception ignored) {} + isRunning = false; + return null; + } +} diff --git a/RedisBungee-Velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeVelocityPlugin.java b/RedisBungee-Velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeVelocityPlugin.java index 2d250f0..80babdc 100644 --- a/RedisBungee-Velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeVelocityPlugin.java +++ b/RedisBungee-Velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeVelocityPlugin.java @@ -286,7 +286,7 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin, Con // load commands CommandPlatformHelper.init(new VelocityCommandPlatformHelper()); this.commandManager = new VelocityCommandManager(this.getProxy(), this); - CommandLoader.initCommands(this.commandManager, configuration()); + CommandLoader.initCommands(this.commandManager, this); logInfo("RedisBungee initialized successfully "); }