-
Notifications
You must be signed in to change notification settings - Fork 7
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
서포터가 선택완료 된 후 기간 만료가 되면 3일 뒤 자동 리뷰 완료로 상태 변경하는 기능 구현 #727
Open
cookienc
wants to merge
7
commits into
dev/BE
Choose a base branch
from
feat/718
base: dev/BE
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
312f2fa
feat: DeadlineSheduler 구
cookienc 5475412
test: DeadlineSheduler Test 구현
cookienc 6879cd8
refactor: 비동기 제거
cookienc fae0edb
test: 동시성 테스트 구현
cookienc f5c1a7a
test: 테스트 데이터 정합성 문제 해결
cookienc 921ceb7
Merge branch 'dev/BE' into feat/718
cookienc d9c81f5
test: 중복 정의 메서드 제거
cookienc 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
17 changes: 0 additions & 17 deletions
17
...end/baton/src/main/java/touch/baton/common/schedule/RunnerPostDeadlineCheckScheduler.java
This file was deleted.
Oops, something went wrong.
46 changes: 46 additions & 0 deletions
46
backend/baton/src/main/java/touch/baton/common/schedule/deadline/command/DeadlineOutbox.java
This file contains 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,46 @@ | ||
package touch.baton.common.schedule.deadline.command; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.Id; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import touch.baton.common.schedule.deadline.command.exception.DeadlineOutboxException; | ||
import touch.baton.domain.common.BaseEntity; | ||
|
||
import java.time.Instant; | ||
|
||
import static jakarta.persistence.GenerationType.IDENTITY; | ||
import static lombok.AccessLevel.PROTECTED; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = PROTECTED) | ||
@Entity | ||
public class DeadlineOutbox extends BaseEntity { | ||
|
||
@GeneratedValue(strategy = IDENTITY) | ||
@Id | ||
private Long id; | ||
|
||
private Long runnerPostId; | ||
|
||
private Instant instantToRun; | ||
|
||
@Builder | ||
public DeadlineOutbox(final Long runnerPostId, final Instant instantToRun) { | ||
validateNotNull(runnerPostId, instantToRun); | ||
this.runnerPostId = runnerPostId; | ||
this.instantToRun = instantToRun; | ||
} | ||
|
||
private void validateNotNull(final Long runnerPostId, final Instant instantToRun) { | ||
if (runnerPostId == null) { | ||
throw new DeadlineOutboxException("DeadlineOutBox의 runnerPostId는 null이 될 수 없습니다."); | ||
} | ||
|
||
if (instantToRun == null) { | ||
throw new DeadlineOutboxException("DeadlineOutBox의 instantToRun은 null이 될 수 없습니다."); | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...n/java/touch/baton/common/schedule/deadline/command/event/TaskSchedulerEventListener.java
This file contains 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,57 @@ | ||
package touch.baton.common.schedule.deadline.command.event; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.boot.context.event.ApplicationStartedEvent; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.scheduling.TaskScheduler; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.transaction.event.TransactionalEventListener; | ||
import touch.baton.common.schedule.deadline.command.service.RunnerPostDeadlineCheckScheduler; | ||
import touch.baton.common.schedule.deadline.command.DeadlineOutbox; | ||
import touch.baton.common.schedule.deadline.command.repository.DeadlineOutboxCommandRepository; | ||
import touch.baton.domain.runnerpost.command.RunnerPost; | ||
import touch.baton.domain.runnerpost.command.event.RunnerPostApplySupporterEvent; | ||
|
||
import java.time.Instant; | ||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
|
||
import static org.springframework.transaction.event.TransactionPhase.AFTER_COMMIT; | ||
|
||
@RequiredArgsConstructor | ||
public class TaskSchedulerEventListener { | ||
|
||
private static final int GRACE_PERIODS_DAYS = 3; | ||
|
||
private final TaskScheduler taskScheduler; | ||
private final RunnerPostDeadlineCheckScheduler runnerPostDeadlineCheckScheduler; | ||
private final DeadlineOutboxCommandRepository deadlineOutboxCommandRepository; | ||
|
||
@TransactionalEventListener(phase = AFTER_COMMIT) | ||
@Transactional | ||
public void subscribeAppliedRunnerPostDeadline(final RunnerPostApplySupporterEvent event) { | ||
final RunnerPost findRunnerPost = runnerPostDeadlineCheckScheduler.joinBySupporter(event.runnerPostId()); | ||
final Instant instantToRun = calculateInstant(findRunnerPost); | ||
|
||
final DeadlineOutbox deadlineOutBox = DeadlineOutbox.builder() | ||
.runnerPostId(event.runnerPostId()) | ||
.instantToRun(instantToRun) | ||
.build(); | ||
deadlineOutboxCommandRepository.save(deadlineOutBox); | ||
taskScheduler.schedule(() -> runnerPostDeadlineCheckScheduler.finishReview(event.runnerPostId()), instantToRun); | ||
} | ||
|
||
private Instant calculateInstant(final RunnerPost findRunnerPost) { | ||
final LocalDateTime gracePeriods = findRunnerPost.getDeadline().getValue().plusDays(GRACE_PERIODS_DAYS); | ||
return gracePeriods.atZone(ZoneId.systemDefault()).toInstant(); | ||
} | ||
|
||
@EventListener(ApplicationStartedEvent.class) | ||
public void setUpSchedules() { | ||
deadlineOutboxCommandRepository.findAll().forEach(deadlineOutbox -> | ||
taskScheduler.schedule(() -> runnerPostDeadlineCheckScheduler.finishReview( | ||
deadlineOutbox.getRunnerPostId()), deadlineOutbox.getInstantToRun() | ||
)); | ||
} | ||
} | ||
|
10 changes: 10 additions & 0 deletions
10
.../java/touch/baton/common/schedule/deadline/command/exception/DeadlineOutboxException.java
This file contains 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,10 @@ | ||
package touch.baton.common.schedule.deadline.command.exception; | ||
|
||
import touch.baton.domain.common.exception.BusinessException; | ||
|
||
public class DeadlineOutboxException extends BusinessException { | ||
|
||
public DeadlineOutboxException(final String message) { | ||
super(message); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...ava/touch/baton/common/schedule/deadline/command/exception/ScheduleBusinessException.java
This file contains 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,10 @@ | ||
package touch.baton.common.schedule.deadline.command.exception; | ||
|
||
import touch.baton.domain.common.exception.BusinessException; | ||
|
||
public class ScheduleBusinessException extends BusinessException { | ||
|
||
public ScheduleBusinessException(final String message) { | ||
super(message); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...ch/baton/common/schedule/deadline/command/repository/DeadlineOutboxCommandRepository.java
This file contains 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,17 @@ | ||
package touch.baton.common.schedule.deadline.command.repository; | ||
|
||
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 touch.baton.common.schedule.deadline.command.DeadlineOutbox; | ||
|
||
public interface DeadlineOutboxCommandRepository extends JpaRepository<DeadlineOutbox, Long> { | ||
|
||
@Modifying(clearAutomatically = true, flushAutomatically = true) | ||
@Query(""" | ||
delete from DeadlineOutbox do | ||
where do.runnerPostId = :id | ||
""") | ||
void deleteByRunnerPostId(@Param("id") Long runnerPostId); | ||
} |
4 changes: 2 additions & 2 deletions
4
...chedule/ScheduleRunnerPostRepository.java → .../RunnerPostDeadlineCommandRepository.java
This file contains 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
41 changes: 41 additions & 0 deletions
41
...ouch/baton/common/schedule/deadline/command/service/RunnerPostDeadlineCheckScheduler.java
This file contains 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,41 @@ | ||
package touch.baton.common.schedule.deadline.command.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import touch.baton.common.schedule.deadline.command.exception.ScheduleBusinessException; | ||
import touch.baton.common.schedule.deadline.command.repository.DeadlineOutboxCommandRepository; | ||
import touch.baton.common.schedule.deadline.command.repository.RunnerPostDeadlineCommandRepository; | ||
import touch.baton.common.schedule.deadline.query.repository.RunnerPostDeadlineQueryRepository; | ||
import touch.baton.domain.runnerpost.command.RunnerPost; | ||
|
||
@RequiredArgsConstructor | ||
@Transactional | ||
@Component | ||
public class RunnerPostDeadlineCheckScheduler { | ||
|
||
private final RunnerPostDeadlineCommandRepository runnerPostDeadlineCommandRepository; | ||
private final RunnerPostDeadlineQueryRepository runnerPostDeadlineQueryRepository; | ||
private final DeadlineOutboxCommandRepository deadlineOutboxCommandRepository; | ||
|
||
public void updateReviewStatus() { | ||
runnerPostDeadlineCommandRepository.updateAllPassedDeadline(); | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public RunnerPost joinBySupporter(final Long runnerPostId) { | ||
return runnerPostDeadlineQueryRepository.joinSupporterByRunnerPostId(runnerPostId) | ||
.orElseThrow(() -> new ScheduleBusinessException(String.format("RunnerPost를 찾을 수 없습니다. runnerPostId=%s",runnerPostId))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 마지막 컴마 뒤에 띄어쓰기 |
||
} | ||
|
||
public void finishReview(final Long runnerPostId) { | ||
final RunnerPost findRunnerPost = runnerPostDeadlineQueryRepository.joinSupporterByRunnerPostIdWithLock(runnerPostId) | ||
.orElseThrow(() -> new ScheduleBusinessException(String.format("RunnerPost를 찾을 수 없습니다. runnerPostId=%s",runnerPostId))); | ||
if (findRunnerPost.isReviewStatusDone()) { | ||
return; | ||
} | ||
|
||
findRunnerPost.finishReview(); | ||
deadlineOutboxCommandRepository.deleteByRunnerPostId(runnerPostId); | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
...ton/common/schedule/SchedulerService.java → ...e/RunnerPostDeadlineSchedulerService.java
This file contains 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
30 changes: 30 additions & 0 deletions
30
...ch/baton/common/schedule/deadline/query/repository/RunnerPostDeadlineQueryRepository.java
This file contains 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,30 @@ | ||
package touch.baton.common.schedule.deadline.query.repository; | ||
|
||
import jakarta.persistence.LockModeType; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Lock; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
import touch.baton.domain.runnerpost.command.RunnerPost; | ||
|
||
import java.util.Optional; | ||
|
||
public interface RunnerPostDeadlineQueryRepository extends JpaRepository<RunnerPost, Long> { | ||
|
||
@Query(""" | ||
select rp | ||
from RunnerPost rp | ||
join fetch rp.supporter | ||
where rp.id = :id | ||
""") | ||
Optional<RunnerPost> joinSupporterByRunnerPostId(@Param("id") Long runnerPostId); | ||
|
||
@Lock(LockModeType.PESSIMISTIC_WRITE) | ||
@Query(""" | ||
select rp | ||
from RunnerPost rp | ||
join fetch rp.supporter | ||
where rp.id = :id | ||
""") | ||
Optional<RunnerPost> joinSupporterByRunnerPostIdWithLock(@Param("id") Long runnerPostId); | ||
} |
This file contains 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 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
18 changes: 18 additions & 0 deletions
18
backend/baton/src/test/java/touch/baton/common/fixture/DeadlineOutboxFixture.java
This file contains 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,18 @@ | ||
package touch.baton.common.fixture; | ||
|
||
import touch.baton.common.schedule.deadline.command.DeadlineOutbox; | ||
|
||
import java.time.Instant; | ||
|
||
public abstract class DeadlineOutboxFixture { | ||
|
||
private DeadlineOutboxFixture() { | ||
} | ||
|
||
public static DeadlineOutbox deadlineOutbox(final Long runnerPostId, final Instant instantToRun) { | ||
return DeadlineOutbox.builder() | ||
.runnerPostId(runnerPostId) | ||
.instantToRun(instantToRun) | ||
.build(); | ||
} | ||
} |
58 changes: 0 additions & 58 deletions
58
...baton/src/test/java/touch/baton/common/schedule/RunnerPostDeadlineCheckSchedulerTest.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이거 일단 해보고, 불편하면 우테코 금지되었던거 써볼까...?