Fixed bug where connection manager is keep reconnecting when connection reconnect after failure

This commit is contained in:
mohammed jasem alaajel 2020-12-25 23:27:06 +04:00
parent 029222a068
commit bfb8ebc862
2 changed files with 15 additions and 6 deletions

View File

@ -26,12 +26,12 @@ public class RediSkript extends JavaPlugin {
public void sendLogs(String message) { public void sendLogs(String message) {
getLogger().info( getLogger().info(
ChatColor.translateAlternateColorCodes('&', "&b[RediSkript]&e " + message) ChatColor.translateAlternateColorCodes('&', "&b" + message)
); );
} }
public void sendErrorLogs(String message) { public void sendErrorLogs(String message) {
getLogger().severe( getLogger().severe(
ChatColor.translateAlternateColorCodes('&', "&b[RediSkript]&c " + message) ChatColor.translateAlternateColorCodes('&', "&c" + message)
); );
} }

View File

@ -33,6 +33,7 @@ public class RedisController extends BinaryJedisPubSub implements Runnable {
private byte[][] channelsInByte; private byte[][] channelsInByte;
private final AtomicBoolean isConnectionBroken; private final AtomicBoolean isConnectionBroken;
private final AtomicBoolean isConnecting;
private final RediSkript plugin; private final RediSkript plugin;
private final BukkitTask ConnectionTask; private final BukkitTask ConnectionTask;
@ -58,30 +59,38 @@ public class RedisController extends BinaryJedisPubSub implements Runnable {
encryption = new Encryption(config); encryption = new Encryption(config);
setupChannels(config); setupChannels(config);
isConnectionBroken = new AtomicBoolean(true); isConnectionBroken = new AtomicBoolean(true);
isConnecting = new AtomicBoolean(false);
//Start the main task on async thread //Start the main task on async thread
ConnectionTask = plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin,this, 0 , 20 * 5 ); ConnectionTask = plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin, this, 0, 20 * 5);
} }
@Override @Override
public void run() { public void run() {
if (!isConnectionBroken.get()) { if (!isConnectionBroken.get() || isConnecting.get()) {
return; return;
} }
plugin.sendLogs("Connecting to redis......"); plugin.sendLogs("Connecting to redis......");
isConnecting.set(true);
try (Jedis jedis = jedisPool.getResource()) { try (Jedis jedis = jedisPool.getResource()) {
isConnectionBroken.set(false); isConnectionBroken.set(false);
plugin.sendLogs("&aConnection to redis has established!"); plugin.sendLogs("&aConnection to redis has established!");
jedis.subscribe(this, channelsInByte); jedis.subscribe(this, channelsInByte);
} catch (Exception e) { } catch (Exception e) {
isConnecting.set(false);
isConnectionBroken.set(true); isConnectionBroken.set(true);
plugin.sendLogs("Connection has &kFAILED &cor Unable to connect to redis retrying to make connection..."); plugin.sendLogs("Connection has &kFAILED &cor Unable to connect to redis retrying to make connection...");
e.printStackTrace();
} }
} }
public void shutdown() { public void shutdown() {
ConnectionTask.cancel(); ConnectionTask.cancel();
this.unsubscribe(); try {
this.unsubscribe();
} catch (Exception e) {
plugin.sendErrorLogs("Something went wrong during unsubscribing...");
}
jedisPool.close(); jedisPool.close();
} }