2
0
mirror of https://github.com/proxiodev/RedisBungee.git synced 2024-11-22 20:28:00 +00:00

move Heartbeat task into its own class

This commit is contained in:
mohammed jasem alaajel 2022-07-20 13:21:24 +04:00
parent 4d3f1a551e
commit 9da5845da3
5 changed files with 88 additions and 95 deletions

View File

@ -102,4 +102,6 @@ public interface RedisBungeePlugin<P> extends EventsPlatform {
Long getRedisClusterTime(); Long getRedisClusterTime();
void updateProxyIds();
} }

View File

@ -0,0 +1,66 @@
package com.imaginarycode.minecraft.redisbungee.api.tasks;
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.exceptions.JedisConnectionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class HeartbeatTask extends RedisTask<Void>{
public final static TimeUnit REPEAT_INTERVAL_TIME_UNIT = TimeUnit.SECONDS;
public final static int INTERVAL = 1;
private final AtomicInteger globalPlayerCount;
public HeartbeatTask(RedisBungeePlugin<?> plugin, AtomicInteger globalPlayerCount) {
super(plugin);
this.globalPlayerCount = globalPlayerCount;
}
@Override
public Void jedisTask(Jedis jedis) {
try {
long redisTime = plugin.getRedisTime(jedis.time());
jedis.hset("heartbeats", plugin.getConfiguration().getProxyId(), String.valueOf(redisTime));
} catch (JedisConnectionException e) {
// Redis server has disappeared!
plugin.logFatal("Unable to update heartbeat - did your Redis server go away?");
e.printStackTrace();
return null;
}
try {
plugin.updateProxyIds();
globalPlayerCount.set(plugin.getCurrentCount());
} catch (Throwable e) {
plugin.logFatal("Unable to update data - did your Redis server go away?");
e.printStackTrace();
}
return null;
}
@Override
public Void clusterJedisTask(JedisCluster jedisCluster) {
try {
long redisTime = plugin.getRedisClusterTime();
jedisCluster.hset("heartbeats", plugin.getConfiguration().getProxyId(), String.valueOf(redisTime));
} catch (JedisConnectionException e) {
// Redis server has disappeared!
plugin.logFatal("Unable to update heartbeat - did your Redis server go away?");
e.printStackTrace();
return null;
}
try {
plugin.updateProxyIds();
globalPlayerCount.set(plugin.getCurrentCount());
} catch (Throwable e) {
plugin.logFatal("Unable to update data - did your Redis server go away?");
e.printStackTrace();
}
return null;
}
}

View File

@ -13,10 +13,10 @@ import java.util.concurrent.Callable;
public abstract class RedisTask<V> implements Runnable, Callable<V> { public abstract class RedisTask<V> implements Runnable, Callable<V> {
private final Summoner<?> summoner; protected final Summoner<?> summoner;
private final RedisBungeeAPI api; protected final RedisBungeeAPI api;
private Jedis jedis; protected Jedis jedis;
private RedisBungeePlugin<?> plugin; protected RedisBungeePlugin<?> plugin;
@Override @Override
public V call() throws Exception { public V call() throws Exception {

View File

@ -9,6 +9,7 @@ import com.google.common.collect.Multimap;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import com.imaginarycode.minecraft.redisbungee.api.summoners.ClusterJedisSummoner; import com.imaginarycode.minecraft.redisbungee.api.summoners.ClusterJedisSummoner;
import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisSummoner; import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisSummoner;
import com.imaginarycode.minecraft.redisbungee.api.tasks.HeartbeatTask;
import com.imaginarycode.minecraft.redisbungee.api.tasks.RedisTask; import com.imaginarycode.minecraft.redisbungee.api.tasks.RedisTask;
import com.imaginarycode.minecraft.redisbungee.api.util.RedisUtil; import com.imaginarycode.minecraft.redisbungee.api.util.RedisUtil;
import com.imaginarycode.minecraft.redisbungee.api.util.payload.PayloadUtils; import com.imaginarycode.minecraft.redisbungee.api.util.payload.PayloadUtils;
@ -567,8 +568,6 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin
} }
} catch (NumberFormatException ignored) { } catch (NumberFormatException ignored) {
} }
} else {
jedis.hset("heartbeats", configuration.getProxyId(), jedis.time().get(0));
} }
return null; return null;
@ -592,58 +591,17 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin
} }
} catch (NumberFormatException ignored) { } catch (NumberFormatException ignored) {
} }
} else {
jedisCluster.hset("heartbeats", configuration.getProxyId(), String.valueOf(getRedisClusterTime()));
} }
return null; return null;
} }
}.execute(); }.execute();
proxiesIds = getCurrentProxiesIds(true, false); updateProxyIds();
uuidTranslator = new UUIDTranslator(this); uuidTranslator = new UUIDTranslator(this);
RedisTask<Void> heartBeatRedisTask = new RedisTask<Void>(api) {
@Override
public Void jedisTask(Jedis jedis) {
try {
long redisTime = getRedisTime(jedis.time());
jedis.hset("heartbeats", configuration.getProxyId(), String.valueOf(redisTime));
} catch (JedisConnectionException e) {
// Redis server has disappeared!
getLogger().log(Level.SEVERE, "Unable to update heartbeat - did your Redis server go away?", e);
return null;
}
try {
proxiesIds = getCurrentProxiesIds(true, false);
globalPlayerCount.set(getCurrentCount());
} catch (Throwable e) {
getLogger().log(Level.SEVERE, "Unable to update data - did your Redis server go away?", e);
}
return null;
}
@Override heartbeatTask = service.scheduleAtFixedRate(new HeartbeatTask(this, this.globalPlayerCount), 0, HeartbeatTask.INTERVAL, HeartbeatTask.REPEAT_INTERVAL_TIME_UNIT);
public Void clusterJedisTask(JedisCluster jedisCluster) {
try {
long redisTime = getRedisClusterTime();
jedisCluster.hset("heartbeats", configuration.getProxyId(), String.valueOf(redisTime));
} catch (JedisConnectionException e) {
// Redis server has disappeared!
getLogger().log(Level.SEVERE, "Unable to update heartbeat - did your Redis server go away?", e);
return null;
}
try {
proxiesIds = getCurrentProxiesIds(true, false);
globalPlayerCount.set(getCurrentCount());
} catch (Throwable e) {
getLogger().log(Level.SEVERE, "Unable to update data - did your Redis server go away?", e);
}
return null;
}
};
heartbeatTask = service.scheduleAtFixedRate(heartBeatRedisTask::execute, 0, 3, TimeUnit.SECONDS);
dataManager = new BungeeDataManager(this); dataManager = new BungeeDataManager(this);
getProxy().getPluginManager().registerListener(this, new RedisBungeeBungeeListener(this, configuration.getExemptAddresses())); getProxy().getPluginManager().registerListener(this, new RedisBungeeBungeeListener(this, configuration.getExemptAddresses()));
@ -954,6 +912,11 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin
return getRedisTime((List<String>) this.getRedisClusterTimeScript.eval(Collections.singletonList("0"), Collections.emptyList())); return getRedisTime((List<String>) this.getRedisClusterTimeScript.eval(Collections.singletonList("0"), Collections.emptyList()));
} }
@Override
public void updateProxyIds() {
proxiesIds = getCurrentProxiesIds(true, false);
}
@Override @Override
public void onEnable() { public void onEnable() {
initialize(); initialize();

View File

@ -12,6 +12,7 @@ import com.imaginarycode.minecraft.redisbungee.api.*;
import com.imaginarycode.minecraft.redisbungee.api.summoners.ClusterJedisSummoner; import com.imaginarycode.minecraft.redisbungee.api.summoners.ClusterJedisSummoner;
import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisSummoner; import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisSummoner;
import com.imaginarycode.minecraft.redisbungee.api.summoners.Summoner; import com.imaginarycode.minecraft.redisbungee.api.summoners.Summoner;
import com.imaginarycode.minecraft.redisbungee.api.tasks.HeartbeatTask;
import com.imaginarycode.minecraft.redisbungee.api.tasks.RedisTask; import com.imaginarycode.minecraft.redisbungee.api.tasks.RedisTask;
import com.imaginarycode.minecraft.redisbungee.api.util.RedisUtil; import com.imaginarycode.minecraft.redisbungee.api.util.RedisUtil;
import com.imaginarycode.minecraft.redisbungee.api.util.payload.PayloadUtils; import com.imaginarycode.minecraft.redisbungee.api.util.payload.PayloadUtils;
@ -178,10 +179,7 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin<Player> {
} }
} catch (NumberFormatException ignored) { } catch (NumberFormatException ignored) {
} }
} else {
jedis.hset("heartbeats", configuration.getProxyId(), jedis.time().get(0));
} }
return null; return null;
} }
@ -208,8 +206,6 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin<Player> {
} }
} catch (NumberFormatException ignored) { } catch (NumberFormatException ignored) {
} }
} else {
jedisCluster.hset("heartbeats", configuration.getProxyId(), String.valueOf(getRedisClusterTime()));
} }
return null; return null;
} }
@ -634,48 +630,9 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin<Player> {
@Override @Override
public void initialize() { public void initialize() {
proxiesIds = getCurrentProxiesIds(true, false); updateProxyIds();
// start heartbeat task // start heartbeat task
RedisTask<Void> heartBeatRedisTask = new RedisTask<Void>(api) { heartbeatTask = getProxy().getScheduler().buildTask(this, new HeartbeatTask(this, this.globalPlayerCount)).repeat(HeartbeatTask.INTERVAL, HeartbeatTask.REPEAT_INTERVAL_TIME_UNIT).schedule();
@Override
public Void jedisTask(Jedis jedis) {
try {
long redisTime = getRedisTime(jedis.time());
jedis.hset("heartbeats", configuration.getProxyId(), String.valueOf(redisTime));
} catch (JedisConnectionException e) {
// Redis server has disappeared!
getLogger().error("Unable to update heartbeat - did your Redis server go away?", e);
return null;
}
try {
proxiesIds = getCurrentProxiesIds(true, false);
globalPlayerCount.set(getCurrentCount());
} catch (Throwable e) {
getLogger().error("Unable to update data - did your Redis server go away?", e);
}
return null;
}
@Override
public Void clusterJedisTask(JedisCluster jedisCluster) {
try {
long redisTime = getRedisClusterTime();
jedisCluster.hset("heartbeats", configuration.getProxyId(), String.valueOf(redisTime));
} catch (JedisConnectionException e) {
// Redis server has disappeared!
getLogger().error("Unable to update heartbeat - did your Redis server go away?", e);
return null;
}
try {
proxiesIds = getCurrentProxiesIds(true, false);
globalPlayerCount.set(getCurrentCount());
} catch (Throwable e) {
getLogger().error("Unable to update data - did your Redis server go away?", e);
}
return null;
}
};
heartbeatTask = getProxy().getScheduler().buildTask(this, heartBeatRedisTask::execute).repeat(3, TimeUnit.SECONDS).schedule();
getProxy().getEventManager().register(this, new RedisBungeeVelocityListener(this, configuration.getExemptAddresses())); getProxy().getEventManager().register(this, new RedisBungeeVelocityListener(this, configuration.getExemptAddresses()));
getProxy().getEventManager().register(this, dataManager); getProxy().getEventManager().register(this, dataManager);
@ -992,6 +949,11 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin<Player> {
return getRedisTime((List<String>) this.getRedisClusterTimeScript.eval(Collections.singletonList("0"), Collections.emptyList())); return getRedisTime((List<String>) this.getRedisClusterTimeScript.eval(Collections.singletonList("0"), Collections.emptyList()));
} }
@Override
public void updateProxyIds() {
this.proxiesIds = this.getCurrentProxiesIds(true, false);
}
@Subscribe @Subscribe
public void proxyInit(ProxyInitializeEvent event) { public void proxyInit(ProxyInitializeEvent event) {
initialize(); initialize();