diff --git a/infrastructure/src/main/java/org/depromeet/spot/infrastructure/redis/EmbeddedRedisConfig.java b/infrastructure/src/main/java/org/depromeet/spot/infrastructure/redis/EmbeddedRedisConfig.java index 73385e03..788e6558 100644 --- a/infrastructure/src/main/java/org/depromeet/spot/infrastructure/redis/EmbeddedRedisConfig.java +++ b/infrastructure/src/main/java/org/depromeet/spot/infrastructure/redis/EmbeddedRedisConfig.java @@ -2,9 +2,11 @@ import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.net.InetSocketAddress; import java.net.Socket; -import java.util.Objects; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; import jakarta.annotation.PostConstruct; import jakarta.annotation.PreDestroy; @@ -12,10 +14,12 @@ import org.redisson.Redisson; import org.redisson.api.RedissonClient; import org.redisson.config.Config; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; -import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import org.springframework.core.io.ResourceLoader; import lombok.extern.slf4j.Slf4j; import redis.embedded.RedisServer; @@ -31,6 +35,8 @@ public class EmbeddedRedisConfig { private RedisServer redisServer; private final int embeddedRedisPort; + @Autowired private ResourceLoader resourceLoader; + public EmbeddedRedisConfig() { this.embeddedRedisPort = isPortInUse(REDIS_DEFAULT_PORT) ? findAvailablePort() : REDIS_DEFAULT_PORT; @@ -39,27 +45,33 @@ public EmbeddedRedisConfig() { @PostConstruct public void redisServer() { - // redisServer = new RedisServer(embeddedRedisPort); - if (isArmMac()) { - redisServer = new RedisServer(getRedisFileForArcMac(), embeddedRedisPort); - } else { - redisServer = - RedisServer.builder() - .port(embeddedRedisPort) - .setting("maxmemory 128M") // maxheap 128M - .build(); - } try { + if (isArmMac()) { + File redisFile = getRedisFileForArcMac(); + if (redisFile == null || !redisFile.exists()) { + throw new RuntimeException("Redis binary file not found for ARM Mac"); + } + redisServer = new RedisServer(redisFile, embeddedRedisPort); + } else { + redisServer = + RedisServer.builder() + .port(embeddedRedisPort) + .setting("maxmemory 128M") + .build(); + } redisServer.start(); + log.info("Embedded Redis server started on port {}", embeddedRedisPort); } catch (Exception e) { - e.printStackTrace(); + log.error("Failed to start embedded Redis server", e); + throw new RuntimeException("Failed to start embedded Redis server", e); } } @PreDestroy public void stopRedis() { - if (redisServer != null) { + if (redisServer != null && redisServer.isActive()) { redisServer.stop(); + log.info("Embedded Redis server stopped"); } } @@ -75,23 +87,39 @@ public RedissonClient redissonClient() { * System.getProperty("os.name") : 시스템 이름 반환 */ private boolean isArmMac() { - return Objects.equals(System.getProperty("os.arch"), "aarch64") - && Objects.equals(System.getProperty("os.name"), "Mac OS X"); + String osArch = System.getProperty("os.arch"); + String osName = System.getProperty("os.name"); + return (osArch != null && osArch.equals("aarch64")) + && (osName != null && osName.equals("Mac OS X")); } - /** ARM 아키텍처를 사용하는 Mac에서 실행할 수 있는 Redis 바이너리 파일을 반환 */ private File getRedisFileForArcMac() { try { - return new ClassPathResource("binary/redis/redis-server-7.2.3-mac-arm64").getFile(); - } catch (Exception e) { - e.printStackTrace(); - return null; + Resource resource = + resourceLoader.getResource( + "classpath:binary/redis/redis-server-7.2.3-mac-arm64"); + if (!resource.exists()) { + throw new IOException("Redis binary file not found"); + } + + File tempFile = File.createTempFile("redis-server", ".tmp"); + tempFile.deleteOnExit(); + + try (InputStream is = resource.getInputStream()) { + Files.copy(is, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + } + + tempFile.setExecutable(true); + return tempFile; + } catch (IOException e) { + log.error("Failed to load Redis binary file", e); + throw new RuntimeException("Failed to load Redis binary file", e); } } private boolean isPortInUse(final int port) { try (Socket socket = new Socket()) { - socket.connect(new InetSocketAddress("localhost", port), 200); + socket.connect(new InetSocketAddress("localhost", port), 500); return true; } catch (IOException e) { return false;