Skip to content

Commit

Permalink
(fix): change messaging port detection
Browse files Browse the repository at this point in the history
  • Loading branch information
ZakShearman committed Jan 5, 2024
1 parent 055a5d8 commit 76536d4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
public final class MessagingModule extends Module {
private static final Logger LOGGER = LoggerFactory.getLogger(MessagingModule.class);

private static final String KAFKA_HOST = EnvUtils.getOrDefaultUnlessProd("KAFKA_HOST", "127.0.0.1");
private static final String KAFKA_PORT = EnvUtils.getOrDefaultUnlessProd("KAFKA_PORT", "9092");
private static final String KAFKA_HOST = EnvUtils.getOrDefaultUnlessProd("KAFKA_HOST", null);
private static final String KAFKA_PORT = EnvUtils.getOrDefaultUnlessProd("KAFKA_PORT", null);

private @Nullable FriendlyKafkaConsumer kafkaConsumer;
private @Nullable FriendlyKafkaProducer kafkaProducer;
Expand All @@ -53,6 +53,11 @@ public <T extends AbstractMessage> void addListener(@NotNull Class<T> messageTyp

@Override
public boolean onLoad() {
if (KAFKA_HOST == null || KAFKA_PORT == null) {
LOGGER.warn("Kafka is not available, disabling Kafka consumer and producer");
return false;
}

if (!Environment.isProduction() && !PortUtils.isPortUsed(KAFKA_HOST, Integer.parseInt(KAFKA_PORT))) {
LOGGER.warn("Kafka is not available, disabling Kafka consumer and producer");
return false;
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/dev/emortal/minestom/core/utils/EnvUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import dev.emortal.minestom.core.Environment;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnknownNullability;

public final class EnvUtils {

Expand All @@ -16,7 +18,7 @@ public final class EnvUtils {
* @return the environment variable value or the default value
* @throws IllegalStateException if the environment variable is not set and the environment is production
*/
public static @NotNull String getOrDefaultUnlessProd(@NotNull String envKey, @NotNull String defaultValue) {
public static @UnknownNullability String getOrDefaultUnlessProd(@NotNull String envKey, @Nullable String defaultValue) {
String envValue = System.getenv(envKey);
if (envValue != null && !envValue.isEmpty()) return envValue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public final class PortUtils {
*/
public static boolean isPortUsed(@NotNull String address, int port) {
try (var socket = new Socket(address, port)) {
socket.setSoTimeout(10);
socket.setSoTimeout(50);
return true;
} catch (ConnectException exception) {
return false;
Expand Down

0 comments on commit 76536d4

Please sign in to comment.