refactor: enhance session cleanup task with error handling and logging

This commit is contained in:
Rodrigo Verdiani 2026-03-28 19:57:53 -03:00
parent 9e562fbc1a
commit 8052c391bf

View File

@ -1,13 +1,16 @@
package com.magamochi.ingestion.task;
import com.magamochi.ingestion.client.FlareClient;
import com.magamochi.ingestion.model.entity.FlareSession;
import com.magamochi.ingestion.service.FlareSessionRegistry;
import java.time.Duration;
import java.time.Instant;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Log4j2
@Component
@RequiredArgsConstructor
public class FlareSessionCleanupTask {
@ -18,18 +21,21 @@ public class FlareSessionCleanupTask {
@Scheduled(fixedDelayString = "1m")
public void cleanExpiredSessions() {
registry
.getSessions()
.forEach(
(provider, session) -> {
if (Duration.between(session.lastAccess(), Instant.now()).compareTo(TIMEOUT) <= 0) {
return;
}
registry.getSessions().forEach(this::destroySession);
}
client.destroySession(
FlareClient.SessionDestroyRequest.builder().session(session.sessionId()).build());
public void destroySession(String provider, FlareSession session) {
if (Duration.between(session.lastAccess(), Instant.now()).compareTo(TIMEOUT) <= 0) {
return;
}
registry.remove(provider);
});
try {
client.destroySession(
FlareClient.SessionDestroyRequest.builder().session(session.sessionId()).build());
} catch (Exception e) {
log.warn("Failed to destroy session for provider {}: {}", provider, e.getMessage());
}
registry.remove(provider);
}
}