mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 22:15:08 +00:00
improve join/save exception handling
This commit is contained in:
7 files changed
+33
-24
No files matched your search
@@ -38,12 +38,12 @@ import net.draycia.carbon.common.listeners.Listener;
|
||||
import net.draycia.carbon.common.messages.CarbonMessages;
|
||||
import net.draycia.carbon.common.messaging.MessagingManager;
|
||||
import net.draycia.carbon.common.messaging.packets.PacketFactory;
|
||||
import net.draycia.carbon.common.users.PlayerUtils;
|
||||
import net.draycia.carbon.common.users.ProfileCache;
|
||||
import net.draycia.carbon.common.users.ProfileResolver;
|
||||
import net.draycia.carbon.common.users.UserManagerInternal;
|
||||
import net.draycia.carbon.common.util.CloudUtils;
|
||||
import net.draycia.carbon.common.util.ConcurrentUtil;
|
||||
import net.draycia.carbon.common.util.PlayerUtils;
|
||||
import net.draycia.carbon.common.util.UpdateChecker;
|
||||
import ninja.egg82.messenger.services.PacketService;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
@@ -36,12 +36,13 @@ import net.draycia.carbon.common.messaging.MessagingManager;
|
||||
import net.draycia.carbon.common.messaging.packets.PacketFactory;
|
||||
import net.draycia.carbon.common.users.db.DatabaseUserManager;
|
||||
import net.draycia.carbon.common.util.ConcurrentUtil;
|
||||
import net.draycia.carbon.common.util.PlayerUtils;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
import static net.draycia.carbon.common.users.PlayerUtils.saveExceptionHandler;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public abstract class CachingUserManager implements UserManagerInternal<CarbonPlayerCommon> {
|
||||
|
||||
@@ -114,7 +115,7 @@ public abstract class CachingUserManager implements UserManagerInternal<CarbonPl
|
||||
this.playerInjector.injectMembers(player);
|
||||
if (this instanceof DatabaseUserManager) {
|
||||
player.registerPropertyUpdateListener(() ->
|
||||
this.save(player).exceptionally(PlayerUtils.saveExceptionHandler(this.logger, player.username, uuid)));
|
||||
this.save(player).exceptionally(saveExceptionHandler(this.logger, player.username, uuid)));
|
||||
}
|
||||
return player;
|
||||
}, this.executor);
|
||||
@@ -136,7 +137,7 @@ public abstract class CachingUserManager implements UserManagerInternal<CarbonPl
|
||||
try {
|
||||
entry.getValue().join();
|
||||
} catch (final Exception ex) {
|
||||
this.logger.warn("Exception saving data for player with uuid " + entry.getKey());
|
||||
this.logger.warn("Exception saving data for player with uuid '{}'", entry.getKey(), ex);
|
||||
}
|
||||
}
|
||||
ConcurrentUtil.shutdownExecutor(this.executor, TimeUnit.MILLISECONDS, 500);
|
||||
@@ -175,10 +176,7 @@ public abstract class CachingUserManager implements UserManagerInternal<CarbonPl
|
||||
continue;
|
||||
}
|
||||
this.cache.remove(entry.getKey());
|
||||
this.saveIfNeeded(getNow).exceptionally(thr -> {
|
||||
this.logger.warn("Exception saving data for player {} with UUID {}", getNow.username(), getNow.uuid(), thr);
|
||||
return null;
|
||||
});
|
||||
this.saveIfNeeded(getNow).exceptionally(saveExceptionHandler(this.logger, getNow.username, getNow.uuid()));
|
||||
}
|
||||
} finally {
|
||||
this.cacheLock.unlock();
|
||||
|
||||
+20
-9
@@ -17,7 +17,7 @@
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package net.draycia.carbon.common.util;
|
||||
package net.draycia.carbon.common.users;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
@@ -25,7 +25,6 @@ import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Function;
|
||||
import net.draycia.carbon.api.CarbonServer;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.common.users.UserManagerInternal;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
@@ -37,6 +36,7 @@ public final class PlayerUtils {
|
||||
private PlayerUtils() {
|
||||
}
|
||||
|
||||
// return value is mostly useful to check if saves are still running; exceptions are already handled on returned futures
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <C extends CarbonPlayer> List<CompletableFuture<Void>> saveLoggedInPlayers(
|
||||
final CarbonServer carbonServer,
|
||||
@@ -48,28 +48,39 @@ public final class PlayerUtils {
|
||||
.toList();
|
||||
}
|
||||
|
||||
public static <C extends CarbonPlayer> CompletableFuture<Void> savePlayer(
|
||||
private static <C extends CarbonPlayer> CompletableFuture<Void> savePlayer(
|
||||
final UserManagerInternal<C> userManager,
|
||||
final C player,
|
||||
final Logger logger
|
||||
) {
|
||||
final var saveResult = userManager.saveIfNeeded(player);
|
||||
saveResult.exceptionally(saveExceptionHandler(logger, player.username(), player.uuid()));
|
||||
return saveResult;
|
||||
|
||||
// avoid fetching the username if it wasn't populated yet; a bit ugly but works (since userManager is always UserManagerInternal<WrappedCarbonPlayer>)
|
||||
final @Nullable CarbonPlayerCommon common = player instanceof WrappedCarbonPlayer wrapped ? wrapped.carbonPlayerCommon() : null;
|
||||
if (common == null) {
|
||||
throw new IllegalStateException("Failed to unwrap " + CarbonPlayerCommon.class.getSimpleName() + " from " + player.getClass());
|
||||
}
|
||||
final @Nullable String username = common.username;
|
||||
|
||||
return saveResult.exceptionally(saveExceptionHandler(logger, username, player.uuid()));
|
||||
}
|
||||
|
||||
public static <T> Function<Throwable, @Nullable T> joinExceptionHandler(final Logger logger) {
|
||||
public static <T> Function<Throwable, @Nullable T> joinExceptionHandler(final Logger logger, final String username, final UUID uuid) {
|
||||
return thr -> {
|
||||
logger.warn("Exception handling player join", thr);
|
||||
logger.warn("Exception handling join for player uuid='{}', username='{}'", uuid, username(username), thr);
|
||||
return null;
|
||||
};
|
||||
}
|
||||
|
||||
public static Function<Throwable, @Nullable Void> saveExceptionHandler(final Logger logger, final String username, final UUID uuid) {
|
||||
public static Function<Throwable, @Nullable Void> saveExceptionHandler(final Logger logger, final @Nullable String username, final UUID uuid) {
|
||||
return thr -> {
|
||||
logger.warn("Exception saving data for player {} with uuid {}", username, uuid, thr);
|
||||
logger.warn("Exception saving data for player uuid='{}', username='{}'", uuid, username(username), thr);
|
||||
return null;
|
||||
};
|
||||
}
|
||||
|
||||
private static String username(final @Nullable String username) {
|
||||
return username == null ? "<unresolved>" : username;
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -37,7 +37,7 @@ import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
import static net.draycia.carbon.common.util.PlayerUtils.saveExceptionHandler;
|
||||
import static net.draycia.carbon.common.users.PlayerUtils.saveExceptionHandler;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class FabricJoinQuitListener implements ServerPlayConnectionEvents.Join, ServerPlayConnectionEvents.Disconnect {
|
||||
|
||||
@@ -38,8 +38,8 @@ import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
import static net.draycia.carbon.common.util.PlayerUtils.joinExceptionHandler;
|
||||
import static net.draycia.carbon.common.util.PlayerUtils.saveExceptionHandler;
|
||||
import static net.draycia.carbon.common.users.PlayerUtils.joinExceptionHandler;
|
||||
import static net.draycia.carbon.common.users.PlayerUtils.saveExceptionHandler;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class PaperPlayerJoinListener implements Listener {
|
||||
@@ -82,7 +82,7 @@ public class PaperPlayerJoinListener implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGH)
|
||||
public void onJoin(final PlayerJoinEvent event) {
|
||||
this.userManager.user(event.getPlayer().getUniqueId()).exceptionally(joinExceptionHandler(this.logger));
|
||||
this.userManager.user(event.getPlayer().getUniqueId()).exceptionally(joinExceptionHandler(this.logger, event.getPlayer().getName(), event.getPlayer().getUniqueId()));
|
||||
|
||||
final @Nullable List<String> suggestions = this.configManager.primaryConfig().customChatSuggestions();
|
||||
|
||||
|
||||
+2
-2
@@ -29,7 +29,7 @@ import org.apache.logging.log4j.Logger;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
import static net.draycia.carbon.common.util.PlayerUtils.joinExceptionHandler;
|
||||
import static net.draycia.carbon.common.users.PlayerUtils.joinExceptionHandler;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class VelocityPlayerJoinListener implements VelocityListener<LoginEvent> {
|
||||
@@ -54,7 +54,7 @@ public class VelocityPlayerJoinListener implements VelocityListener<LoginEvent>
|
||||
@Override
|
||||
public EventTask executeAsync(final LoginEvent event) {
|
||||
return EventTask.async(
|
||||
() -> this.userManager.user(event.getPlayer().getUniqueId()).exceptionally(joinExceptionHandler(this.logger))
|
||||
() -> this.userManager.user(event.getPlayer().getUniqueId()).exceptionally(joinExceptionHandler(this.logger, event.getPlayer().getUsername(), event.getPlayer().getUniqueId()))
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -29,7 +29,7 @@ import org.apache.logging.log4j.Logger;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
import static net.draycia.carbon.common.util.PlayerUtils.saveExceptionHandler;
|
||||
import static net.draycia.carbon.common.users.PlayerUtils.saveExceptionHandler;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public final class VelocityPlayerLeaveListener implements VelocityListener<DisconnectEvent> {
|
||||
|
||||
Reference in new issue
Block a user