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:
parent
4d3f1a551e
commit
9da5845da3
@ -102,4 +102,6 @@ public interface RedisBungeePlugin<P> extends EventsPlatform {
|
||||
|
||||
Long getRedisClusterTime();
|
||||
|
||||
void updateProxyIds();
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -13,10 +13,10 @@ import java.util.concurrent.Callable;
|
||||
|
||||
public abstract class RedisTask<V> implements Runnable, Callable<V> {
|
||||
|
||||
private final Summoner<?> summoner;
|
||||
private final RedisBungeeAPI api;
|
||||
private Jedis jedis;
|
||||
private RedisBungeePlugin<?> plugin;
|
||||
protected final Summoner<?> summoner;
|
||||
protected final RedisBungeeAPI api;
|
||||
protected Jedis jedis;
|
||||
protected RedisBungeePlugin<?> plugin;
|
||||
|
||||
@Override
|
||||
public V call() throws Exception {
|
||||
|
@ -9,6 +9,7 @@ import com.google.common.collect.Multimap;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import com.imaginarycode.minecraft.redisbungee.api.summoners.ClusterJedisSummoner;
|
||||
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.util.RedisUtil;
|
||||
import com.imaginarycode.minecraft.redisbungee.api.util.payload.PayloadUtils;
|
||||
@ -567,8 +568,6 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin
|
||||
}
|
||||
} catch (NumberFormatException ignored) {
|
||||
}
|
||||
} else {
|
||||
jedis.hset("heartbeats", configuration.getProxyId(), jedis.time().get(0));
|
||||
}
|
||||
|
||||
return null;
|
||||
@ -592,58 +591,17 @@ public class RedisBungeeBungeePlugin extends Plugin implements RedisBungeePlugin
|
||||
}
|
||||
} catch (NumberFormatException ignored) {
|
||||
}
|
||||
} else {
|
||||
jedisCluster.hset("heartbeats", configuration.getProxyId(), String.valueOf(getRedisClusterTime()));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}.execute();
|
||||
|
||||
proxiesIds = getCurrentProxiesIds(true, false);
|
||||
updateProxyIds();
|
||||
|
||||
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
|
||||
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);
|
||||
heartbeatTask = service.scheduleAtFixedRate(new HeartbeatTask(this, this.globalPlayerCount), 0, HeartbeatTask.INTERVAL, HeartbeatTask.REPEAT_INTERVAL_TIME_UNIT);
|
||||
|
||||
dataManager = new BungeeDataManager(this);
|
||||
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()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateProxyIds() {
|
||||
proxiesIds = getCurrentProxiesIds(true, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
initialize();
|
||||
|
@ -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.JedisSummoner;
|
||||
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.util.RedisUtil;
|
||||
import com.imaginarycode.minecraft.redisbungee.api.util.payload.PayloadUtils;
|
||||
@ -178,10 +179,7 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin<Player> {
|
||||
}
|
||||
} catch (NumberFormatException ignored) {
|
||||
}
|
||||
} else {
|
||||
jedis.hset("heartbeats", configuration.getProxyId(), jedis.time().get(0));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -208,8 +206,6 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin<Player> {
|
||||
}
|
||||
} catch (NumberFormatException ignored) {
|
||||
}
|
||||
} else {
|
||||
jedisCluster.hset("heartbeats", configuration.getProxyId(), String.valueOf(getRedisClusterTime()));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@ -634,48 +630,9 @@ public class RedisBungeeVelocityPlugin implements RedisBungeePlugin<Player> {
|
||||
|
||||
@Override
|
||||
public void initialize() {
|
||||
proxiesIds = getCurrentProxiesIds(true, false);
|
||||
updateProxyIds();
|
||||
// start heartbeat task
|
||||
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().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();
|
||||
heartbeatTask = getProxy().getScheduler().buildTask(this, new HeartbeatTask(this, this.globalPlayerCount)).repeat(HeartbeatTask.INTERVAL, HeartbeatTask.REPEAT_INTERVAL_TIME_UNIT).schedule();
|
||||
|
||||
getProxy().getEventManager().register(this, new RedisBungeeVelocityListener(this, configuration.getExemptAddresses()));
|
||||
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()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateProxyIds() {
|
||||
this.proxiesIds = this.getCurrentProxiesIds(true, false);
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void proxyInit(ProxyInitializeEvent event) {
|
||||
initialize();
|
||||
|
Loading…
Reference in New Issue
Block a user