Skip to content

Commit

Permalink
bug: jar 파일에서 binary 파일 가지고 오지 못하는 버그 픽스
Browse files Browse the repository at this point in the history
  • Loading branch information
pminsung12 committed Sep 5, 2024
1 parent 0bf9742 commit fd979f4
Showing 1 changed file with 50 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@

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;

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;
Expand All @@ -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;
Expand All @@ -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");
}
}

Expand All @@ -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;
Expand Down

0 comments on commit fd979f4

Please sign in to comment.