new ConnectionController! #11

Merged
ham1255 merged 4 commits from patch-1 into master 2020-12-26 10:04:19 +00:00
2 changed files with 15 additions and 6 deletions
Showing only changes of commit bfb8ebc862 - Show all commits

View File

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