-
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.
Using newConfigAvailable in heartbeat to refresh AssignmentsCache (#5)
* using newConfigAvailable in heartbeat to refresh AssignmentsCache * reformatting and fixing detekt errors * fixing access-token-uri in application-local.yaml * minor yaml changes * reverting test application.yaml changes * adding tests for HeartbeatScheduler * reformatting HeartbeatSchedulerTest
- Loading branch information
1 parent
54860fc
commit d9e4c9c
Showing
9 changed files
with
104 additions
and
26 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
6 changes: 6 additions & 0 deletions
6
src/main/kotlin/net/leanix/vsm/gitlab/broker/connector/domain/GitLabHeartbeatResponse.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,6 @@ | ||
package net.leanix.vsm.gitlab.broker.connector.domain | ||
|
||
data class GitLabHeartbeatResponse( | ||
val status: String, | ||
val newConfigAvailable: Boolean | ||
) |
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
19 changes: 14 additions & 5 deletions
19
src/main/kotlin/net/leanix/vsm/gitlab/broker/connector/scheduler/HeartbeatScheduler.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 |
---|---|---|
@@ -1,23 +1,32 @@ | ||
package net.leanix.vsm.gitlab.broker.connector.scheduler | ||
|
||
import net.leanix.vsm.gitlab.broker.connector.adapter.feign.VsmClient | ||
import net.leanix.vsm.gitlab.broker.connector.applicaiton.AssignmentService | ||
import net.leanix.vsm.gitlab.broker.shared.cache.AssignmentsCache | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.scheduling.annotation.Scheduled | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class HeartbeatScheduler( | ||
private val vsmClient: VsmClient | ||
private val vsmClient: VsmClient, | ||
private val assignmentService: AssignmentService | ||
) { | ||
|
||
private val logger = LoggerFactory.getLogger(HeartbeatScheduler::class.java) | ||
|
||
@Scheduled(fixedRate = 300000) // 5 minute | ||
@Scheduled(fixedRateString = "\${leanix.heartbeat.interval}") | ||
@Suppress("ForbiddenComment") | ||
fun heartbeat() { | ||
AssignmentsCache.getAll().values.forEach { assigment -> | ||
logger.info("Sending heartbeat for runId: ${assigment.runId}") | ||
vsmClient.heartbeat(assigment.runId.toString()) | ||
AssignmentsCache.getAll().values.forEach { assignment -> | ||
logger.info("Sending heartbeat for runId: ${assignment.runId}") | ||
vsmClient.heartbeat(assignment.runId.toString()) | ||
.takeIf { it.newConfigAvailable } | ||
?.also { | ||
assignmentService.getAssignments() | ||
// TODO: here we need to re-fetch everything for this config | ||
// remove @Suppress from function definition | ||
} | ||
} | ||
} | ||
} |
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,12 @@ | ||
leanix: | ||
base-url: http://localhost:8080 | ||
heartbeat: | ||
interval: ${LEANIX_HEARTBEAT_INTERVAL:300000} # 5 minutes | ||
vsm: | ||
events-broker: | ||
base-url: http://localhost:8080 | ||
auth: | ||
access-token-uri: https://test-app-1.leanix.net/services/mtm/v1 | ||
|
||
server: | ||
port: 8082 |
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
50 changes: 50 additions & 0 deletions
50
src/test/kotlin/net/leanix/vsm/gitlab/broker/connector/scheduler/HeartbeatSchedulerTest.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,50 @@ | ||
package net.leanix.vsm.gitlab.broker.connector.scheduler | ||
|
||
import net.leanix.vsm.gitlab.broker.connector.adapter.feign.VsmClient | ||
import net.leanix.vsm.gitlab.broker.connector.applicaiton.AssignmentService | ||
import net.leanix.vsm.gitlab.broker.connector.domain.GitLabAssignment | ||
import net.leanix.vsm.gitlab.broker.connector.domain.GitLabConfiguration | ||
import net.leanix.vsm.gitlab.broker.connector.domain.GitLabHeartbeatResponse | ||
import net.leanix.vsm.gitlab.broker.shared.cache.AssignmentsCache | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.mockito.Mockito.mock | ||
import org.mockito.Mockito.verify | ||
import org.mockito.Mockito.verifyNoInteractions | ||
import org.mockito.Mockito.`when` | ||
import java.util.UUID | ||
import java.util.UUID.randomUUID | ||
|
||
class HeartbeatSchedulerTest { | ||
|
||
private val vsmClient = mock(VsmClient::class.java) | ||
private val assignmentService = mock(AssignmentService::class.java) | ||
private val subject = HeartbeatScheduler(vsmClient, assignmentService) | ||
private val runId: UUID = randomUUID() | ||
|
||
@BeforeEach | ||
fun setupAssignmentCache() { | ||
AssignmentsCache.deleteAll() | ||
AssignmentsCache.addAll(listOf(getGitlabAssignment())) | ||
} | ||
|
||
@Test | ||
fun `should re-fetch assignments when new config available`() { | ||
`when`(vsmClient.heartbeat(runId.toString())).thenReturn(GitLabHeartbeatResponse("OK", true)) | ||
|
||
subject.heartbeat() | ||
|
||
verify(assignmentService).getAssignments() | ||
} | ||
|
||
@Test | ||
fun `should not re-fetch assignments when no new config available`() { | ||
`when`(vsmClient.heartbeat(runId.toString())).thenReturn(GitLabHeartbeatResponse("OK", false)) | ||
|
||
subject.heartbeat() | ||
|
||
verifyNoInteractions(assignmentService) | ||
} | ||
|
||
private fun getGitlabAssignment() = GitLabAssignment(runId, randomUUID(), randomUUID(), GitLabConfiguration("")) | ||
} |
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