-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(indexing): schedule indexing based on cron trigger
- Loading branch information
Showing
11 changed files
with
149 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...tion/src/main/kotlin/com/roche/ambassador/configuration/properties/SchedulerProperties.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.roche.ambassador.configuration.properties | ||
|
||
import java.time.Duration | ||
|
||
data class SchedulerProperties( | ||
val enabled: Boolean = false, | ||
val cron: String = "0 0 14 ? * SUN", | ||
val lockFor: Duration = Duration.ofMinutes(30) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...application/src/main/kotlin/com/roche/ambassador/indexing/ScheduledIndexingInitializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.roche.ambassador.indexing | ||
|
||
import com.roche.ambassador.extensions.LoggerDelegate | ||
import com.roche.ambassador.security.RunAsTechnicalUser | ||
import kotlinx.coroutines.runBlocking | ||
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock | ||
import org.springframework.scheduling.annotation.Scheduled | ||
|
||
internal open class ScheduledIndexingInitializer(private val indexingService: IndexingService) { | ||
|
||
companion object { | ||
private val log by LoggerDelegate() | ||
} | ||
|
||
@Scheduled(cron = "\${ambassador.indexer.scheduler.cron}") | ||
@SchedulerLock(name = " indexing", lockAtLeastFor = "\${ambassador.indexer.scheduler.lockFor}", lockAtMostFor = "1h") | ||
@RunAsTechnicalUser | ||
open fun triggerScheduling() { | ||
log.info("Triggering scheduled indexing") | ||
// purposely run blocking, cause it will get async in downstream when indexing is triggered successfully | ||
runBlocking { | ||
try { | ||
indexingService.reindex() | ||
} catch (ex: IndexingAlreadyStartedException) { | ||
log.warn("Unable to start scheduled indexing because it is already running") | ||
} | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
ambassador-application/src/main/kotlin/com/roche/ambassador/security/RunAsTechnicalUser.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.roche.ambassador.security | ||
|
||
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
annotation class RunAsTechnicalUser |
44 changes: 44 additions & 0 deletions
44
...dor-application/src/main/kotlin/com/roche/ambassador/security/RunAsTechnicalUserAspect.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.roche.ambassador.security | ||
|
||
import org.aspectj.lang.ProceedingJoinPoint | ||
import org.aspectj.lang.annotation.Around | ||
import org.aspectj.lang.annotation.Aspect | ||
import org.springframework.security.authentication.AbstractAuthenticationToken | ||
import org.springframework.security.core.context.ReactiveSecurityContextHolder | ||
import org.springframework.stereotype.Component | ||
|
||
@Aspect | ||
@Component | ||
internal open class RunAsTechnicalUserAspect { | ||
|
||
companion object { | ||
val technicalUser = AmbassadorUser( | ||
"Technical User", | ||
"_admin", "[email protected]", | ||
mapOf(), listOf("ROLE_ADMIN", "ROLE_USER") | ||
) | ||
private val technicalUserToken = TechnicalUserToken(technicalUser) | ||
} | ||
|
||
private class TechnicalUserToken(private val ambassadorUser: AmbassadorUser) : AbstractAuthenticationToken(ambassadorUser.authorities) { | ||
override fun getCredentials(): Any = "__none__" | ||
|
||
override fun getPrincipal(): AmbassadorUser = ambassadorUser | ||
|
||
} | ||
|
||
@Around("@annotation(com.roche.ambassador.security.RunAsTechnicalUser)") | ||
open fun logExecutionTime(joinPoint: ProceedingJoinPoint): Any? { | ||
val savedContext = ReactiveSecurityContextHolder.getContext() | ||
ReactiveSecurityContextHolder.withAuthentication(technicalUserToken) | ||
try { | ||
return joinPoint.proceed() | ||
} finally { | ||
if (savedContext != null) { | ||
ReactiveSecurityContextHolder.withSecurityContext(savedContext) | ||
} else { | ||
ReactiveSecurityContextHolder.clearContext() | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
ambassador-storage/src/main/resources/db/migration/V202204181912__add_shedlock_tables.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CREATE TABLE shedlock( | ||
name VARCHAR(64) NOT NULL, | ||
lock_until TIMESTAMP NOT NULL, | ||
locked_at TIMESTAMP NOT NULL, | ||
locked_by VARCHAR(255) NOT NULL, | ||
PRIMARY KEY (name) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters