-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CID:1287 Adds heartbeat and graceful shutdown
- Loading branch information
1 parent
fb2ef48
commit 407ee76
Showing
12 changed files
with
205 additions
and
8 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
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,4 +1,4 @@ | ||
# These owners will be the default owners for everything in | ||
# the repo. Unless a later match takes precedence, | ||
# they will be requested for review when someone opens a pull request. | ||
* @alfredo-mfaria @aravindmetku @geoandri @henriqamaral @mohamedlajmileanix | ||
* @leanix/team-cider |
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
13 changes: 13 additions & 0 deletions
13
...main/kotlin/net/leanix/vsm/githubbroker/connector/adapter/feign/FeignRunStatusProvider.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,13 @@ | ||
package net.leanix.vsm.githubbroker.connector.adapter.feign | ||
|
||
import net.leanix.vsm.githubbroker.connector.adapter.feign.data.UpdateRunStateRequest | ||
import net.leanix.vsm.githubbroker.connector.domain.RunStatusProvider | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class FeignRunStatusProvider(private val vsmClient: VsmClient) : RunStatusProvider { | ||
|
||
override fun updateRunStatus(runId: String, runState: UpdateRunStateRequest) { | ||
vsmClient.updateRunState(runId, runState) | ||
} | ||
} |
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
23 changes: 23 additions & 0 deletions
23
.../kotlin/net/leanix/vsm/githubbroker/connector/adapter/feign/data/UpdateRunStateRequest.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,23 @@ | ||
package net.leanix.vsm.githubbroker.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 { | ||
QUEUED, | ||
DELETED, | ||
RUNNING, | ||
FINISHED, | ||
FAILED, | ||
FINISHED_FOR_LIVE, | ||
LIVE | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/net/leanix/vsm/githubbroker/connector/domain/RunStatusProvider.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,7 @@ | ||
package net.leanix.vsm.githubbroker.connector.domain | ||
|
||
import net.leanix.vsm.githubbroker.connector.adapter.feign.data.UpdateRunStateRequest | ||
|
||
interface RunStatusProvider { | ||
fun updateRunStatus(runId: String, runState: UpdateRunStateRequest) | ||
} |
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
36 changes: 36 additions & 0 deletions
36
src/main/kotlin/net/leanix/vsm/githubbroker/connector/runner/ShutdownService.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,36 @@ | ||
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 org.slf4j.LoggerFactory | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class ShutdownService(private val runStatusProvider: FeignRunStatusProvider) { | ||
|
||
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" | ||
) | ||
) | ||
} | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/kotlin/net/leanix/vsm/githubbroker/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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package net.leanix.vsm.githubbroker.connector.scheduler | ||
|
||
import net.leanix.vsm.githubbroker.connector.adapter.feign.VsmClient | ||
import net.leanix.vsm.githubbroker.shared.cache.AssignmentCache | ||
import org.springframework.scheduling.annotation.Scheduled | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class HeartbeatScheduler( | ||
private val vsmClient: VsmClient | ||
) { | ||
|
||
@Scheduled(fixedRate = 300000) // 5 minutes | ||
fun heartbeat() { | ||
AssignmentCache.getAll().values.forEach { assigment -> vsmClient.heartbeat(assigment.runId.toString()) } | ||
} | ||
} |
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
24 changes: 24 additions & 0 deletions
24
src/main/kotlin/net/leanix/vsm/githubbroker/shared/cache/AssignmentCache.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,24 @@ | ||
package net.leanix.vsm.githubbroker.shared.cache | ||
|
||
import net.leanix.vsm.githubbroker.connector.domain.Assignment | ||
|
||
object AssignmentCache { | ||
|
||
private val assigmentCache: MutableMap<String, Assignment> = mutableMapOf() | ||
|
||
fun addAll(newAssignments: List<Assignment>) { | ||
newAssignments.forEach { assignment -> assigmentCache[assignment.organizationName] = assignment } | ||
} | ||
|
||
fun get(key: String): Assignment? { | ||
return assigmentCache[key] | ||
} | ||
|
||
fun getAll(): Map<String, Assignment> { | ||
return assigmentCache | ||
} | ||
|
||
fun deleteAll() { | ||
assigmentCache.clear() | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/test/kotlin/net/leanix/vsm/githubbroker/connector/runner/ShutdownServiceTest.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,60 @@ | ||
package net.leanix.vsm.githubbroker.connector.runner | ||
|
||
import io.mockk.clearAllMocks | ||
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.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) | ||
|
||
@BeforeEach | ||
fun setup() { | ||
clearAllMocks() | ||
AssignmentCache.deleteAll() | ||
} | ||
|
||
@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`() { | ||
val assignment = Assignment(UUID.randomUUID(), UUID.randomUUID(), UUID.randomUUID(), null, "mock-org") | ||
AssignmentCache.addAll(listOf(assignment)) | ||
|
||
val runStateSlot = slot<UpdateRunStateRequest>() | ||
|
||
every { runStatusProvider.updateRunStatus(any(), capture(runStateSlot)) } 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" | ||
} | ||
) | ||
} | ||
} | ||
} |