Skip to content

Commit 39fc624

Browse files
Merge pull request #130 from leanix/feature/CID-3864/Make-the-full-scan-only-run-once
CID-3864: Make full scan run only once
2 parents ef77c00 + 5c725c1 commit 39fc624

File tree

7 files changed

+98
-32
lines changed

7 files changed

+98
-32
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package net.leanix.githubagent.dto
2+
3+
enum class FullScanStatus {
4+
IN_PROGRESS,
5+
FINISHED,
6+
}

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

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

33
import net.leanix.githubagent.services.CachingService
4-
import net.leanix.githubagent.services.GitHubStartService
4+
import net.leanix.githubagent.services.FullScanService
55
import org.slf4j.LoggerFactory
66
import org.springframework.beans.factory.annotation.Autowired
77
import org.springframework.beans.factory.annotation.Value
@@ -16,7 +16,7 @@ class FullScanHandler(
1616
@Lazy @Autowired
1717
private val cachingService: CachingService,
1818
@Lazy @Autowired
19-
private val gitHubStartService: GitHubStartService,
19+
private val fullScanService: FullScanService,
2020
@Value("\${webhookEventService.waitingTime}") private val waitingTime: Long,
2121
) : StompFrameHandler {
2222

@@ -33,8 +33,8 @@ class FullScanHandler(
3333
logger.info("A full scan is already in progress, waiting for it to finish.")
3434
Thread.sleep(waitingTime)
3535
}
36-
GitHubStartService.requireScan = true
37-
gitHubStartService.verifyAndStartScan()
36+
FullScanService.requireScan = true
37+
fullScanService.verifyAndStartScan()
3838
}
3939
}
4040
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package net.leanix.githubagent.scheduler
2+
3+
import net.leanix.githubagent.dto.FullScanStatus
4+
import net.leanix.githubagent.services.CachingService
5+
import net.leanix.githubagent.services.WebSocketService
6+
import org.springframework.scheduling.annotation.Scheduled
7+
import org.springframework.stereotype.Component
8+
9+
@Component
10+
class FullScanStatusScheduler(
11+
private val webSocketService: WebSocketService,
12+
private val cachingService: CachingService,
13+
) {
14+
15+
@Scheduled(fixedDelay = 30000, initialDelay = 30000)
16+
fun sendFullScanStatus() {
17+
val runId = cachingService.get("runId")
18+
val status = if (runId != null) FullScanStatus.IN_PROGRESS else FullScanStatus.FINISHED
19+
webSocketService.sendMessage("/fullScanStatus", status.name)
20+
}
21+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package net.leanix.githubagent.services
2+
3+
import net.leanix.githubagent.exceptions.UnableToSendMessageException
4+
import org.springframework.stereotype.Service
5+
6+
@Service
7+
class FullScanService(
8+
private val gitHubScanningService: GitHubScanningService,
9+
private val syncLogService: SyncLogService
10+
) {
11+
12+
companion object {
13+
var requireScan: Boolean = false
14+
}
15+
16+
fun verifyAndStartScan() {
17+
if (requireScan) {
18+
runCatching {
19+
requireScan = false
20+
syncLogService.sendFullScanStart(null)
21+
gitHubScanningService.scanGitHubResources()
22+
}.onSuccess {
23+
syncLogService.sendFullScanSuccess()
24+
}.onFailure {
25+
if (it is UnableToSendMessageException) requireScan = true
26+
syncLogService.sendFullScanFailure(it.message)
27+
}
28+
}
29+
}
30+
}
Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package net.leanix.githubagent.services
22

33
import net.leanix.githubagent.dto.ConnectionEstablishedEvent
4-
import net.leanix.githubagent.exceptions.UnableToSendMessageException
54
import net.leanix.githubagent.shared.APP_NAME_TOPIC
65
import org.springframework.context.event.EventListener
76
import org.springframework.scheduling.annotation.Async
@@ -11,44 +10,23 @@ import org.springframework.stereotype.Service
1110
class GitHubStartService(
1211
private val githubAuthenticationService: GitHubAuthenticationService,
1312
private val webSocketService: WebSocketService,
14-
private val gitHubScanningService: GitHubScanningService,
1513
private val gitHubEnterpriseService: GitHubEnterpriseService,
1614
private val cachingService: CachingService,
17-
private val syncLogService: SyncLogService
1815
) {
19-
companion object {
20-
var requireScan: Boolean = true
21-
}
2216

2317
@SuppressWarnings("UnusedParameter")
2418
@Async
2519
@EventListener
2620
fun startAgent(connectionEstablishedEvent: ConnectionEstablishedEvent) {
27-
verifyAndStartScan()
28-
}
29-
30-
fun verifyAndStartScan() {
31-
if (requireScan) {
32-
runCatching {
33-
requireScan = false
34-
syncLogService.sendFullScanStart(null)
35-
scanResources()
36-
}.onSuccess {
37-
syncLogService.sendFullScanSuccess()
38-
}.onFailure {
39-
if (it is UnableToSendMessageException) requireScan = true
40-
syncLogService.sendFullScanFailure(it.message)
41-
}
42-
}
21+
githubAuthenticationService.generateAndCacheJwtToken()
22+
sendAppName()
4323
}
4424

45-
private fun scanResources() {
46-
githubAuthenticationService.generateAndCacheJwtToken()
25+
private fun sendAppName() {
4726
val jwt = cachingService.get("jwtToken") as String
4827
webSocketService.sendMessage(
4928
APP_NAME_TOPIC,
5029
gitHubEnterpriseService.getGitHubApp(jwt).slug
5130
)
52-
gitHubScanningService.scanGitHubResources()
5331
}
5432
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package net.leanix.githubagent.scheduler
2+
3+
import io.mockk.every
4+
import io.mockk.mockk
5+
import io.mockk.verify
6+
import net.leanix.githubagent.dto.FullScanStatus
7+
import net.leanix.githubagent.services.CachingService
8+
import net.leanix.githubagent.services.WebSocketService
9+
import org.junit.jupiter.api.Test
10+
11+
class FullScanStatusSchedulerTest {
12+
13+
private val webSocketService: WebSocketService = mockk(relaxed = true)
14+
private val cachingService: CachingService = mockk()
15+
private val scheduler = FullScanStatusScheduler(webSocketService, cachingService)
16+
17+
@Test
18+
fun `should send IN_PROGRESS status when runId is present`() {
19+
every { cachingService.get("runId") } returns "someRunId"
20+
21+
scheduler.sendFullScanStatus()
22+
23+
verify { webSocketService.sendMessage("/fullScanStatus", FullScanStatus.IN_PROGRESS.name) }
24+
}
25+
26+
@Test
27+
fun `should send FINISHED status when runId is null`() {
28+
every { cachingService.get("runId") } returns null
29+
30+
scheduler.sendFullScanStatus()
31+
32+
verify { webSocketService.sendMessage("/fullScanStatus", FullScanStatus.FINISHED.name) }
33+
}
34+
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,8 @@ class GitHubStartServiceTest {
3434
gitHubStartService = GitHubStartService(
3535
githubAuthenticationService,
3636
webSocketService,
37-
gitHubScanningService,
3837
gitHubEnterpriseService,
3938
cachingService,
40-
syncLogService
4139
)
4240

4341
every { webSocketService.initSession() } returns Unit
@@ -64,6 +62,5 @@ class GitHubStartServiceTest {
6462
gitHubStartService.startAgent(ConnectionEstablishedEvent())
6563

6664
verify { webSocketService.sendMessage(APP_NAME_TOPIC, gitHubAppName) }
67-
verify { syncLogService.sendFullScanStart(any()) }
6865
}
6966
}

0 commit comments

Comments
 (0)