From 0b58f8dafb62c3e4c05e13a2c6eb97ecf2280f7a Mon Sep 17 00:00:00 2001 From: mrflick72 Date: Sat, 23 Nov 2024 23:01:41 +0100 Subject: [PATCH] remove the scheduling to k8s job that will use an actuator endpoint --- local-environment/http-client.env.json | 3 +- local-environment/request.http | 8 ++++ .../server/job/DatabaseTtlEntryCleanJob.kt | 35 ++++------------ .../job/DatabaseTtlEntryCleanJobEndPoint.kt | 17 ++++++++ .../vauthenticator/server/job/LockService.kt | 42 ------------------- .../job/DatabaseTtlEntryCleanJobTest.kt | 21 +--------- .../server/job/RedisLockServiceTest.kt | 31 -------------- 7 files changed, 37 insertions(+), 120 deletions(-) create mode 100644 src/main/kotlin/com/vauthenticator/server/job/DatabaseTtlEntryCleanJobEndPoint.kt delete mode 100644 src/main/kotlin/com/vauthenticator/server/job/LockService.kt delete mode 100644 src/test/kotlin/com/vauthenticator/server/job/RedisLockServiceTest.kt diff --git a/local-environment/http-client.env.json b/local-environment/http-client.env.json index f0f9caa8..e5583fe3 100644 --- a/local-environment/http-client.env.json +++ b/local-environment/http-client.env.json @@ -1,5 +1,6 @@ { "LOCAL": { - "host": "http://local.management.vauthenticator.com:9090" + "host": "http://local.management.vauthenticator.com:9090", + "actuatorHost": "http://local.management.vauthenticator.com:9091" } } \ No newline at end of file diff --git a/local-environment/request.http b/local-environment/request.http index 4b675909..06ae8efe 100644 --- a/local-environment/request.http +++ b/local-environment/request.http @@ -1,3 +1,11 @@ +### actuator root +GET {{actuatorHost}}/actuator + + +### acgtuator database-clean-up job +POST {{actuatorHost}}/actuator/database-clean-up + + ### login page GET {{host}}/login diff --git a/src/main/kotlin/com/vauthenticator/server/job/DatabaseTtlEntryCleanJob.kt b/src/main/kotlin/com/vauthenticator/server/job/DatabaseTtlEntryCleanJob.kt index 17c54836..9d535c5c 100644 --- a/src/main/kotlin/com/vauthenticator/server/job/DatabaseTtlEntryCleanJob.kt +++ b/src/main/kotlin/com/vauthenticator/server/job/DatabaseTtlEntryCleanJob.kt @@ -1,44 +1,29 @@ package com.vauthenticator.server.job import org.slf4j.LoggerFactory -import org.springframework.beans.factory.annotation.Value import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Profile -import org.springframework.data.redis.core.RedisTemplate import org.springframework.jdbc.core.JdbcTemplate import org.springframework.jdbc.core.queryForList -import org.springframework.scheduling.annotation.EnableScheduling -import org.springframework.scheduling.annotation.Scheduled -import org.springframework.stereotype.Repository import org.springframework.transaction.annotation.Transactional import java.time.Clock @Transactional class DatabaseTtlEntryCleanJob( private val jdbcTemplate: JdbcTemplate, - private val lockTtl: Long, - private val lockService: LockService, private val clock: Clock ) { private val logger = LoggerFactory.getLogger(DatabaseTtlEntryCleanJob::class.java) - @Scheduled(cron = "\${scheduled.database-cleanup.cron}") fun execute() { - try { - logger.info("Try to Schedule") + logger.info("Job Running") + val now = clock.instant().epochSecond - lockService.lock(lockTtl) - logger.info("Job Running") - val now = clock.instant().epochSecond - - deleteOldTicket(now) - deleteOldKeys(now) - logger.info("Job Completed") - } finally { - lockService.unlock() - } + deleteOldTicket(now) + deleteOldKeys(now) + logger.info("Job Completed") } @@ -70,17 +55,15 @@ class DatabaseTtlEntryCleanJob( @Profile("!kms") -@EnableScheduling @Configuration(proxyBeanMethods = false) -class DatabaseTtlEntryCleanJobConfig { +class DatabaseTtlEntryCleanJobConfig() { @Bean fun databaseTtlEntryCleanJob( - @Value("\${scheduled.database-cleanup.lock-ttl}") lockTtl: Long, - lockService: LockService, jdbcTemplate: JdbcTemplate - ) = DatabaseTtlEntryCleanJob(jdbcTemplate, lockTtl, lockService, Clock.systemUTC()) + ) = DatabaseTtlEntryCleanJob(jdbcTemplate, Clock.systemUTC()) @Bean - fun lockService(redisTemplate: RedisTemplate) = RedisLockService(redisTemplate) + fun databaseTtlEntryCleanJobEndPoint(databaseTtlEntryCleanJob: DatabaseTtlEntryCleanJob) = + DatabaseTtlEntryCleanJobEndPoint(databaseTtlEntryCleanJob) } \ No newline at end of file diff --git a/src/main/kotlin/com/vauthenticator/server/job/DatabaseTtlEntryCleanJobEndPoint.kt b/src/main/kotlin/com/vauthenticator/server/job/DatabaseTtlEntryCleanJobEndPoint.kt new file mode 100644 index 00000000..20104a4e --- /dev/null +++ b/src/main/kotlin/com/vauthenticator/server/job/DatabaseTtlEntryCleanJobEndPoint.kt @@ -0,0 +1,17 @@ +package com.vauthenticator.server.job + +import org.springframework.boot.actuate.endpoint.annotation.Endpoint +import org.springframework.boot.actuate.endpoint.annotation.WriteOperation +import org.springframework.http.ResponseEntity + +@Endpoint(id = "database-clean-up") +class DatabaseTtlEntryCleanJobEndPoint( + private val databaseTtlEntryCleanJob: DatabaseTtlEntryCleanJob +) { + + @WriteOperation + fun cleanUp(): ResponseEntity { + databaseTtlEntryCleanJob.execute() + return ResponseEntity.noContent().build() + } +} diff --git a/src/main/kotlin/com/vauthenticator/server/job/LockService.kt b/src/main/kotlin/com/vauthenticator/server/job/LockService.kt deleted file mode 100644 index 9786dd05..00000000 --- a/src/main/kotlin/com/vauthenticator/server/job/LockService.kt +++ /dev/null @@ -1,42 +0,0 @@ -package com.vauthenticator.server.job - -import org.slf4j.LoggerFactory -import org.springframework.data.redis.core.RedisTemplate -import java.util.concurrent.TimeUnit - - -interface LockService { - - fun lock(timeout: Long) - - fun unlock() - -} - -class RedisLockService( - private val redisTemplate: RedisTemplate -) : LockService { - - private val logger = LoggerFactory.getLogger(RedisLockService::class.java) - - override fun lock(timeout: Long) { - if (!acquireLockWith(timeout)) { - logger.info("lock already acquired") - Thread.sleep(timeout) - } else { - logger.info("lock acquired") - } - } - - private fun acquireLockWith(timeout: Long): Boolean { - logger.info("try to acquire Lock") - return redisTemplate.opsForValue() - .setIfAbsent("lockKey", "locked", timeout, TimeUnit.MILLISECONDS)!! - } - - override fun unlock() { - logger.info("lock released") - redisTemplate.opsForValue().getAndDelete("lockKey") - } - -} \ No newline at end of file diff --git a/src/test/kotlin/com/vauthenticator/server/job/DatabaseTtlEntryCleanJobTest.kt b/src/test/kotlin/com/vauthenticator/server/job/DatabaseTtlEntryCleanJobTest.kt index 591b8b88..fa8e7a4a 100644 --- a/src/test/kotlin/com/vauthenticator/server/job/DatabaseTtlEntryCleanJobTest.kt +++ b/src/test/kotlin/com/vauthenticator/server/job/DatabaseTtlEntryCleanJobTest.kt @@ -14,27 +14,16 @@ import com.vauthenticator.server.support.JdbcUtils.resetDb import com.vauthenticator.server.support.TicketFixture import com.vauthenticator.server.ticket.adapter.jdbc.JdbcTicketRepository import com.vauthenticator.server.ticket.domain.TicketId -import io.mockk.every -import io.mockk.impl.annotations.MockK -import io.mockk.junit5.MockKExtension -import io.mockk.just -import io.mockk.runs -import io.mockk.verify import org.junit.jupiter.api.Assertions.assertThrows import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test -import org.junit.jupiter.api.extension.ExtendWith import java.time.Clock import java.time.Duration import java.util.* -@ExtendWith(MockKExtension::class) class DatabaseTtlEntryCleanJobTest { - @MockK - lateinit var lockService: LockService - @BeforeEach fun setUp() { resetDb() @@ -45,7 +34,7 @@ class DatabaseTtlEntryCleanJobTest { val ticketRepository = JdbcTicketRepository(jdbcTemplate, jacksonObjectMapper()) val keyStorage = JdbcKeyStorage(jdbcTemplate, Clock.systemDefaultZone()) - val uut = DatabaseTtlEntryCleanJob(jdbcTemplate, 100, lockService, Clock.systemUTC()) + val uut = DatabaseTtlEntryCleanJob(jdbcTemplate, Clock.systemUTC()) val kid = Kid("") val anAccount = AccountTestFixture.anAccount() @@ -61,17 +50,9 @@ class DatabaseTtlEntryCleanJobTest { ) keyStorage.keyDeleteJodPlannedFor(kid, Duration.ofSeconds(-200), SIGNATURE) - every { - lockService.lock(100) - lockService.unlock() - } just runs uut.execute() - verify { - lockService.lock(100) - lockService.unlock() - } val actualTicket = ticketRepository.loadFor(TicketId("A_TICKET")) assertTrue(actualTicket.isEmpty) diff --git a/src/test/kotlin/com/vauthenticator/server/job/RedisLockServiceTest.kt b/src/test/kotlin/com/vauthenticator/server/job/RedisLockServiceTest.kt deleted file mode 100644 index f4bad567..00000000 --- a/src/test/kotlin/com/vauthenticator/server/job/RedisLockServiceTest.kt +++ /dev/null @@ -1,31 +0,0 @@ -package com.vauthenticator.server.job - -import io.mockk.every -import io.mockk.impl.annotations.MockK -import io.mockk.junit5.MockKExtension -import io.mockk.mockk -import org.junit.jupiter.api.Test -import org.junit.jupiter.api.extension.ExtendWith -import org.springframework.data.redis.core.RedisTemplate -import org.springframework.data.redis.core.ValueOperations -import java.util.concurrent.TimeUnit - -@ExtendWith(MockKExtension::class) -class RedisLockServiceTest { - - @MockK - lateinit var redisTemplate: RedisTemplate - - @Test - fun `when lock and unlock`() { - val uut = RedisLockService(redisTemplate) - - every { redisTemplate.opsForValue() } returns mockk> { - every { setIfAbsent("lockKey", "locked", 100, TimeUnit.MILLISECONDS) } returns true - every { getAndDelete("lockKey") } returns "lockKey" - } - - uut.lock(100) - uut.unlock() - } -} \ No newline at end of file