Skip to content

Commit

Permalink
redis shutdown fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Sep 12, 2023
1 parent e1eb6b1 commit df1be68
Show file tree
Hide file tree
Showing 6 changed files with 411 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .checkstyle/suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@
checks="MissingJavadocType"/>
<suppress files="src[\\/]main[\\/]java[\\/]com[\\/]google[\\/]inject[\\/]assistedinject[\\/].*"
checks="[a-zA-Z0-9]*"/>
<suppress files="src[\\/]main[\\/]java[\\/]net[\\/]draycia[\\/]carbon[\\/]common[\\/]messaging[\\/]RedisMessagingService.java"
checks="[a-zA-Z0-9]*"/>
</suppressions>
5 changes: 5 additions & 0 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ dependencies {
api(libs.messengerNats)
api(libs.messengerRabbitmq)
api(libs.messengerRedis)
compileOnly(libs.jedis)
compileOnlyApi(libs.netty)

api(libs.event)
Expand All @@ -67,3 +68,7 @@ dependencies {
// Plugins
compileOnly(libs.miniplaceholders)
}

license {
exclude("**/net/draycia/carbon/common/messaging/RedisMessagingService.java")
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ protected void init() {
}
new UpdateChecker(this.logger).checkVersion();
});

this.messagingManager.get();
}

protected void shutdown() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@
import net.draycia.carbon.common.users.UserManagerInternal;
import net.draycia.carbon.common.util.ConcurrentUtil;
import net.draycia.carbon.common.util.ExceptionLoggingScheduledThreadPoolExecutor;
import net.draycia.carbon.common.util.Exceptions;
import ninja.egg82.messenger.MessagingService;
import ninja.egg82.messenger.NATSMessagingService;
import ninja.egg82.messenger.PacketManager;
import ninja.egg82.messenger.RabbitMQMessagingService;
import ninja.egg82.messenger.RedisMessagingService;
import ninja.egg82.messenger.handler.AbstractServerMessagingHandler;
import ninja.egg82.messenger.handler.MessagingHandler;
import ninja.egg82.messenger.handler.MessagingHandlerImpl;
Expand Down Expand Up @@ -81,8 +81,8 @@ public class MessagingManager {
private final Logger logger;
private final UUID serverId;
private final @MonotonicNonNull ScheduledExecutorService scheduledExecutor;
private final @MonotonicNonNull PacketService packetService;
private @MonotonicNonNull MessagingService messagingService;
private volatile @MonotonicNonNull PacketService packetService;
private final MessagingService messagingService;

@Inject
public MessagingManager(
Expand Down Expand Up @@ -139,11 +139,14 @@ public boolean removeMessenger(final @NonNull String serviceName) {
handlerImpl.addHandler(new CarbonChatPacketHandler(carbonChat, this, userManager, networkUsers, whisper));

try {
this.initMessagingService(this.packetService, handlerImpl, new File("/"),
configManager.primaryConfig().messagingSettings());
this.messagingService = this.initMessagingService(
this.packetService,
handlerImpl,
new File("/"),
configManager.primaryConfig().messagingSettings()
);
} catch (final IOException | TimeoutException | InterruptedException e) {
e.printStackTrace();
return;
throw Exceptions.rethrow(e);
}

this.packetService.addMessenger(this.messagingService);
Expand Down Expand Up @@ -187,11 +190,12 @@ public void onShutdown() {
if (this.packetService != null) {
this.packetService.flushQueue();
this.packetService.shutdown();
this.packetService = null;
}
// this.messagingService.close(); // todo - this is really slow, easier to just skip for now
this.messagingService.close();
}

private void initMessagingService(
private MessagingService initMessagingService(
final PacketService packetService,
final MessagingHandlerImpl handlerImpl,
final File packetDir,
Expand All @@ -200,7 +204,7 @@ private void initMessagingService(
final String name = "engine1";
final String channelName = "carbon-data";

switch (messagingSettings.brokerType()) {
return switch (messagingSettings.brokerType()) {
case RABBITMQ -> {
this.logger.info("Initializing RabbitMQ Messaging services...");

Expand All @@ -212,7 +216,7 @@ private void initMessagingService(
builder.credentials(messagingSettings.username(), messagingSettings.password());
}

this.messagingService = builder.build();
yield builder.build();
}
case NATS -> {
this.logger.info("Initializing NATS Messaging services...");
Expand All @@ -225,7 +229,7 @@ private void initMessagingService(
builder.credentials(messagingSettings.credentialsFile());
}

this.messagingService = builder.build();
yield builder.build();
}
case REDIS -> {
this.logger.info("Initializing Redis Messaging services...");
Expand All @@ -237,11 +241,11 @@ private void initMessagingService(
builder.credentials(messagingSettings.password());
}

this.messagingService = builder.build();
yield builder.build();
}
case NONE ->
throw new IllegalStateException("MessagingManager initialized with no messaging broker selected!");
}
};
}

public enum BrokerType {
Expand Down
Loading

0 comments on commit df1be68

Please sign in to comment.