Skip to content

Commit

Permalink
refactor: Further improve debug log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
WiIIiam278 committed Dec 21, 2023
1 parent 48e087a commit 35fdcf7
Showing 1 changed file with 18 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import java.util.logging.Level;

/**
* Manages the connection to the Redis server, handling the caching of user data
* Manages the connection to Redis, handling the caching of user data
*/
public class RedisManager extends JedisPubSub {

Expand All @@ -54,7 +54,7 @@ public RedisManager(@NotNull HuskSync plugin) {
}

/**
* Initialize the redis connection pool
* Initialize Redis connection pool
*/
@Blocking
public void initialize() throws IllegalStateException {
Expand Down Expand Up @@ -83,7 +83,7 @@ public void initialize() throws IllegalStateException {
try {
jedisPool.getResource().ping();
} catch (JedisException e) {
throw new IllegalStateException("Failed to establish connection with the Redis server. "
throw new IllegalStateException("Failed to establish connection with Redis. "
+ "Please check the supplied credentials in the config file", e);
}

Expand Down Expand Up @@ -179,7 +179,7 @@ private CompletableFuture<Optional<DataSnapshot.Packed>> requestData(@NotNull UU
}

/**
* Set a user's data to the Redis server
* Set a user's data to Redis
*
* @param user the user to set data for
* @param data the user's data to set
Expand All @@ -192,7 +192,7 @@ public void setUserData(@NotNull User user, @NotNull DataSnapshot.Packed data) {
RedisKeyType.DATA_UPDATE.getTimeToLive(),
data.asBytes(plugin)
);
plugin.debug(String.format("[%s] Set %s key from Redis server",
plugin.debug(String.format("[%s] Set %s key from Redis",
user.getUsername(), RedisKeyType.DATA_UPDATE));
} catch (Throwable e) {
plugin.log(Level.SEVERE, "An exception occurred setting user data to Redis", e);
Expand All @@ -210,7 +210,7 @@ public void setUserCheckedOut(@NotNull User user, boolean checkedOut) {
} else {
jedis.del(getKey(RedisKeyType.DATA_CHECKOUT, user.getUuid(), clusterId));
}
plugin.debug(String.format("[%s] %s %s key from Redis server", user.getUsername(),
plugin.debug(String.format("[%s] %s %s key from Redis", user.getUsername(),
checkedOut ? "Set" : "Removed", RedisKeyType.DATA_CHECKOUT));
} catch (Throwable e) {
plugin.log(Level.SEVERE, "An exception occurred setting checkout to", e);
Expand All @@ -223,14 +223,15 @@ public Optional<String> getUserCheckedOut(@NotNull User user) {
final byte[] key = getKey(RedisKeyType.DATA_CHECKOUT, user.getUuid(), clusterId);
final byte[] readData = jedis.get(key);
if (readData != null) {
plugin.debug(String.format("[%s] Waiting for %s key to be unset on Redis server",
user.getUsername(), RedisKeyType.DATA_CHECKOUT));
return Optional.of(new String(readData, StandardCharsets.UTF_8));
final String checkoutServer = new String(readData, StandardCharsets.UTF_8);
plugin.debug(String.format("[%s] Waiting for %s key to be unset from %s on Redis",
user.getUsername(), checkoutServer, RedisKeyType.DATA_CHECKOUT));
return Optional.of(checkoutServer);
}
} catch (Throwable e) {
plugin.log(Level.SEVERE, "An exception occurred getting a user's checkout key from Redis", e);
}
plugin.debug(String.format("[%s] %s key no longer set on Redis server", user.getUsername(),
plugin.debug(String.format("[%s] %s key no longer set on Redis", user.getUsername(),
RedisKeyType.DATA_CHECKOUT));
return Optional.empty();
}
Expand All @@ -255,7 +256,7 @@ public void clearUsersCheckedOutOnServer() {
}

/**
* Set a user's server switch to the Redis server
* Set a user's server switch to Redis
*
* @param user the user to set the server switch for
*/
Expand All @@ -266,15 +267,15 @@ public void setUserServerSwitch(@NotNull User user) {
getKey(RedisKeyType.SERVER_SWITCH, user.getUuid(), clusterId),
RedisKeyType.SERVER_SWITCH.getTimeToLive(), new byte[0]
);
plugin.debug(String.format("[%s] Set %s key to Redis server",
plugin.debug(String.format("[%s] Set %s key to Redis",
user.getUsername(), RedisKeyType.SERVER_SWITCH));
} catch (Throwable e) {
plugin.log(Level.SEVERE, "An exception occurred setting a user's server switch key from Redis", e);
}
}

/**
* Fetch a user's data from the Redis server and consume the key if found
* Fetch a user's data from Redis and consume the key if found
*
* @param user The user to fetch data for
* @return The user's data, if it's present on the database. Otherwise, an empty optional.
Expand All @@ -285,11 +286,11 @@ public Optional<DataSnapshot.Packed> getUserData(@NotNull User user) {
final byte[] key = getKey(RedisKeyType.DATA_UPDATE, user.getUuid(), clusterId);
final byte[] dataByteArray = jedis.get(key);
if (dataByteArray == null) {
plugin.debug(String.format("[%s] Waiting for %s key from Redis server",
plugin.debug(String.format("[%s] Waiting for %s key from Redis",
user.getUsername(), RedisKeyType.DATA_UPDATE));
return Optional.empty();
}
plugin.debug(String.format("[%s] Read %s key from Redis server",
plugin.debug(String.format("[%s] Read %s key from Redis",
user.getUsername(), RedisKeyType.DATA_UPDATE));

// Consume the key (delete from redis)
Expand All @@ -309,11 +310,11 @@ public boolean getUserServerSwitch(@NotNull User user) {
final byte[] key = getKey(RedisKeyType.SERVER_SWITCH, user.getUuid(), clusterId);
final byte[] readData = jedis.get(key);
if (readData == null) {
plugin.debug(String.format("[%s] Waiting for %s key from Redis server",
plugin.debug(String.format("[%s] Waiting for %s key from Redis",
user.getUsername(), RedisKeyType.SERVER_SWITCH));
return false;
}
plugin.debug(String.format("[%s] Read %s key from Redis server",
plugin.debug(String.format("[%s] Read %s key from Redis",
user.getUsername(), RedisKeyType.SERVER_SWITCH));

// Consume the key (delete from redis)
Expand Down

0 comments on commit 35fdcf7

Please sign in to comment.