-
Notifications
You must be signed in to change notification settings - Fork 1
[FEAT] 작가 생성 토픽으로 작가 스케줄 생성 (#273) #277
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 all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a8d0698
[FEAT] kafka 의존성 및 event-schema-shared 모듈 의존성 추가
redblackblossom 084cde0
[FEAT] kafka 설정 추가
redblackblossom 7c74e27
[CHORE] 주석 추가
redblackblossom 7a3846c
[FEAT] 스케줄 리파지토리 정의
redblackblossom 80389c8
[FEAT] 작가 스케줄 생성 서비스 개발
redblackblossom 9d00c2e
[FEAT] kafka 작가 생성 이벤트로 스케줄 작성
redblackblossom 5c4ce68
[REFACT] PhotographerCreatedEventConsumer에서 ByteBuffer payload 안전 처리
redblackblossom 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
21 changes: 21 additions & 0 deletions
21
...vation/src/main/java/net/catsnap/CatsnapReservation/event/infrastructure/EventConfig.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,21 @@ | ||
| package net.catsnap.CatsnapReservation.event.infrastructure; | ||
|
|
||
| import net.catsnap.shared.application.EventDeserializer; | ||
| import net.catsnap.shared.infrastructure.AvroEventDeserializer; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| /** | ||
| * 이벤트 처리 관련 빈 설정 | ||
| * | ||
| * <p>Kafka Consumer가 수신한 EventEnvelope의 payload를 도메인 이벤트로 변환할 때 사용됩니다.</p> | ||
| * | ||
| */ | ||
| @Configuration | ||
| public class EventConfig { | ||
|
|
||
| @Bean | ||
| public EventDeserializer eventDeserializer() { | ||
| return new AvroEventDeserializer(); | ||
| } | ||
| } |
45 changes: 45 additions & 0 deletions
45
...java/net/catsnap/CatsnapReservation/schedule/application/PhotographerScheduleService.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,45 @@ | ||
| package net.catsnap.CatsnapReservation.schedule.application; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import net.catsnap.CatsnapReservation.schedule.domain.PhotographerSchedule; | ||
| import net.catsnap.CatsnapReservation.schedule.infrastructure.repository.PhotographerScheduleRepository; | ||
| import org.springframework.dao.DataIntegrityViolationException; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| /** | ||
| * 사진작가 스케줄 관리 서비스 | ||
| * | ||
| * <p>사진작가의 예약 가능 스케줄을 생성하고 관리합니다.</p> | ||
| */ | ||
| @Slf4j | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class PhotographerScheduleService { | ||
|
|
||
| private final PhotographerScheduleRepository photographerScheduleRepository; | ||
|
|
||
| /** | ||
| * 사진작가의 기본 스케줄을 생성합니다. | ||
| * | ||
| * <p>이미 스케줄이 존재하면 아무 작업도 수행하지 않습니다 (멱등성 보장). | ||
| * 동시 요청으로 인한 중복 생성 시도 시 DataIntegrityViolationException을 catch하여 정상 처리합니다.</p> | ||
| * | ||
| * @param photographerId 사진작가 ID | ||
| */ | ||
| @Transactional | ||
| public void createDefaultSchedule(Long photographerId) { | ||
| if (photographerScheduleRepository.existsByPhotographerId(photographerId)) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| PhotographerSchedule schedule = PhotographerSchedule.initSchedule(photographerId); | ||
| photographerScheduleRepository.saveAndFlush(schedule); | ||
| log.info("Schedule created: photographerId={}", photographerId); | ||
| } catch (DataIntegrityViolationException e) { | ||
| log.info("Schedule already created by concurrent request: photographerId={}", photographerId); | ||
| } | ||
redblackblossom marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
92 changes: 92 additions & 0 deletions
92
...ap/CatsnapReservation/schedule/infrastructure/event/PhotographerCreatedEventConsumer.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,92 @@ | ||
| package net.catsnap.CatsnapReservation.schedule.infrastructure.event; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import net.catsnap.CatsnapReservation.schedule.application.PhotographerScheduleService; | ||
| import net.catsnap.event.photographer.v1.PhotographerCreated; | ||
| import net.catsnap.event.shared.EventEnvelope; | ||
| import net.catsnap.shared.application.EventDeserializer; | ||
| import org.springframework.kafka.annotation.DltHandler; | ||
| import org.springframework.kafka.annotation.KafkaListener; | ||
| import org.springframework.kafka.annotation.RetryableTopic; | ||
| import org.springframework.kafka.retrytopic.DltStrategy; | ||
| import org.springframework.kafka.support.KafkaHeaders; | ||
| import org.springframework.messaging.handler.annotation.Header; | ||
| import org.springframework.retry.annotation.Backoff; | ||
| import org.springframework.stereotype.Component; | ||
| import java.nio.ByteBuffer; | ||
|
|
||
| /** | ||
| * PhotographerCreated 이벤트 Kafka Consumer | ||
| * | ||
| * <p>member-service에서 사진작가가 생성되면 해당 이벤트를 수신하여 | ||
| * 기본 스케줄을 생성합니다.</p> | ||
| * | ||
| * <h3>재시도 정책</h3> | ||
| * <ul> | ||
| * <li>최대 3회 시도 (원본 1회 + 재시도 2회)</li> | ||
| * <li>재시도 간격: 5초 → 10초 (exponential backoff)</li> | ||
| * <li>최종 실패 시 DLT(Dead Letter Topic)로 이동</li> | ||
| * </ul> | ||
| * | ||
| * <h3>생성되는 토픽</h3> | ||
| * <ul> | ||
| * <li>PhotographerCreated (원본)</li> | ||
| * <li>PhotographerCreated-retry-0 (5초 후 재시도)</li> | ||
| * <li>PhotographerCreated-retry-1 (10초 후 재시도)</li> | ||
| * <li>PhotographerCreated-dlt (최종 실패)</li> | ||
| * </ul> | ||
| */ | ||
| @Slf4j | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class PhotographerCreatedEventConsumer { | ||
|
|
||
| private final PhotographerScheduleService photographerScheduleService; | ||
| private final EventDeserializer eventDeserializer; | ||
|
|
||
| /** | ||
| * PhotographerCreated 이벤트를 처리합니다. | ||
| * | ||
| * @param envelope Kafka로부터 수신한 이벤트 envelope | ||
| */ | ||
| @RetryableTopic( | ||
| attempts = "3", | ||
| backoff = @Backoff(delay = 5000, multiplier = 2), | ||
| dltStrategy = DltStrategy.FAIL_ON_ERROR, | ||
| autoCreateTopics = "true" | ||
| ) | ||
| @KafkaListener(topics = "PhotographerCreated", groupId = "schedule-create") | ||
| public void consume(EventEnvelope envelope) { | ||
| log.info("Received PhotographerCreated event: eventId={}, aggregateId={}", | ||
| envelope.getEventId(), envelope.getAggregateId()); | ||
|
|
||
| ByteBuffer buffer = envelope.getPayload(); | ||
| byte[] payloadBytes = new byte[buffer.remaining()]; | ||
| buffer.duplicate().get(payloadBytes); | ||
|
|
||
| PhotographerCreated event = eventDeserializer.deserialize( | ||
| payloadBytes, | ||
| PhotographerCreated.class | ||
| ); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| photographerScheduleService.createDefaultSchedule(event.getPhotographerId()); | ||
| } | ||
|
|
||
| /** | ||
| * DLT(Dead Letter Topic)로 이동된 메시지를 처리합니다. | ||
| * | ||
| * <p>재시도 횟수를 모두 소진한 메시지가 이 핸들러로 전달됩니다. | ||
| * 현재는 로깅만 수행하며, 추후 알림 연동이 필요합니다.</p> | ||
| * | ||
| * @param envelope 처리 실패한 이벤트 envelope | ||
| * @param errorMessage 실패 원인 메시지 | ||
| */ | ||
| @DltHandler | ||
| public void handleDlt(EventEnvelope envelope, | ||
| @Header(KafkaHeaders.EXCEPTION_MESSAGE) String errorMessage) { | ||
| log.error("DLT 이동 - eventId: {}, aggregateId: {}, error: {}", | ||
| envelope.getEventId(), envelope.getAggregateId(), errorMessage); | ||
| // TODO: Slack/Discord 알림 연동 | ||
| } | ||
| } | ||
27 changes: 27 additions & 0 deletions
27
...CatsnapReservation/schedule/infrastructure/repository/PhotographerScheduleRepository.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,27 @@ | ||
| package net.catsnap.CatsnapReservation.schedule.infrastructure.repository; | ||
|
|
||
| import java.util.Optional; | ||
| import net.catsnap.CatsnapReservation.schedule.domain.PhotographerSchedule; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| /** | ||
| * 사진작가 스케줄 Repository | ||
| */ | ||
| public interface PhotographerScheduleRepository extends JpaRepository<PhotographerSchedule, Long> { | ||
|
|
||
| /** | ||
| * 사진작가 ID로 스케줄을 조회합니다. | ||
| * | ||
| * @param photographerId 사진작가 ID | ||
| * @return 스케줄 (존재하지 않으면 empty) | ||
| */ | ||
| Optional<PhotographerSchedule> findByPhotographerId(Long photographerId); | ||
|
|
||
| /** | ||
| * 사진작가 ID로 스케줄 존재 여부를 확인합니다. | ||
| * | ||
| * @param photographerId 사진작가 ID | ||
| * @return 존재하면 true, 없으면 false | ||
| */ | ||
| boolean existsByPhotographerId(Long photographerId); | ||
| } |
101 changes: 101 additions & 0 deletions
101
...p/CatsnapReservation/schedule/application/PhotographerScheduleServiceIntegrationTest.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,101 @@ | ||
| package net.catsnap.CatsnapReservation.schedule.application; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import java.util.Optional; | ||
| import net.catsnap.CatsnapReservation.schedule.domain.PhotographerSchedule; | ||
| import net.catsnap.CatsnapReservation.schedule.fixture.PhotographerScheduleFixture; | ||
| import net.catsnap.CatsnapReservation.schedule.infrastructure.repository.PhotographerScheduleRepository; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.DisplayNameGeneration; | ||
| import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @DisplayName("PhotographerScheduleService 통합 테스트") | ||
| @DisplayNameGeneration(ReplaceUnderscores.class) | ||
| @SuppressWarnings("NonAsciiCharacters") | ||
| @SpringBootTest | ||
| @Transactional | ||
| class PhotographerScheduleServiceIntegrationTest { | ||
|
|
||
| @Autowired | ||
| private PhotographerScheduleService photographerScheduleService; | ||
|
|
||
| @Autowired | ||
| private PhotographerScheduleRepository photographerScheduleRepository; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| photographerScheduleRepository.deleteAll(); | ||
| } | ||
|
|
||
| @Test | ||
| void 새로운_photographerId로_기본_스케줄을_생성한다() { | ||
| // given | ||
| Long photographerId = 1L; | ||
|
|
||
| // when | ||
| photographerScheduleService.createDefaultSchedule(photographerId); | ||
|
|
||
| // then | ||
| Optional<PhotographerSchedule> found = photographerScheduleRepository.findByPhotographerId(photographerId); | ||
| assertThat(found).isPresent(); | ||
| assertThat(found.get().getPhotographerId()).isEqualTo(photographerId); | ||
| assertThat(found.get().getWeekdayRules()).hasSize(7); | ||
| } | ||
|
|
||
| @Test | ||
| void 이미_스케줄이_존재하면_중복_생성하지_않는다() { | ||
| // given | ||
| Long photographerId = 1L; | ||
| PhotographerSchedule existingSchedule = PhotographerScheduleFixture.createWithPhotographerId(photographerId); | ||
| photographerScheduleRepository.save(existingSchedule); | ||
| Long originalId = existingSchedule.getId(); | ||
|
|
||
| // when | ||
| photographerScheduleService.createDefaultSchedule(photographerId); | ||
|
|
||
| // then | ||
| Optional<PhotographerSchedule> found = photographerScheduleRepository.findByPhotographerId(photographerId); | ||
| assertThat(found).isPresent(); | ||
| assertThat(found.get().getId()).isEqualTo(originalId); | ||
| assertThat(photographerScheduleRepository.count()).isEqualTo(1); | ||
| } | ||
|
|
||
| @Test | ||
| void 동일한_photographerId로_여러번_호출해도_스케줄은_하나만_존재한다() { | ||
| // given | ||
| Long photographerId = 1L; | ||
|
|
||
| // when | ||
| photographerScheduleService.createDefaultSchedule(photographerId); | ||
| photographerScheduleService.createDefaultSchedule(photographerId); | ||
| photographerScheduleService.createDefaultSchedule(photographerId); | ||
|
|
||
| // then | ||
| assertThat(photographerScheduleRepository.count()).isEqualTo(1); | ||
| } | ||
|
|
||
| @Test | ||
| void 서로_다른_photographerId로_호출하면_각각_스케줄이_생성된다() { | ||
| // given | ||
| Long photographerId1 = 1L; | ||
| Long photographerId2 = 2L; | ||
| Long photographerId3 = 3L; | ||
|
|
||
| // when | ||
| photographerScheduleService.createDefaultSchedule(photographerId1); | ||
| photographerScheduleService.createDefaultSchedule(photographerId2); | ||
| photographerScheduleService.createDefaultSchedule(photographerId3); | ||
|
|
||
| // then | ||
| assertThat(photographerScheduleRepository.count()).isEqualTo(3); | ||
| assertThat(photographerScheduleRepository.findByPhotographerId(photographerId1)).isPresent(); | ||
| assertThat(photographerScheduleRepository.findByPhotographerId(photographerId2)).isPresent(); | ||
| assertThat(photographerScheduleRepository.findByPhotographerId(photographerId3)).isPresent(); | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions
49
...est/java/net/catsnap/CatsnapReservation/schedule/fixture/PhotographerScheduleFixture.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 net.catsnap.CatsnapReservation.schedule.fixture; | ||
|
|
||
| import java.time.DayOfWeek; | ||
| import java.time.LocalTime; | ||
| import java.util.List; | ||
| import net.catsnap.CatsnapReservation.schedule.domain.PhotographerSchedule; | ||
| import net.catsnap.CatsnapReservation.schedule.domain.vo.AvailableStartTimes; | ||
|
|
||
| /** | ||
| * PhotographerSchedule 테스트용 Fixture | ||
| */ | ||
| public class PhotographerScheduleFixture { | ||
|
|
||
| public static final Long DEFAULT_PHOTOGRAPHER_ID = 1L; | ||
|
|
||
| /** | ||
| * 기본 스케줄 생성 (모든 요일 휴무) | ||
| */ | ||
| public static PhotographerSchedule createDefault() { | ||
| return PhotographerSchedule.initSchedule(DEFAULT_PHOTOGRAPHER_ID); | ||
| } | ||
|
|
||
| /** | ||
| * 지정된 photographerId로 기본 스케줄 생성 | ||
| */ | ||
| public static PhotographerSchedule createWithPhotographerId(Long photographerId) { | ||
| return PhotographerSchedule.initSchedule(photographerId); | ||
| } | ||
|
|
||
| /** | ||
| * 평일 근무 스케줄 생성 (월~금 9:00, 10:00, 11:00) | ||
| */ | ||
| public static PhotographerSchedule createWeekdaySchedule(Long photographerId) { | ||
| PhotographerSchedule schedule = PhotographerSchedule.initSchedule(photographerId); | ||
| AvailableStartTimes workingTimes = AvailableStartTimes.of(List.of( | ||
| LocalTime.of(9, 0), | ||
| LocalTime.of(10, 0), | ||
| LocalTime.of(11, 0) | ||
| )); | ||
|
|
||
| schedule.updateWeekdayRule(DayOfWeek.MONDAY, workingTimes); | ||
| schedule.updateWeekdayRule(DayOfWeek.TUESDAY, workingTimes); | ||
| schedule.updateWeekdayRule(DayOfWeek.WEDNESDAY, workingTimes); | ||
| schedule.updateWeekdayRule(DayOfWeek.THURSDAY, workingTimes); | ||
| schedule.updateWeekdayRule(DayOfWeek.FRIDAY, workingTimes); | ||
|
|
||
| return schedule; | ||
| } | ||
| } |
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.