42 lines
1.3 KiB
Java
42 lines
1.3 KiB
Java
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 {
|
|
private static final Duration TIMEOUT = Duration.ofMinutes(15);
|
|
|
|
private final FlareClient client;
|
|
private final FlareSessionRegistry registry;
|
|
|
|
@Scheduled(fixedDelayString = "1m")
|
|
public void cleanExpiredSessions() {
|
|
registry.getSessions().forEach(this::destroySession);
|
|
}
|
|
|
|
public void destroySession(String provider, FlareSession session) {
|
|
if (Duration.between(session.lastAccess(), Instant.now()).compareTo(TIMEOUT) <= 0) {
|
|
return;
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|