Skip to content

Commit

Permalink
using newConfigAvailable in heartbeat to refresh AssignmentsCache
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmed-ali-55 committed Aug 23, 2023
1 parent 54860fc commit 2c35d5b
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.leanix.vsm.gitlab.broker.connector.adapter.feign

import net.leanix.vsm.gitlab.broker.connector.domain.GitLabAssignment
import net.leanix.vsm.gitlab.broker.connector.domain.GitLabHeartbeatResponse
import net.leanix.vsm.gitlab.broker.shared.auth.adapter.feign.config.MtmFeignClientConfiguration
import org.springframework.cloud.openfeign.FeignClient
import org.springframework.web.bind.annotation.GetMapping
Expand All @@ -17,6 +18,6 @@ interface VsmClient {
@GetMapping("/gitlab-on-prem/assignments")
fun getAssignments(): List<GitLabAssignment>

@PutMapping("/gitlab-on-prem/health/heartbeat")
fun heartbeat(@RequestParam("runId") runId: String): String
@PutMapping("/gitlab-on-prem/heartbeat")
fun heartbeat(@RequestParam("runId") runId: String): GitLabHeartbeatResponse
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package net.leanix.vsm.gitlab.broker.connector.applicaiton

import net.leanix.vsm.gitlab.broker.connector.domain.AssignmentProvider
import net.leanix.vsm.gitlab.broker.connector.domain.GitLabAssignment
import net.leanix.vsm.gitlab.broker.shared.cache.AssignmentsCache
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service

Expand All @@ -12,11 +13,18 @@ class AssignmentService(

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

fun getAssignments(): List<GitLabAssignment> {
return assignmentProvider.getAssignments().onFailure {
logger.error("Failed to retrieve assignment list: ", it)
}.onSuccess {
logger.info("Assignment list retrieved with success with ${it.size} assignments")
}.getOrThrow()
fun getAssignments(): List<GitLabAssignment>? {
kotlin.runCatching {
val assignments = assignmentProvider.getAssignments().onFailure {
logger.error("Failed to retrieve assignment list: ", it)
}.onSuccess {
logger.info("Assignment list retrieved with success with ${it.size} assignments")
}.getOrThrow()

AssignmentsCache.deleteAll()
AssignmentsCache.addAll(assignments)
return assignments
}
return null
}
}
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
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.leanix.vsm.gitlab.broker.connector.runner

import net.leanix.vsm.gitlab.broker.connector.applicaiton.AssignmentService
import net.leanix.vsm.gitlab.broker.connector.domain.GitLabAssignment
import net.leanix.vsm.gitlab.broker.shared.cache.AssignmentsCache
import org.slf4j.Logger
import org.slf4j.LoggerFactory
Expand All @@ -19,10 +18,10 @@ class InitialStateRunner(
override fun run(args: ApplicationArguments?) {
logger.info("Started to get initial state")
runCatching {
getAssignments()?.forEach { assignment ->
assignmentService.getAssignments()?.forEach { assignment ->
logger.info(
"Received assignment for ${assignment.connectorConfiguration.orgName} " +
"with configuration id: ${assignment.configurationId} and with run id: ${assignment.runId}"
"with configuration id: ${assignment.configurationId} and with run id: ${assignment.runId}"
)
}
}.onSuccess {
Expand All @@ -31,15 +30,4 @@ class InitialStateRunner(
logger.error("Failed to get initial state", e)
}
}

private fun getAssignments(): List<GitLabAssignment>? {
kotlin.runCatching {
val assignments = assignmentService.getAssignments()
AssignmentsCache.addAll(assignments)
return assignments
}.onFailure {
logger.error("Failed to get initial state. No assignment found for this workspace id")
}
return null
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
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}")
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
}
}
}
}
12 changes: 12 additions & 0 deletions src/main/resources/application-local.yaml
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: ${leanix.base-url}/mtm/v1

server:
port: 8082
6 changes: 4 additions & 2 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
leanix:
base-url: https://${LEANIX_DOMAIN}/services
heartbeat:
interval: ${LEANIX_HEARTBEAT_INTERVAL:300000} # 5 minutes
vsm:
connector:
api-user-token: ${LEANIX_TECHNICAL_USER_TOKEN}
events-broker:
base-url: ${leanix.vsm.base-url}/vsm-events-broker
# events-broker:
# base-url: ${leanix.vsm.base-url}/vsm-events-broker
auth:
access-token-uri: ${leanix.base-url}/mtm/v1

Expand Down
6 changes: 4 additions & 2 deletions src/test/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
leanix:
base-url: http://localhost:${wiremock.server.port:6666}/services
heartbeat:
interval: ${LEANIX_HEARTBEAT_INTERVAL:300000} # 5 minutes
vsm:
connector:
api-user-token: ${LEANIX_TECHNICAL_USER_TOKEN}
events-broker:
base-url: http://localhost:${wiremock.server.port:6666}
# events-broker:
# base-url: http://localhost:${wiremock.server.port:6666}
auth:
access-token-uri: ${leanix.base-url}/mtm/v1

0 comments on commit 2c35d5b

Please sign in to comment.