Reload redis in a separate thread to not lag the main thread

This commit is contained in:
Govindas 2020-12-03 13:21:16 +02:00
parent 4363d121a4
commit 36d3b4642e
2 changed files with 13 additions and 5 deletions

View File

@ -7,6 +7,8 @@ import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
public class CommandReloadRedis implements CommandExecutor { public class CommandReloadRedis implements CommandExecutor {
private RediSkript plugin; private RediSkript plugin;
public CommandReloadRedis(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."))); , "&2[&aRediSkript&2] &cThis command can only be executed in console.")));
return true; 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 //not sending to sender, because this command can only be executed via console
Bukkit.getLogger().info(ChatColor.translateAlternateColorCodes('&', "&eReloaded channels, encryption and login details!")); Bukkit.getLogger().info(ChatColor.translateAlternateColorCodes('&', "&eReloaded channels, encryption and login details!"));

View File

@ -236,7 +236,9 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable {
} }
public void shutdown() { public void shutdown() {
this.isShuttingDown.set(true); this.isShuttingDown.set(true);
if (this.subscribeJedis != null) { if (this.subscribeJedis != null) {
this.unsubscribe(); this.unsubscribe();
this.subscribeJedis.close(); this.subscribeJedis.close();
@ -244,13 +246,9 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable {
if (this.jedisPool != null) { if (this.jedisPool != null) {
jedisPool.close(); jedisPool.close();
jedisPool = null;
} }
this.RedisReconnector.shutdown(); this.RedisReconnector.shutdown();
this.RedisService.shutdown(); this.RedisService.shutdown();
this.RedisService = null;
this.RedisReconnector = null;
encryption = null;
} }
public void reload() { public void reload() {