Skip to content

Commit

Permalink
✨ [Feature] 방 상태 변경 스케쥴러 (#722)
Browse files Browse the repository at this point in the history
굿~
  • Loading branch information
JaBeast authored Mar 13, 2024
1 parent 1b7d451 commit 62d3052
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package gg.party.api.admin.room.service;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import gg.data.party.Room;
import gg.data.party.type.RoomType;
import gg.repo.party.RoomRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Service
@RequiredArgsConstructor
@Slf4j
public class RoomStatusService {
private final RoomRepository roomRepository;

/**
* 방 FINISH 변경 스케쥴러
* 시작한 방(START) 2시간후에 FINISH로 변경
*/
@Transactional
public void finishStartedRooms() {
LocalDateTime twoHoursAgo = LocalDateTime.now().minusHours(2);
List<Room> startedRooms = roomRepository.findByStatus(RoomType.START, null);
for (Room room : startedRooms) {
room.updateRoomStatus(RoomType.FINISH);
roomRepository.save(room);
log.info("Room {} status updated to FINISH", room.getId());
}
}

/**
* 방 FAIL 변경 스케쥴러
* due date 지났는데 시작 안했으면 FAIL로 변경
*/
@Transactional
public void failOpenedRooms() {
LocalDateTime now = LocalDateTime.now();
List<Room> openRooms = roomRepository.findByStatus(RoomType.OPEN, null);
for (Room room : openRooms) {
if (room.getDueDate().isBefore(now)) {
room.updateRoomStatus(RoomType.FAIL);
roomRepository.save(room);
log.info("Room {} status updated to FAIL", room.getId());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package gg.party.api.admin.scheduler;

import org.springframework.stereotype.Component;

import gg.party.api.admin.room.service.RoomStatusService;
import gg.pingpong.api.global.scheduler.AbstractScheduler;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class FailRoomScheduler extends AbstractScheduler {
private final RoomStatusService roomStatusService;

public FailRoomScheduler(RoomStatusService roomStatusService) {
this.roomStatusService = roomStatusService;
this.setCron("0 0/5 * * * *");
}

@Override
public Runnable runnable() {
return () -> {
log.info("FailOpenedRoomsScheduler start");
roomStatusService.failOpenedRooms();
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package gg.party.api.admin.scheduler;

import org.springframework.stereotype.Component;

import gg.party.api.admin.room.service.RoomStatusService;
import gg.pingpong.api.global.scheduler.AbstractScheduler;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class FinishRoomScheduler extends AbstractScheduler {
private final RoomStatusService roomStatusService;

public FinishRoomScheduler(RoomStatusService roomStatusService) {
this.roomStatusService = roomStatusService;
this.setCron("0 0/5 * * * *"); // Example: Run every 5 minutes
}

@Override
public Runnable runnable() {
return () -> {
log.info("FinishStartedRoomsScheduler start");
roomStatusService.finishStartedRooms();
};
}
}

0 comments on commit 62d3052

Please sign in to comment.