From 36d3b4642e2953674a93452814d9853cdec0d7d0 Mon Sep 17 00:00:00 2001 From: Govindas Date: Thu, 3 Dec 2020 13:21:16 +0200 Subject: [PATCH] Reload redis in a separate thread to not lag the main thread --- .../rediskript/commands/CommandReloadRedis.java | 12 +++++++++++- .../limework/rediskript/managers/RedisManager.java | 6 ++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/limework/rediskript/commands/CommandReloadRedis.java b/src/main/java/net/limework/rediskript/commands/CommandReloadRedis.java index 3fcf3e6..4eef890 100644 --- a/src/main/java/net/limework/rediskript/commands/CommandReloadRedis.java +++ b/src/main/java/net/limework/rediskript/commands/CommandReloadRedis.java @@ -7,6 +7,8 @@ import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitRunnable; + public class CommandReloadRedis implements CommandExecutor { private RediSkript plugin; public CommandReloadRedis(RediSkript plugin) { @@ -20,7 +22,15 @@ public class CommandReloadRedis implements CommandExecutor { , "&2[&aRediSkript&2] &cThis command can only be executed in console."))); return true; } - plugin.getRedisManager().reload(); + + //reload redis asynchronously to not lag the main thread (was doing a few seconds lagspike at most) + new BukkitRunnable() { + @Override + public void run() { + plugin.getRedisManager().reload(); + } + }.runTaskAsynchronously(plugin); + //not sending to sender, because this command can only be executed via console Bukkit.getLogger().info(ChatColor.translateAlternateColorCodes('&', "&eReloaded channels, encryption and login details!")); diff --git a/src/main/java/net/limework/rediskript/managers/RedisManager.java b/src/main/java/net/limework/rediskript/managers/RedisManager.java index e34d57d..eff0e5c 100644 --- a/src/main/java/net/limework/rediskript/managers/RedisManager.java +++ b/src/main/java/net/limework/rediskript/managers/RedisManager.java @@ -236,7 +236,9 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable { } public void shutdown() { + this.isShuttingDown.set(true); + if (this.subscribeJedis != null) { this.unsubscribe(); this.subscribeJedis.close(); @@ -244,13 +246,9 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable { if (this.jedisPool != null) { jedisPool.close(); - jedisPool = null; } this.RedisReconnector.shutdown(); this.RedisService.shutdown(); - this.RedisService = null; - this.RedisReconnector = null; - encryption = null; } public void reload() {