From fd979f410e20ec7992ee806bc83058d2c110c631 Mon Sep 17 00:00:00 2001 From: Minseong Park Date: Fri, 6 Sep 2024 03:45:29 +0900 Subject: [PATCH] =?UTF-8?q?bug:=20jar=20=ED=8C=8C=EC=9D=BC=EC=97=90?= =?UTF-8?q?=EC=84=9C=20binary=20=ED=8C=8C=EC=9D=BC=20=EA=B0=80=EC=A7=80?= =?UTF-8?q?=EA=B3=A0=20=EC=98=A4=EC=A7=80=20=EB=AA=BB=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=EB=B2=84=EA=B7=B8=20=ED=94=BD=EC=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../redis/EmbeddedRedisConfig.java | 72 +++++++++++++------ 1 file changed, 50 insertions(+), 22 deletions(-) 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;