Skip to content

Commit

Permalink
feat: repository 의존성 제거, 테스트 명확하게 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
youngsu5582 committed Oct 14, 2024
1 parent f6beb1a commit 4a27da8
Show file tree
Hide file tree
Showing 5 changed files with 337 additions and 259 deletions.
Original file line number Diff line number Diff line change
@@ -1,52 +1,55 @@
package corea.scheduler.service;

import corea.room.dto.RoomResponse;
import corea.room.domain.Room;
import corea.scheduler.domain.AutomaticMatching;
import corea.scheduler.domain.ScheduleStatus;
import corea.scheduler.repository.AutomaticMatchingRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;

@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class AutomaticMatchingService {

private static final ZoneId ZONE_ID = ZoneId.of("Asia/Seoul");

private final TaskScheduler taskScheduler;
private final AutomaticMatchingExecutor automaticMatchingExecutor;
private final AutomaticMatchingRepository automaticMatchingRepository;
private Map<Long, ScheduledFuture<?>> scheduledTasks;

private final Map<Long, ScheduledFuture<?>> scheduledTasks = new ConcurrentHashMap<>();

@EventListener(ApplicationReadyEvent.class)
public void schedulePendingAutomaticMatching() {
List<AutomaticMatching> matchings = automaticMatchingRepository.findAllByStatus(ScheduleStatus.PENDING);
@Autowired
public AutomaticMatchingService(TaskScheduler taskScheduler, AutomaticMatchingExecutor automaticMatchingExecutor) {
this.taskScheduler = taskScheduler;
this.automaticMatchingExecutor = automaticMatchingExecutor;
this.scheduledTasks = new ConcurrentHashMap<>();
}

log.info("{}개의 방에 대해 자동 매칭 재예약 시작", matchings.size());
public AutomaticMatchingService(TaskScheduler taskScheduler, AutomaticMatchingExecutor automaticMatchingExecutor, Map<Long, ScheduledFuture<?>> scheduledTasks) {
this.taskScheduler = taskScheduler;
this.automaticMatchingExecutor = automaticMatchingExecutor;
this.scheduledTasks = scheduledTasks;
}

matchings.forEach(matching -> scheduleMatching(matching.getRoomId(), matching.getMatchingStartTime()));
public void modifyTask(Room room) {
cancelScheduledMatching(room.getId());
scheduleMatching(room.getId(), room.getRecruitmentDeadline());
}

log.info("{}개의 방에 대해 자동 매칭 재예약 완료", matchings.size());
public void matchOnRecruitmentDeadline(Room room) {
scheduleMatching(room.getId(), room.getRecruitmentDeadline());
}

public void matchOnRecruitmentDeadline(RoomResponse response) {
scheduleMatching(response.id(), response.recruitmentDeadline());
public void matchOnRecruitmentDeadline(AutomaticMatching automaticMatching) {
scheduleMatching(automaticMatching.getRoomId(), automaticMatching.getMatchingStartTime());
}

private void scheduleMatching(long roomId, LocalDateTime matchingStartTime) {
Expand All @@ -60,7 +63,8 @@ private void scheduleMatching(long roomId, LocalDateTime matchingStartTime) {
}

private Instant toInstant(LocalDateTime matchingStartTime) {
return matchingStartTime.atZone(ZONE_ID).toInstant();
return matchingStartTime.atZone(ZONE_ID)
.toInstant();
}

public void cancel(long roomId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,66 +1,69 @@
package corea.scheduler.service;

import corea.room.dto.RoomResponse;
import corea.room.domain.Room;
import corea.scheduler.domain.AutomaticUpdate;
import corea.scheduler.domain.ScheduleStatus;
import corea.scheduler.repository.AutomaticUpdateRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;

@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class AutomaticUpdateService {

private static final ZoneId ZONE_ID = ZoneId.of("Asia/Seoul");

private final TaskScheduler taskScheduler;
private final AutomaticUpdateExecutor automaticUpdateExecutor;
private final AutomaticUpdateRepository automaticUpdateRepository;
private final Map<Long, ScheduledFuture<?>> scheduledTasks;

private final Map<Long, ScheduledFuture<?>> scheduledTasks = new ConcurrentHashMap<>();

@EventListener(ApplicationReadyEvent.class)
public void schedulePendingAutomaticUpdate() {
List<AutomaticUpdate> updates = automaticUpdateRepository.findAllByStatus(ScheduleStatus.PENDING);
@Autowired
public AutomaticUpdateService(TaskScheduler taskScheduler, AutomaticUpdateExecutor automaticUpdateExecutor) {
this.taskScheduler = taskScheduler;
this.automaticUpdateExecutor = automaticUpdateExecutor;
this.scheduledTasks = new ConcurrentHashMap<>();
}

log.info("{}개의 방에 대해 자동 상태 업데이트 재예약 시작", updates.size());
public AutomaticUpdateService(TaskScheduler taskScheduler, AutomaticUpdateExecutor automaticUpdateExecutor, Map<Long, ScheduledFuture<?>> scheduledTasks) {
this.taskScheduler = taskScheduler;
this.automaticUpdateExecutor = automaticUpdateExecutor;
this.scheduledTasks = scheduledTasks;
}

updates.forEach(update -> scheduleUpdate(update.getRoomId(), update.getUpdateStartTime()));
public void updateAtReviewDeadline(Room room) {
scheduleUpdate(room.getId(), room.getReviewDeadline());
}

log.info("{}개의 방에 대해 자동 상태 업데이트 재예약 완료", updates.size());
public void updateAtReviewDeadline(AutomaticUpdate automaticUpdate) {
scheduleUpdate(automaticUpdate.getRoomId(), automaticUpdate.getUpdateStartTime());
}

public void updateAtReviewDeadline(RoomResponse response) {
scheduleUpdate(response.id(), response.reviewDeadline());
public void modifyTask(Room room) {
cancelScheduledUpdate(room.getId());
scheduleUpdate(room.getId(), room.getReviewDeadline());
}

private void scheduleUpdate(long roomId, LocalDateTime updateStartTime) {
ScheduledFuture<?> schedule = taskScheduler.schedule(
() -> automaticUpdateExecutor.execute(roomId),
toInstant(updateStartTime)
);

log.info("{}번 방 자동 상태 업데이트 예약 - 예약 시간: {}", roomId, updateStartTime);
scheduledTasks.put(roomId, schedule);
}

private Instant toInstant(LocalDateTime updateStartTime) {
return updateStartTime.atZone(ZONE_ID).toInstant();
return updateStartTime.atZone(ZONE_ID)
.toInstant();
}

public void cancel(long roomId) {
Expand All @@ -74,7 +77,6 @@ public void cancel(long roomId) {
private void cancelScheduledUpdate(long roomId) {
ScheduledFuture<?> scheduledUpdate = scheduledTasks.remove(roomId);
scheduledUpdate.cancel(true);

log.info("{}번 방 기존 자동 상태 업데이트 예약 취소", roomId);
}
}
45 changes: 38 additions & 7 deletions backend/src/test/java/corea/fixture/RoomFixture.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import corea.room.domain.RoomClassification;
import corea.room.domain.RoomStatus;
import corea.room.dto.RoomCreateRequest;
import corea.room.dto.RoomUpdateRequest;

import java.time.LocalDateTime;
import java.util.List;
Expand All @@ -19,6 +20,10 @@ public static Room ROOM_DOMAIN(Member member, LocalDateTime recruitmentDeadline)
return ROOM_DOMAIN(member, recruitmentDeadline, RoomStatus.OPEN);
}

public static Room ROOM_DOMAIN_REVIEW_DEADLINE(Member member,LocalDateTime reviewDeadline) {
return ROOM_DOMAIN(member, LocalDateTime.now().plusDays(2),reviewDeadline, RoomStatus.OPEN);
}

public static Room ROOM_DOMAIN_WITH_CLOSED(Member member) {
return ROOM_DOMAIN(member, LocalDateTime.now(), RoomStatus.CLOSE);
}
Expand All @@ -27,6 +32,24 @@ public static Room ROOM_DOMAIN_WITH_PROGRESS(Member member) {
return ROOM_DOMAIN(member, LocalDateTime.now(), RoomStatus.PROGRESS);
}

public static Room ROOM_DOMAIN(Member member, LocalDateTime recruitmentDeadline, LocalDateTime reviewDeadline, RoomStatus status) {
return new Room(
"자바 레이싱 카 - MVC",
"MVC 패턴을 아시나요?",
2,
"https://github.com/example/java-racingcar",
"https://gongu.copyright.or.kr/gongu/wrt/cmmn/wrtFileImageView.do?wrtSn=13301655&filePath=L2Rpc2sxL25ld2RhdGEvMjAyMS8yMS9DTFMxMDAwNC8xMzMwMTY1NV9XUlRfMjFfQ0xTMTAwMDRfMjAyMTEyMTNfMQ==&thumbAt=Y&thumbSe=b_tbumb&wrtTy=10004",
List.of("TDD, 클린코드,자바"),
17,
30,
member,
recruitmentDeadline,
reviewDeadline,
RoomClassification.BACKEND,
status
);
}

public static Room ROOM_DOMAIN(Member member, LocalDateTime recruitmentDeadline, RoomStatus status) {
return new Room(
"자바 레이싱 카 - MVC",
Expand All @@ -39,7 +62,8 @@ public static Room ROOM_DOMAIN(Member member, LocalDateTime recruitmentDeadline,
30,
member,
recruitmentDeadline,
LocalDateTime.now().plusDays(14),
LocalDateTime.now()
.plusDays(14),
RoomClassification.BACKEND,
status
);
Expand All @@ -58,22 +82,27 @@ public static Room ROOM_DOMAIN(Long id, Member member) {
30,
member,
LocalDateTime.now(),
LocalDateTime.now().plusDays(14),
LocalDateTime.now()
.plusDays(14),
RoomClassification.BACKEND,
RoomStatus.OPEN
);
}

public static RoomCreateRequest ROOM_CREATE_REQUEST() {
return ROOM_CREATE_REQUEST(LocalDateTime.now().plusHours(2), LocalDateTime.now().plusDays(2));
return ROOM_CREATE_REQUEST(LocalDateTime.now()
.plusHours(2), LocalDateTime.now()
.plusDays(2));
}

public static RoomCreateRequest ROOM_CREATE_REQUEST_WITH_RECRUITMENT_DEADLINE(LocalDateTime recruitmentDeadline) {
return ROOM_CREATE_REQUEST(recruitmentDeadline, LocalDateTime.now().plusDays(2));
return ROOM_CREATE_REQUEST(recruitmentDeadline, LocalDateTime.now()
.plusDays(2));
}

public static RoomCreateRequest ROOM_CREATE_REQUEST_WITH_REVIEW_DEADLINE(LocalDateTime reviewDeadline) {
return ROOM_CREATE_REQUEST(LocalDateTime.now().plusHours(2), reviewDeadline);
return ROOM_CREATE_REQUEST(LocalDateTime.now()
.plusHours(2), reviewDeadline);
}

public static RoomCreateRequest ROOM_CREATE_REQUEST(LocalDateTime recruitmentDeadline, LocalDateTime reviewDeadline) {
Expand Down Expand Up @@ -102,8 +131,10 @@ public static Room ROOM_PULL_REQUEST(Member member) {
0,
100,
member,
LocalDateTime.now().plusSeconds(100),
LocalDateTime.now().plusDays(1),
LocalDateTime.now()
.plusSeconds(100),
LocalDateTime.now()
.plusDays(1),
RoomClassification.BACKEND,
RoomStatus.OPEN);
}
Expand Down
Loading

0 comments on commit 4a27da8

Please sign in to comment.