@@ -0,0 +1,38 @@
|
||||
package ru.soune.nocopy.service.auth;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.soune.nocopy.entity.AuthToken;
|
||||
import ru.soune.nocopy.repository.AuthTokenRepository;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class CleanupTokenSessionsService {
|
||||
@Autowired
|
||||
private AuthTokenRepository authTokenRepository;
|
||||
|
||||
@Value("${file.storage.auth-token-life-hours}")
|
||||
private int authTokenLife;
|
||||
|
||||
@Transactional
|
||||
@Scheduled(fixedDelay = 30000)
|
||||
public void cleanupExpiredFiles() {
|
||||
LocalDateTime hourBefore = LocalDateTime.now().minusHours(authTokenLife);
|
||||
|
||||
List<AuthToken> tokensForDelete = authTokenRepository.findByLastUsedAtBefore(hourBefore).stream().toList();
|
||||
List<AuthToken> tokensDeleteWithNullUpdate = authTokenRepository.findByLastUsedAtAndExpiresAtBefore(
|
||||
null, hourBefore).stream().toList();
|
||||
|
||||
authTokenRepository.deleteAll(tokensForDelete);
|
||||
authTokenRepository.deleteAll(tokensDeleteWithNullUpdate);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user