Sachin
Sachin

Reputation: 527

Spring Scheduler (Spring Boot) - Time never changes

When I run this, it does log out every 30 seconds, but the system time and time range never change...?

Why is that?

Do I need to remove @Component? Or should I change the val variables to var?

Function

@Component
    internal class SessionEvicter(
        private val redisOperations: ReactiveRedisOperations<String, String>,
        springSessionProperties: SpringSessionProperties,
    ) {

    private val logger = LoggerFactory.getLogger(SessionEvicter::class.java)

    private val redisKeyLocation = springSessionProperties.redis?.expiredSessionsNamespace
        ?: "spring:session:sessions:expirations"

    // run every 120 seconds
    @Scheduled(fixedRate = 30, timeUnit = TimeUnit.SECONDS)
    fun cleanup(): Mono<Void> {
        val now = Instant.now()
        val pastFiveDays = now.minus(Duration.ofDays(5))
        val range = Range.closed(
            (pastFiveDays.toEpochMilli()).toDouble(),
            (now.toEpochMilli()).toDouble()
        )
        val limit = Limit.limit().count(500)

        // get the ZSet (Sorted Set) operations
        val zSetOps = redisOperations.opsForZSet()

        return zSetOps.reverseRangeByScore(redisKeyLocation, range, limit)
            .collectList()
            .flatMap { sessionIdsList ->

                // Detailed logging
                logger.info("Scheduled cleanup execution started.")
                logger.info("Current time (now): $now")
                logger.info("Time range start: ${Date(pastFiveDays.toEpochMilli())}")
                logger.info("Time range end: ${Date(now.toEpochMilli())}")
                logger.info("Limit count: ${limit.count}")
                logger.info("Redis key location: $redisKeyLocation")
                
                if (sessionIdsList.isNotEmpty()) {
                    logger.info("Found ${sessionIdsList.size} sessions to remove.")
                    val removal = zSetOps.remove(
                        redisKeyLocation,
                        *sessionIdsList.toTypedArray()
                    )
                    removal
                } else {
                    logger.info("No sessions found to remove.")
                    Mono.empty()
                }
            }
            .doOnSuccess {
                logger.info("Scheduled session cleanup check completed.")
            }
            .doOnError { e ->
                logger.error("Error during session cleanup check: ${e.message}")
            }
            .then()
    }
}

Console log

2024-09-01T15:47:41.781+01:00  INFO 65800 --- [BFFApplication] [xecutorLoop-3-2] c.f.bff.auth.sessions.SessionEvicter     : Current time (now): 2024-09-01T14:46:10.273882Z

2024-09-01T15:48:11.779+01:00  INFO 65800 --- [BFFApplication] [xecutorLoop-3-5] c.f.bff.auth.sessions.SessionEvicter     : Current time (now): 2024-09-01T14:46:10.273882Z

Not sure what is going on, but my session evictor doesn't really work as it stays locked into the same time range, set when Spring Boot loads up

Upvotes: 0

Views: 42

Answers (0)

Related Questions