Skip to content

Commit ce46541

Browse files
Merge pull request #127 from leanix/feature/CID-3926/Full-scan-not-finishing-if-git-data-hub-shuts-downs-mid-full-scan
CID-3926: Full scan not finishing if git-data-hub shuts down
2 parents b0f3ae4 + bdfacb9 commit ce46541

File tree

10 files changed

+50
-33
lines changed

10 files changed

+50
-33
lines changed

src/main/kotlin/net/leanix/githubagent/GitHubAgentApplication.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import org.springframework.boot.autoconfigure.SpringBootApplication
1010
import org.springframework.boot.context.properties.EnableConfigurationProperties
1111
import org.springframework.boot.runApplication
1212
import org.springframework.cloud.openfeign.EnableFeignClients
13+
import org.springframework.scheduling.annotation.EnableScheduling
1314

1415
@SpringBootApplication
1516
@EnableFeignClients
17+
@EnableScheduling
1618
@EnableConfigurationProperties(
1719
value = [
1820
net.leanix.githubagent.config.GitHubEnterpriseProperties::class,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package net.leanix.githubagent.dto
2+
3+
class ConnectionEstablishedEvent

src/main/kotlin/net/leanix/githubagent/exceptions/Exceptions.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ class GraphQLApiException(errors: List<GraphQLClientError>) :
1414
class WebhookSecretNotSetException : RuntimeException("Webhook secret not set")
1515
class InvalidEventSignatureException : RuntimeException("Invalid event signature")
1616
class ManifestFileNotFoundException : RuntimeException("Manifest File Not Found")
17+
class UnableToSendMessageException : RuntimeException("Unable to send message to the backend")

src/main/kotlin/net/leanix/githubagent/handler/BrokerStompSessionHandler.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package net.leanix.githubagent.handler
22

3+
import net.leanix.githubagent.dto.ConnectionEstablishedEvent
34
import net.leanix.githubagent.services.WebSocketService
45
import org.slf4j.LoggerFactory
56
import org.springframework.beans.factory.annotation.Autowired
7+
import org.springframework.context.ApplicationEventPublisher
68
import org.springframework.context.annotation.Lazy
79
import org.springframework.messaging.simp.stomp.StompCommand
810
import org.springframework.messaging.simp.stomp.StompHeaders
@@ -15,6 +17,7 @@ class BrokerStompSessionHandler(
1517
private val artifactDownloadHandler: ArtifactDownloadHandler,
1618
private val repositoryGetHandler: RepositoryGetHandler,
1719
private val installationGetHandler: InstallationGetHandler,
20+
private val eventPublisher: ApplicationEventPublisher
1821
) : StompSessionHandlerAdapter() {
1922
@Lazy
2023
@Autowired
@@ -30,6 +33,7 @@ class BrokerStompSessionHandler(
3033
session.subscribe("/user/queue/message/artifact", artifactDownloadHandler)
3134
session.subscribe("/user/queue/message/repository", repositoryGetHandler)
3235
session.subscribe("/user/queue/message/installation", installationGetHandler)
36+
eventPublisher.publishEvent(ConnectionEstablishedEvent())
3337
}
3438

3539
override fun handleException(
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package net.leanix.githubagent.listener
22

3-
import net.leanix.githubagent.services.GitHubStartService
3+
import net.leanix.githubagent.services.WebSocketService
44
import org.slf4j.LoggerFactory
55
import org.springframework.boot.context.event.ApplicationReadyEvent
66
import org.springframework.context.ApplicationListener
@@ -10,13 +10,13 @@ import org.springframework.stereotype.Component
1010
@Component
1111
@Profile("!test")
1212
class ApplicationReadyListener(
13-
private val gitHubStartService: GitHubStartService,
13+
private val webSocketService: WebSocketService,
1414
) : ApplicationListener<ApplicationReadyEvent> {
1515

1616
private val logger = LoggerFactory.getLogger(this::class.java)
1717

1818
override fun onApplicationEvent(event: ApplicationReadyEvent) {
1919
logger.info("Agent started")
20-
gitHubStartService.startAgent()
20+
webSocketService.initSession()
2121
}
2222
}

src/main/kotlin/net/leanix/githubagent/services/GitHubStartService.kt

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package net.leanix.githubagent.services
22

3-
import net.leanix.githubagent.handler.BrokerStompSessionHandler
3+
import net.leanix.githubagent.dto.ConnectionEstablishedEvent
4+
import net.leanix.githubagent.exceptions.UnableToSendMessageException
45
import net.leanix.githubagent.shared.APP_NAME_TOPIC
5-
import org.slf4j.LoggerFactory
6+
import org.springframework.context.event.EventListener
67
import org.springframework.scheduling.annotation.Async
78
import org.springframework.stereotype.Service
89

@@ -13,26 +14,25 @@ class GitHubStartService(
1314
private val gitHubScanningService: GitHubScanningService,
1415
private val gitHubEnterpriseService: GitHubEnterpriseService,
1516
private val cachingService: CachingService,
16-
private val brokerStompSessionHandler: BrokerStompSessionHandler,
1717
private val syncLogService: SyncLogService
1818
) {
19+
var requireScan = true
1920

20-
private val logger = LoggerFactory.getLogger(this::class.java)
21-
21+
@SuppressWarnings("UnusedParameter")
2222
@Async
23-
fun startAgent() {
24-
webSocketService.initSession()
25-
if (!brokerStompSessionHandler.isConnected()) {
26-
logger.error("Stopping the application as the WebSocket connection could not be established.")
27-
return
28-
}
29-
kotlin.runCatching {
30-
syncLogService.sendFullScanStart(null)
31-
scanResources()
32-
}.onSuccess {
33-
syncLogService.sendFullScanSuccess()
34-
}.onFailure {
35-
syncLogService.sendFullScanFailure(it.message)
23+
@EventListener
24+
fun startAgent(connectionEstablishedEvent: ConnectionEstablishedEvent) {
25+
if (requireScan) {
26+
runCatching {
27+
requireScan = false
28+
syncLogService.sendFullScanStart(null)
29+
scanResources()
30+
}.onSuccess {
31+
syncLogService.sendFullScanSuccess()
32+
}.onFailure {
33+
if (it is UnableToSendMessageException) requireScan = true
34+
syncLogService.sendFullScanFailure(it.message)
35+
}
3636
}
3737
}
3838

src/main/kotlin/net/leanix/githubagent/services/WebSocketService.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package net.leanix.githubagent.services
22

3+
import io.github.resilience4j.retry.annotation.Retry
34
import net.leanix.githubagent.config.WebSocketClientConfig
5+
import net.leanix.githubagent.exceptions.UnableToSendMessageException
46
import net.leanix.githubagent.handler.BrokerStompSessionHandler
57
import net.leanix.githubagent.shared.TOPIC_PREFIX
68
import org.slf4j.LoggerFactory
@@ -11,6 +13,7 @@ import org.springframework.stereotype.Service
1113
class WebSocketService(
1214
private val webSocketClientConfig: WebSocketClientConfig,
1315
private val brokerStompSessionHandler: BrokerStompSessionHandler,
16+
private val cachingService: CachingService,
1417
) {
1518

1619
private val logger = LoggerFactory.getLogger(WebSocketService::class.java)
@@ -27,10 +30,15 @@ class WebSocketService(
2730
}
2831
}
2932

33+
@Retry(name = "sendMessageRetry")
3034
fun sendMessage(topic: String, data: Any) {
31-
if (!brokerStompSessionHandler.isConnected()) {
35+
if (!brokerStompSessionHandler.isConnected() && cachingService.get("runId") == null) {
3236
return
3337
}
34-
stompSession!!.send("$TOPIC_PREFIX$topic", data)
38+
runCatching {
39+
stompSession!!.send("$TOPIC_PREFIX$topic", data)
40+
}.onFailure {
41+
throw UnableToSendMessageException()
42+
}
3543
}
3644
}

src/test/kotlin/net/leanix/githubagent/listener/ApplicationReadyListenerTest.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,28 @@ package net.leanix.githubagent.listener
33
import io.mockk.every
44
import io.mockk.mockk
55
import io.mockk.verify
6-
import net.leanix.githubagent.services.GitHubStartService
6+
import net.leanix.githubagent.services.WebSocketService
77
import org.junit.jupiter.api.BeforeEach
88
import org.junit.jupiter.api.Test
99

1010
class ApplicationReadyListenerTest {
1111

12-
private lateinit var gitHubStartService: GitHubStartService
1312
private lateinit var applicationListener: ApplicationReadyListener
13+
private var webSocketService: WebSocketService = mockk()
1414

1515
@BeforeEach
1616
fun setUp() {
17-
gitHubStartService = mockk()
18-
1917
applicationListener = ApplicationReadyListener(
20-
gitHubStartService
18+
webSocketService
2119
)
2220

23-
every { gitHubStartService.startAgent() } returns Unit
21+
every { webSocketService.initSession() } returns Unit
2422
}
2523

2624
@Test
2725
fun `should start the agent process`() {
2826
applicationListener.onApplicationEvent(mockk())
2927

30-
verify { gitHubStartService.startAgent() }
28+
verify { webSocketService.initSession() }
3129
}
3230
}

src/test/kotlin/net/leanix/githubagent/services/GitHubStartServiceTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package net.leanix.githubagent.services
33
import io.mockk.every
44
import io.mockk.mockk
55
import io.mockk.verify
6+
import net.leanix.githubagent.dto.ConnectionEstablishedEvent
67
import net.leanix.githubagent.dto.GitHubAppResponse
78
import net.leanix.githubagent.handler.BrokerStompSessionHandler
89
import net.leanix.githubagent.shared.APP_NAME_TOPIC
@@ -36,7 +37,6 @@ class GitHubStartServiceTest {
3637
gitHubScanningService,
3738
gitHubEnterpriseService,
3839
cachingService,
39-
brokerStompSessionHandler,
4040
syncLogService
4141
)
4242

@@ -61,7 +61,7 @@ class GitHubStartServiceTest {
6161
gitHubAppName, mapOf(), listOf()
6262
)
6363

64-
gitHubStartService.startAgent()
64+
gitHubStartService.startAgent(ConnectionEstablishedEvent())
6565

6666
verify { webSocketService.sendMessage(APP_NAME_TOPIC, gitHubAppName) }
6767
verify { syncLogService.sendFullScanStart(any()) }

src/test/kotlin/net/leanix/githubagent/services/WebSocketServiceTests.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ class WebSocketServiceTests {
1515
private val webSocketClientConfig: WebSocketClientConfig = mockk()
1616
private val stompSession: StompSession = mockk()
1717
private val brokerStompSessionHandler: BrokerStompSessionHandler = mockk()
18+
private val cachingService: CachingService = mockk()
1819

1920
@BeforeEach
2021
fun setUp() {
21-
webSocketService = WebSocketService(webSocketClientConfig, brokerStompSessionHandler)
22+
webSocketService = WebSocketService(webSocketClientConfig, brokerStompSessionHandler, cachingService)
2223
}
2324

2425
@Test

0 commit comments

Comments
 (0)