Skip to content

Commit

Permalink
CID-2143: Change code to finish run before start the full scan
Browse files Browse the repository at this point in the history
  • Loading branch information
henriqamaral committed Nov 15, 2023
1 parent 6ee9db1 commit 64f3e9a
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package net.leanix.vsm.githubbroker.connector.application

import net.leanix.vsm.githubbroker.connector.adapter.feign.FeignRunStatusProvider
import net.leanix.vsm.githubbroker.connector.adapter.feign.data.RunState
import net.leanix.vsm.githubbroker.connector.adapter.feign.data.UpdateRunStateRequest
import net.leanix.vsm.githubbroker.connector.domain.Assignment
import net.leanix.vsm.githubbroker.shared.Constants
import net.leanix.vsm.githubbroker.shared.cache.AssignmentCache
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service

@Service
class RunService(
private val assignmentService: AssignmentService,
private val runStatusProvider: FeignRunStatusProvider,
) {

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

fun getAssignments(): List<Assignment>? {
kotlin.runCatching {
finishRuns("Finish runs").let {
AssignmentCache.deleteAll()
val assignments = assignmentService.getAssignments()
println(assignments.first().workspaceId)
AssignmentCache.addAll(assignments)
return assignments
}
}.onFailure {
logger.error(it.message)
logger.error("Failed to get initial state. No assignment found for this workspace id")
}
return null
}

fun finishRuns(message: String) {
runCatching {
if (AssignmentCache.getAll().isEmpty()) {
logger.info("$message before receiving any assignment")
} else {
AssignmentCache.getAll().values.forEach { assignment ->
runStatusProvider.updateRunStatus(
assignment.runId.toString(),
UpdateRunStateRequest(
state = RunState.FINISHED,
workspaceId = assignment.workspaceId.toString(),
connector = Constants.GITHUB_ENTERPRISE_CONNECTOR,
orgName = assignment.organizationName,
message = message,
),
)
}
}
}.onFailure {
logger.error("Error finishing run: ", it)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package net.leanix.vsm.githubbroker.connector.runner

import net.leanix.vsm.githubbroker.connector.application.AssignmentService
import net.leanix.vsm.githubbroker.connector.application.RepositoriesService
import net.leanix.vsm.githubbroker.connector.application.RunService
import net.leanix.vsm.githubbroker.connector.application.WebhookService
import net.leanix.vsm.githubbroker.connector.domain.Assignment
import net.leanix.vsm.githubbroker.connector.domain.CommandEventAction
import net.leanix.vsm.githubbroker.connector.domain.CommandProvider
import net.leanix.vsm.githubbroker.logs.application.LoggingService
Expand All @@ -25,7 +24,7 @@ import org.springframework.stereotype.Component
)
@Component
class InitialStateRunner(
private val assignmentService: AssignmentService,
private val runService: RunService,
private val repositoriesService: RepositoriesService,
private val webhookService: WebhookService,
private val loggingService: LoggingService,
Expand All @@ -36,7 +35,7 @@ class InitialStateRunner(
logger.info("Started get initial state")

runCatching {
getAssignments()?.forEach { assignment ->
runService.getAssignments()?.forEach { assignment ->
repositoriesService.getAllRepositories(assignment)
logger.info("Initializing webhooks registration steps")
webhookService.registerWebhook(assignment)
Expand All @@ -59,14 +58,4 @@ class InitialStateRunner(
}
}
}
private fun getAssignments(): List<Assignment>? {
kotlin.runCatching {
val assignments = assignmentService.getAssignments()
AssignmentCache.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,36 +1,18 @@
package net.leanix.vsm.githubbroker.connector.runner

import jakarta.annotation.PreDestroy
import net.leanix.vsm.githubbroker.connector.adapter.feign.FeignRunStatusProvider
import net.leanix.vsm.githubbroker.connector.adapter.feign.data.RunState
import net.leanix.vsm.githubbroker.connector.adapter.feign.data.UpdateRunStateRequest
import net.leanix.vsm.githubbroker.shared.Constants.GITHUB_ENTERPRISE_CONNECTOR
import net.leanix.vsm.githubbroker.shared.cache.AssignmentCache
import net.leanix.vsm.githubbroker.connector.application.RunService
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service

@Service
class ShutdownService(private val runStatusProvider: FeignRunStatusProvider) {
class ShutdownService(private val runService: RunService) {

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

@PreDestroy
fun onDestroy() {
if (AssignmentCache.getAll().isEmpty()) {
logger.info("Shutting down github broker before receiving any assignment")
} else {
AssignmentCache.getAll().values.forEach { assignment ->
runStatusProvider.updateRunStatus(
assignment.runId.toString(),
UpdateRunStateRequest(
state = RunState.FINISHED,
workspaceId = assignment.workspaceId.toString(),
connector = GITHUB_ENTERPRISE_CONNECTOR,
orgName = assignment.organizationName,
message = "Gracefully stopped github enterprise"
)
)
}
}
logger.info("Shutting down github broker")
runService.finishRuns("Gracefully stopped github enterprise")
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.leanix.vsm.githubbroker.connector.scheduler

import net.leanix.vsm.githubbroker.connector.application.RepositoriesService
import net.leanix.vsm.githubbroker.connector.application.RunService
import net.leanix.vsm.githubbroker.connector.domain.CommandEventAction
import net.leanix.vsm.githubbroker.connector.domain.CommandProvider
import net.leanix.vsm.githubbroker.shared.cache.AssignmentCache
Expand All @@ -11,16 +12,17 @@ import org.springframework.stereotype.Component

@Component
class ScheduleRepositories(
private val runService: RunService,
private val repositoriesService: RepositoriesService,
private val commandProvider: CommandProvider,
) {
private val logger: Logger = LoggerFactory.getLogger(ScheduleRepositories::class.java)

@Scheduled(cron = "\${leanix.vsm.schedule:0 0 4 * * *}")
@Scheduled(cron = "\${leanix.vsm.schedule:0 */3 * ? * *}")
fun getAllRepositories() {
logger.info("Started schedule")
runCatching {
AssignmentCache.getAll().values.forEach { assignment ->
runService.getAssignments()?.forEach { assignment ->
repositoriesService.getAllRepositories(assignment)
}
}.onSuccess {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.github.tomakehurst.wiremock.client.WireMock
import com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor
import com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor
import com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo
import net.leanix.vsm.githubbroker.shared.cache.AssignmentCache
import org.awaitility.kotlin.await
import org.junit.jupiter.api.Test
import org.springframework.boot.test.context.SpringBootTest
Expand All @@ -15,6 +16,7 @@ class InitialStateTest {

@Test
fun `it should get the assignments`() {
AssignmentCache.deleteAll()
await.untilAsserted {
WireMock.verify(
1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@ import io.mockk.every
import io.mockk.mockk
import io.mockk.slot
import io.mockk.verify
import net.leanix.vsm.githubbroker.connector.adapter.feign.FeignRunStatusProvider
import net.leanix.vsm.githubbroker.connector.adapter.feign.data.RunState.FINISHED
import net.leanix.vsm.githubbroker.connector.adapter.feign.data.UpdateRunStateRequest
import net.leanix.vsm.githubbroker.connector.application.RunService
import net.leanix.vsm.githubbroker.connector.domain.Assignment
import net.leanix.vsm.githubbroker.shared.Constants.GITHUB_ENTERPRISE_CONNECTOR
import net.leanix.vsm.githubbroker.shared.cache.AssignmentCache
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import java.util.*

class ShutdownServiceTest {
private val runStatusProvider: FeignRunStatusProvider = mockk(relaxed = true)
private val shutdownService = ShutdownService(runStatusProvider)
private val runService: RunService = mockk(relaxed = true)
private val shutdownService = ShutdownService(runService)

@BeforeEach
fun setup() {
Expand All @@ -26,34 +23,19 @@ class ShutdownServiceTest {
}

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

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

@Test
fun `test onDestroy with assignments`() {
fun `test onDestroy should call finish runs`() {
val assignment = Assignment(UUID.randomUUID(), UUID.randomUUID(), UUID.randomUUID(), null, "mock-org")
AssignmentCache.addAll(listOf(assignment))

val runStateSlot = slot<UpdateRunStateRequest>()
val finishMessageSlot = slot<String>()

every { runStatusProvider.updateRunStatus(any(), capture(runStateSlot)) } answers { }
every { runService.finishRuns(capture(finishMessageSlot)) } answers { }

shutdownService.onDestroy()

verify {
runStatusProvider.updateRunStatus(
eq(assignment.runId.toString()),
match {
it.state == FINISHED &&
it.workspaceId == assignment.workspaceId.toString() &&
it.connector == GITHUB_ENTERPRISE_CONNECTOR &&
it.orgName == assignment.organizationName &&
it.message == "Gracefully stopped github enterprise"
}
runService.finishRuns(
eq("Gracefully stopped github enterprise"),
)
}
}
Expand Down

0 comments on commit 64f3e9a

Please sign in to comment.