refactor: enhance session cleanup task with error handling and logging
This commit is contained in:
parent
9e562fbc1a
commit
8052c391bf
@ -1,13 +1,16 @@
|
|||||||
package com.magamochi.ingestion.task;
|
package com.magamochi.ingestion.task;
|
||||||
|
|
||||||
import com.magamochi.ingestion.client.FlareClient;
|
import com.magamochi.ingestion.client.FlareClient;
|
||||||
|
import com.magamochi.ingestion.model.entity.FlareSession;
|
||||||
import com.magamochi.ingestion.service.FlareSessionRegistry;
|
import com.magamochi.ingestion.service.FlareSessionRegistry;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Log4j2
|
||||||
@Component
|
@Component
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class FlareSessionCleanupTask {
|
public class FlareSessionCleanupTask {
|
||||||
@ -18,18 +21,21 @@ public class FlareSessionCleanupTask {
|
|||||||
|
|
||||||
@Scheduled(fixedDelayString = "1m")
|
@Scheduled(fixedDelayString = "1m")
|
||||||
public void cleanExpiredSessions() {
|
public void cleanExpiredSessions() {
|
||||||
registry
|
registry.getSessions().forEach(this::destroySession);
|
||||||
.getSessions()
|
}
|
||||||
.forEach(
|
|
||||||
(provider, session) -> {
|
public void destroySession(String provider, FlareSession session) {
|
||||||
if (Duration.between(session.lastAccess(), Instant.now()).compareTo(TIMEOUT) <= 0) {
|
if (Duration.between(session.lastAccess(), Instant.now()).compareTo(TIMEOUT) <= 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
client.destroySession(
|
client.destroySession(
|
||||||
FlareClient.SessionDestroyRequest.builder().session(session.sessionId()).build());
|
FlareClient.SessionDestroyRequest.builder().session(session.sessionId()).build());
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("Failed to destroy session for provider {}: {}", provider, e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
registry.remove(provider);
|
registry.remove(provider);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user