Skip to content

Commit

Permalink
Merge pull request #84 from onetime-with-members/feature/#76/event-re…
Browse files Browse the repository at this point in the history
…move-scheduler

[feat] : 특정 시기마다 30일이 지난 이벤트들을 제거할 수 있다
  • Loading branch information
bbbang105 authored Oct 21, 2024
2 parents e6b6ff1 + 8a011e5 commit a4806c4
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/main/java/side/onetime/global/config/SchedulerConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package side.onetime.global.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

@Configuration
@EnableScheduling
public class SchedulerConfig {
}
3 changes: 3 additions & 0 deletions src/main/java/side/onetime/repository/EventRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
import side.onetime.domain.Event;
import side.onetime.repository.custom.EventRepositoryCustom;

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

public interface EventRepository extends JpaRepository<Event,Long>, EventRepositoryCustom {
Optional<Event> findByEventId(UUID eventId);
boolean existsByEventId(UUID eventId);
List<Event> findByCreatedDateBefore(LocalDateTime twoWeeksAgo);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import side.onetime.domain.Event;

public interface EventRepositoryCustom {
void deleteUserEvent(Event event);
void deleteEvent(Event event);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class EventRepositoryImpl implements EventRepositoryCustom {
private final JPAQueryFactory queryFactory;

@Override
public void deleteUserEvent(Event e) {
public void deleteEvent(Event e) {
queryFactory.delete(selection)
.where(selection.schedule.event.eq(e))
.execute();
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/side/onetime/service/EventCleanupScheduler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package side.onetime.service;

import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import side.onetime.domain.Event;
import side.onetime.repository.EventRepository;

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

@Component
@RequiredArgsConstructor
public class EventCleanupScheduler {

private final EventRepository eventRepository;

@Scheduled(cron = "${scheduling.cron}")
@Transactional
public void deleteOldEvents() {
LocalDateTime thirtyDaysAgo = LocalDateTime.now().minusDays(30);

// 30일 이상 지난 이벤트를 찾은 후 삭제
List<Event> oldEvents = eventRepository.findByCreatedDateBefore(thirtyDaysAgo);
oldEvents.forEach(eventRepository::deleteEvent);
}
}
2 changes: 1 addition & 1 deletion src/main/java/side/onetime/service/EventService.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,6 @@ public void removeUserCreatedEvent(String authorizationHeader, String eventId) {
throw new EventParticipationException(EventParticipationErrorResult._IS_NOT_USERS_CREATED_EVENT_PARTICIPATION);
}

eventRepository.deleteUserEvent(event);
eventRepository.deleteEvent(event);
}
}
5 changes: 4 additions & 1 deletion src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,7 @@ jwt:
refresh-token:
expiration-time: ${REFRESH_TOKEN_EXPIRATION_TIME}
register-token:
expiration-time: ${REGISTER_TOKEN_EXPIRATION_TIME}
expiration-time: ${REGISTER_TOKEN_EXPIRATION_TIME}

scheduling:
cron: ${CRON}

0 comments on commit a4806c4

Please sign in to comment.