2022-07-20 09:21:24 +00:00
|
|
|
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 {
|
2022-07-26 08:42:42 +00:00
|
|
|
plugin.updateProxiesIds();
|
2022-07-20 09:21:24 +00:00
|
|
|
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 {
|
2022-07-26 08:40:14 +00:00
|
|
|
long redisTime = plugin.getRedisTime(jedisCluster);
|
2022-07-20 09:21:24 +00:00
|
|
|
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 {
|
2022-07-26 08:42:42 +00:00
|
|
|
plugin.updateProxiesIds();
|
2022-07-20 09:21:24 +00:00
|
|
|
globalPlayerCount.set(plugin.getCurrentCount());
|
|
|
|
} catch (Throwable e) {
|
|
|
|
plugin.logFatal("Unable to update data - did your Redis server go away?");
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|