Skip to content

Commit

Permalink
remove the scheduling to k8s job that will use an actuator endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mrFlick72 committed Nov 23, 2024
1 parent 74a9ba2 commit 0b58f8d
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 120 deletions.
3 changes: 2 additions & 1 deletion local-environment/http-client.env.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
8 changes: 8 additions & 0 deletions local-environment/request.http
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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")

}

Expand Down Expand Up @@ -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<String, String>) = RedisLockService(redisTemplate)
fun databaseTtlEntryCleanJobEndPoint(databaseTtlEntryCleanJob: DatabaseTtlEntryCleanJob) =
DatabaseTtlEntryCleanJobEndPoint(databaseTtlEntryCleanJob)
}
Original file line number Diff line number Diff line change
@@ -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<Unit> {
databaseTtlEntryCleanJob.execute()
return ResponseEntity.noContent().build()
}
}
42 changes: 0 additions & 42 deletions src/main/kotlin/com/vauthenticator/server/job/LockService.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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)
Expand Down

This file was deleted.

0 comments on commit 0b58f8d

Please sign in to comment.