Skip to content

Commit

Permalink
CID-1751: Add run update provider
Browse files Browse the repository at this point in the history
  • Loading branch information
henriqamaral committed Aug 28, 2023
1 parent 1d855db commit 24fd095
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package net.leanix.vsm.gitlab.broker.connector.adapter.feign

import net.leanix.vsm.gitlab.broker.connector.adapter.feign.data.RunState
import net.leanix.vsm.gitlab.broker.connector.adapter.feign.data.UpdateRunStateRequest
import net.leanix.vsm.gitlab.broker.connector.domain.GitLabAssignment
import net.leanix.vsm.gitlab.broker.connector.domain.RunProvider
import net.leanix.vsm.gitlab.broker.shared.Constants.GITLAB_ENTERPRISE_CONNECTOR
import org.springframework.stereotype.Component

@Component
class RunFeignProvider(private val vsmClient: VsmClient) : RunProvider {

override fun updateRun(assignment: GitLabAssignment, runState: RunState, message: String?) {
vsmClient.updateRunState(
runId = assignment.runId,
UpdateRunStateRequest(
state = RunState.FINISHED,
workspaceId = assignment.workspaceId.toString(),
connector = GITLAB_ENTERPRISE_CONNECTOR,
orgName = assignment.connectorConfiguration.orgName,
message = message
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package net.leanix.vsm.gitlab.broker.connector.adapter.feign

import net.leanix.vsm.gitlab.broker.connector.adapter.feign.data.CommandRequest
import net.leanix.vsm.gitlab.broker.connector.adapter.feign.data.ServiceRequest
import net.leanix.vsm.gitlab.broker.connector.adapter.feign.data.UpdateRunStateRequest
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.Constants.EVENT_TYPE_HEADER
Expand All @@ -13,6 +14,7 @@ import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestHeader
import org.springframework.web.bind.annotation.RequestParam
import java.util.UUID

@FeignClient(
name = "vsmClient",
Expand All @@ -37,4 +39,10 @@ interface VsmClient {
fun sendCommand(
@RequestBody commandRequest: CommandRequest,
)

@PutMapping("/run/status")
fun updateRunState(
@RequestParam("runId") runId: UUID,
@RequestBody runState: UpdateRunStateRequest,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.leanix.vsm.gitlab.broker.connector.adapter.feign.data

import com.fasterxml.jackson.annotation.JsonIgnoreProperties

@JsonIgnoreProperties(ignoreUnknown = true)
data class UpdateRunStateRequest(
val state: RunState,
val workspaceId: String? = null,
val connector: String? = null,
val orgName: String? = null,
val message: String? = null,
val region: String? = null
)

enum class RunState {
RUNNING,
FINISHED,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package net.leanix.vsm.gitlab.broker.connector.domain

import net.leanix.vsm.gitlab.broker.connector.adapter.feign.data.RunState

interface RunProvider {

fun updateRun(assignment: GitLabAssignment, runState: RunState, message: String?)
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
package net.leanix.vsm.gitlab.broker.connector.runner

import jakarta.annotation.PreDestroy
import net.leanix.vsm.gitlab.broker.connector.adapter.feign.data.RunState
import net.leanix.vsm.gitlab.broker.connector.domain.RunProvider
import net.leanix.vsm.gitlab.broker.shared.cache.AssignmentsCache
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service

@Service
class ShutdownService {
class ShutdownService(private val runProvider: RunProvider) {

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

@PreDestroy
fun onDestroy() {
logger.info("Shutting down gitlab on-prem")
if (AssignmentsCache.getAll().isEmpty()) {
logger.info("Shutting down github broker before receiving any assignment")
} else {
AssignmentsCache.getAll().values.forEach { assignment ->
runProvider.updateRun(
runState = RunState.FINISHED,
assignment = assignment,
message = "gracefully stopped GitHub broker"
)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
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.adapter.feign.data.RunState
import net.leanix.vsm.gitlab.broker.connector.application.AssignmentService
import net.leanix.vsm.gitlab.broker.connector.application.InitialStateService
import net.leanix.vsm.gitlab.broker.connector.domain.RunProvider
import net.leanix.vsm.gitlab.broker.shared.cache.AssignmentsCache
import org.slf4j.LoggerFactory
import org.springframework.scheduling.annotation.Scheduled
Expand All @@ -12,22 +14,36 @@ import org.springframework.stereotype.Component
class HeartbeatScheduler(
private val vsmClient: VsmClient,
private val assignmentService: AssignmentService,
private val initialStateService: InitialStateService
private val initialStateService: InitialStateService,
private val runProvider: RunProvider
) {

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

@Scheduled(fixedRateString = "\${leanix.heartbeat.interval}")
fun heartbeat() {
AssignmentsCache.getAll().values.forEach { assignment ->
val assignments = AssignmentsCache.getAll()
var newConfigAvailable = false
assignments.values.forEach { assignment ->
logger.info("Sending heartbeat for runId: ${assignment.runId}")
vsmClient.heartbeat(assignment.runId.toString())
.takeIf { it.newConfigAvailable }
?.also {
assignmentService.getAssignments()?.let {
initialStateService.initState(it)
}
newConfigAvailable = true
}
}

if (newConfigAvailable) {
assignmentService.getAssignments()?.let {
initialStateService.initState(it)
}
assignments.values.forEach {
runProvider.updateRun(
it,
RunState.FINISHED,
"Finished run after update"
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ enum class LogStatus {
STARTED,
IN_PROGRESS,
FAILED,
SUCCESSFUL
SUCCESSFUL,
FINISHED
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package net.leanix.vsm.gitlab.broker.connector.runner

import io.mockk.clearAllMocks
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import net.leanix.vsm.gitlab.broker.connector.adapter.feign.data.RunState
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.RunProvider
import net.leanix.vsm.gitlab.broker.shared.cache.AssignmentsCache
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import java.util.*

class ShutdownServiceTest {

private val runProvider: RunProvider = mockk(relaxed = true)
private val shutdownService = ShutdownService(runProvider)

@BeforeEach
fun setup() {
clearAllMocks()
AssignmentsCache.deleteAll()
}

@Test
fun `test onDestroy with empty assignment cache`() {
shutdownService.onDestroy()

// Assert no interactions with the run status provider
verify(exactly = 0) { runProvider.updateRun(any(), any(), any()) }
}

@Test
fun `test onDestroy with assignments`() {
val assignment = GitLabAssignment(
UUID.randomUUID(),
UUID.randomUUID(),
UUID.randomUUID(),
GitLabConfiguration(
orgName = "orgName"
)
)
AssignmentsCache.addAll(listOf(assignment))

// val runStateSlot = slot<UpdateRunStateRequest>()

every { runProvider.updateRun(any(), any(), any()) } answers { }

shutdownService.onDestroy()

verify {
runProvider.updateRun(
eq(assignment),
eq(RunState.FINISHED),
eq("gracefully stopped GitHub broker")
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import net.leanix.vsm.gitlab.broker.connector.application.InitialStateService
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.connector.domain.RunProvider
import net.leanix.vsm.gitlab.broker.shared.cache.AssignmentsCache
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
Expand All @@ -21,7 +22,8 @@ class HeartbeatSchedulerTest {
private val vsmClient = mock(VsmClient::class.java)
private val assignmentService = mock(AssignmentService::class.java)
private val initialStateService = mock(InitialStateService::class.java)
private val subject = HeartbeatScheduler(vsmClient, assignmentService, initialStateService)
private val runProvider = mock(RunProvider::class.java)
private val subject = HeartbeatScheduler(vsmClient, assignmentService, initialStateService, runProvider)
private val runId: UUID = randomUUID()

@BeforeEach
Expand Down

0 comments on commit 24fd095

Please sign in to comment.