-
Notifications
You must be signed in to change notification settings - Fork 0
feat(#171): 메인 앱 @Scheduled 스케줄러 8개 pocat-batch 완전 이전 #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
364c725
test(#171): 신규 배치 Job 테스트 RED 작성
cdkkyj123 4d6b3b2
feat(#171): pocat-batch 스케줄러 8개 Job 구현
cdkkyj123 b0bacdf
fix(#171): 누락 엔티티 추가 및 레포 타입 수정
cdkkyj123 844e2c4
fix(#171): 테스트 환경 설정 및 컴파일 오류 수정
cdkkyj123 57ffda8
fix(#171): CRITICAL/HIGH 코드 이슈 수정 (트랜잭션 경계, 분산락, Reaper)
cdkkyj123 590ce56
fix(#171): 외부 리뷰 반영 — 설정·엔티티·Tasklet·Job 수정
cdkkyj123 cf08cdc
fix(#171): 외부 리뷰 반영 2차 — InterruptedException rethrow + 랭킹 staging ke…
cdkkyj123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/main/java/com/rocketcrew/pocatbatch/client/MainAppBuyoutClient.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package com.rocketcrew.pocatbatch.client; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.http.HttpEntity; | ||
| import org.springframework.http.HttpHeaders; | ||
| import org.springframework.http.HttpMethod; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.web.client.HttpClientErrorException; | ||
| import org.springframework.web.client.RestTemplate; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| @Slf4j | ||
| public class MainAppBuyoutClient { | ||
|
|
||
| private final RestTemplate restTemplate; | ||
|
|
||
| @Value("${pocat.main-app.base-url}") | ||
| private String baseUrl; | ||
|
|
||
| @Value("${pocat.main-app.internal-token}") | ||
| private String internalToken; | ||
|
|
||
| /** | ||
| * 경매 구매 확정 실패 복구 요청 | ||
| * POST {baseUrl}/internal/auctions/{id}/recover-buyout | ||
| * 최대 3회 재시도 (지수 백오프) | ||
| * 4xx 오류는 SkipException 발생 | ||
| */ | ||
| public void recoverBuyout(Long auctionId, Long jobExecutionId) { | ||
| String url = String.format("%s/internal/auctions/%d/recover-buyout", baseUrl, auctionId); | ||
|
|
||
| HttpHeaders headers = new HttpHeaders(); | ||
| headers.set("X-Internal-Token", internalToken); | ||
| headers.set("Idempotency-Key", String.format("buyout-%d-%d", auctionId, jobExecutionId)); | ||
|
|
||
| HttpEntity<String> request = new HttpEntity<>(headers); | ||
|
|
||
| int maxRetries = 3; | ||
| long delayMs = 1000; | ||
|
|
||
| for (int attempt = 1; attempt <= maxRetries; attempt++) { | ||
| try { | ||
| restTemplate.exchange(url, HttpMethod.POST, request, String.class); | ||
| log.info("경매 구매 확정 복구 성공: auctionId={}", auctionId); | ||
| return; | ||
| } catch (HttpClientErrorException e) { | ||
| log.warn("경매 구매 확정 복구 4xx 오류: auctionId={}, status={}", auctionId, e.getStatusCode()); | ||
| throw new RuntimeException("4xx 오류로 스킵", e); | ||
| } catch (Exception e) { | ||
| if (attempt == maxRetries) { | ||
| log.error("경매 구매 확정 복구 실패: auctionId={}", auctionId, e); | ||
| throw e; | ||
| } | ||
| try { | ||
| Thread.sleep(delayMs); | ||
| delayMs *= 2; | ||
| } catch (InterruptedException ie) { | ||
| Thread.currentThread().interrupt(); | ||
| throw new RuntimeException(ie); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
67 changes: 67 additions & 0 deletions
67
src/main/java/com/rocketcrew/pocatbatch/client/MainAppRefundClient.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package com.rocketcrew.pocatbatch.client; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.http.HttpEntity; | ||
| import org.springframework.http.HttpHeaders; | ||
| import org.springframework.http.HttpMethod; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.web.client.HttpClientErrorException; | ||
| import org.springframework.web.client.RestTemplate; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| @Slf4j | ||
| public class MainAppRefundClient { | ||
|
|
||
| private final RestTemplate restTemplate; | ||
|
|
||
| @Value("${pocat.main-app.base-url}") | ||
| private String baseUrl; | ||
|
|
||
| @Value("${pocat.main-app.internal-token}") | ||
| private String internalToken; | ||
|
|
||
| /** | ||
| * 환불 재시도 요청 | ||
| * POST {baseUrl}/internal/refunds/{id}/retry | ||
| * 최대 3회 재시도 (지수 백오프) | ||
| * 4xx 오류는 SkipException 발생 | ||
| */ | ||
| public void retryRefund(Long refundId, Long jobExecutionId) { | ||
| String url = String.format("%s/internal/refunds/%d/retry", baseUrl, refundId); | ||
|
|
||
| HttpHeaders headers = new HttpHeaders(); | ||
| headers.set("X-Internal-Token", internalToken); | ||
| headers.set("Idempotency-Key", String.format("refund-%d-%d", refundId, jobExecutionId)); | ||
|
|
||
| HttpEntity<String> request = new HttpEntity<>(headers); | ||
|
|
||
| int maxRetries = 3; | ||
| long delayMs = 1000; | ||
|
|
||
| for (int attempt = 1; attempt <= maxRetries; attempt++) { | ||
| try { | ||
| restTemplate.exchange(url, HttpMethod.POST, request, String.class); | ||
| log.info("환불 재시도 성공: refundId={}", refundId); | ||
| return; | ||
| } catch (HttpClientErrorException e) { | ||
| log.warn("환불 재시도 4xx 오류: refundId={}, status={}", refundId, e.getStatusCode()); | ||
| throw new RuntimeException("4xx 오류로 스킵", e); | ||
| } catch (Exception e) { | ||
| if (attempt == maxRetries) { | ||
| log.error("환불 재시도 실패: refundId={}", refundId, e); | ||
| throw e; | ||
| } | ||
| try { | ||
| Thread.sleep(delayMs); | ||
| delayMs *= 2; | ||
| } catch (InterruptedException ie) { | ||
| Thread.currentThread().interrupt(); | ||
| throw new RuntimeException(ie); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions
49
src/main/java/com/rocketcrew/pocatbatch/config/KafkaProducerConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package com.rocketcrew.pocatbatch.config; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.boot.autoconfigure.kafka.KafkaProperties; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.context.annotation.Profile; | ||
| import org.springframework.kafka.core.DefaultKafkaProducerFactory; | ||
| import org.springframework.kafka.core.KafkaTemplate; | ||
| import org.springframework.kafka.core.ProducerFactory; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| @Configuration | ||
| @Profile("!test") | ||
| @RequiredArgsConstructor | ||
| public class KafkaProducerConfig { | ||
|
|
||
| private final KafkaProperties kafkaProperties; | ||
|
|
||
| public static final Set<String> FINANCIAL_TOPICS = Set.of("payment", "refund", "settlement"); | ||
|
|
||
| /** | ||
| * 기본 KafkaTemplate (acks=1) | ||
| * 일반 이벤트 발행용 | ||
| */ | ||
| @Bean | ||
| public KafkaTemplate<String, String> kafkaTemplate() { | ||
| Map<String, Object> props = new HashMap<>(kafkaProperties.buildProducerProperties(null)); | ||
| props.put("acks", "1"); | ||
| ProducerFactory<String, String> factory = new DefaultKafkaProducerFactory<>(props); | ||
| return new KafkaTemplate<>(factory); | ||
| } | ||
|
|
||
| /** | ||
| * 금융 관련 KafkaTemplate (acks=all, enable.idempotence=true) | ||
| * 환불, 결제, 정산 이벤트 발행용 (중복 방지 + 높은 안정성) | ||
| */ | ||
| @Bean | ||
| public KafkaTemplate<String, String> paymentKafkaTemplate() { | ||
| Map<String, Object> props = new HashMap<>(kafkaProperties.buildProducerProperties(null)); | ||
| props.put("acks", "all"); | ||
| props.put("enable.idempotence", true); | ||
| ProducerFactory<String, String> factory = new DefaultKafkaProducerFactory<>(props); | ||
| return new KafkaTemplate<>(factory); | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/rocketcrew/pocatbatch/config/RedissonConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package com.rocketcrew.pocatbatch.config; | ||
|
|
||
| import org.redisson.Redisson; | ||
| import org.redisson.api.RedissonClient; | ||
| import org.redisson.config.Config; | ||
| import org.redisson.config.SingleServerConfig; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.context.annotation.Profile; | ||
|
|
||
| @Profile("!test") | ||
| @Configuration | ||
| public class RedissonConfig { | ||
|
|
||
| @Value("${spring.data.redis.host:localhost}") | ||
| private String redisHost; | ||
|
|
||
| @Value("${spring.data.redis.port:6379}") | ||
| private int redisPort; | ||
|
|
||
| @Value("${spring.data.redis.password:}") | ||
| private String redisPassword; | ||
|
|
||
| /** | ||
| * Redisson Client Bean | ||
| * 경매 락(auction activation, expiration) 및 일반적인 Redis 분산 락 사용 | ||
| */ | ||
| @Bean | ||
| public RedissonClient redissonClient() { | ||
| Config config = new Config(); | ||
| SingleServerConfig serverConfig = config.useSingleServer() | ||
| .setAddress(String.format("redis://%s:%d", redisHost, redisPort)); | ||
| if (redisPassword != null && !redisPassword.isEmpty()) { | ||
| serverConfig.setPassword(redisPassword); | ||
| } | ||
| return Redisson.create(config); | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/rocketcrew/pocatbatch/config/RestClientConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package com.rocketcrew.pocatbatch.config; | ||
|
|
||
| import org.springframework.boot.web.client.RestTemplateBuilder; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.context.annotation.Profile; | ||
| import org.springframework.web.client.RestTemplate; | ||
|
|
||
| import java.time.Duration; | ||
|
|
||
| @Configuration | ||
| @Profile("!test") | ||
| public class RestClientConfig { | ||
|
|
||
| /** | ||
| * RestTemplate Bean | ||
| * Main App 내부 API 호출용 (buyout recovery, refund retry) | ||
| */ | ||
| @Bean | ||
| public RestTemplate restTemplate(RestTemplateBuilder builder) { | ||
| return builder | ||
| .connectTimeout(Duration.ofSeconds(5)) | ||
| .readTimeout(Duration.ofSeconds(10)) | ||
| .build(); | ||
| } | ||
| } | ||
67 changes: 67 additions & 0 deletions
67
src/main/java/com/rocketcrew/pocatbatch/domain/ai/entity/AiChatSession.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package com.rocketcrew.pocatbatch.domain.ai.entity; | ||
|
|
||
| import com.rocketcrew.pocatbatch.domain.freepost.entity.BaseEntity; | ||
| import jakarta.persistence.*; | ||
| import lombok.*; | ||
| import org.hibernate.annotations.SQLDelete; | ||
| import org.hibernate.annotations.SQLRestriction; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| /** | ||
| * AI 채팅 세션 엔티티. | ||
| * 사용자별 멀티턴 대화 세션 관리. | ||
| */ | ||
| @Getter | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| @Builder | ||
| @AllArgsConstructor | ||
| @Entity | ||
| @Table(name = "ai_chat_sessions", | ||
| indexes = { | ||
| @Index(name = "idx_ai_chat_session_user_id", columnList = "user_id"), | ||
| @Index(name = "idx_ai_chat_session_uuid", columnList = "session_uuid"), | ||
| @Index(name = "idx_ai_chat_session_last_active", columnList = "last_active_at") | ||
| } | ||
| ) | ||
| @SQLDelete(sql = "UPDATE ai_chat_sessions SET deleted_at = NOW() WHERE id = ?") | ||
| @SQLRestriction("deleted_at IS NULL") | ||
| public class AiChatSession extends BaseEntity { | ||
|
|
||
| @Column(name = "user_id", nullable = false) | ||
| private Long userId; | ||
|
|
||
| @Column(name = "session_uuid", nullable = false, length = 36, unique = true) | ||
| private String sessionUuid; | ||
|
|
||
| @Column(name = "total_tokens", nullable = false) | ||
| private Integer totalTokens; | ||
|
|
||
| @Column(name = "is_expired", nullable = false) | ||
| private Boolean isExpired; | ||
|
|
||
| @Column(name = "last_active_at", nullable = false) | ||
| private LocalDateTime lastActiveAt; | ||
|
|
||
| /** | ||
| * 토큰 추가. | ||
| */ | ||
| public void addTokens(int tokens) { | ||
| this.totalTokens += tokens; | ||
| } | ||
|
|
||
| /** | ||
| * 마지막 활동 시간 업데이트. | ||
| */ | ||
| public void updateLastActiveAt(LocalDateTime now) { | ||
| this.lastActiveAt = now; | ||
| } | ||
|
|
||
| /** | ||
| * 만료된 세션 재활성화. | ||
| */ | ||
| public void reactivate(LocalDateTime now) { | ||
| this.isExpired = false; | ||
| this.lastActiveAt = now; | ||
| } | ||
| } |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/rocketcrew/pocatbatch/domain/ai/repository/AiChatSessionRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package com.rocketcrew.pocatbatch.domain.ai.repository; | ||
|
|
||
| import com.rocketcrew.pocatbatch.domain.ai.entity.AiChatSession; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.data.jpa.repository.Modifying; | ||
| import org.springframework.data.jpa.repository.Query; | ||
| import org.springframework.data.repository.query.Param; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| @Repository | ||
| public interface AiChatSessionRepository extends JpaRepository<AiChatSession, Long> { | ||
|
|
||
| /** | ||
| * 지정된 시간 이전의 비활성 세션을 만료 처리. | ||
| * | ||
| * @param threshold 기준 시간 | ||
| * @return 만료된 세션 수 | ||
| */ | ||
| @Modifying | ||
| @Query("UPDATE AiChatSession s SET s.isExpired = true WHERE s.lastActiveAt < :threshold AND s.isExpired = false") | ||
| int expireSessionsBeforeTime(@Param("threshold") LocalDateTime threshold); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.