-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudySessionSchedulerService.java
More file actions
42 lines (36 loc) · 1.43 KB
/
StudySessionSchedulerService.java
File metadata and controls
42 lines (36 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.gpt.geumpumtabackend.study.service;
import com.gpt.geumpumtabackend.study.domain.StudySession;
import com.gpt.geumpumtabackend.study.repository.StudySessionRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@Service
@RequiredArgsConstructor
@Slf4j
public class StudySessionSchedulerService {
private final StudySessionRepository studySessionRepository;
@Transactional
@Scheduled(fixedDelay = 60000)
public void cleanZombieStudySession() {
LocalDateTime now = LocalDateTime.now();
LocalDateTime threshold = now.minusSeconds(90);
List<StudySession> zombieSessions = studySessionRepository.findAllZombieSession(threshold);
List<StudySession> zombieSession = new ArrayList<>();
zombieSessions.forEach(studySession -> {
try {
studySession.endStudySession(now);
zombieSession.add(studySession);
} catch (Exception e) {
log.error("Failed to end zombie session: {}", studySession.getId(), e);
}
});
if (!zombieSession.isEmpty()) {
studySessionRepository.saveAll(zombieSession);
}
}
}