@@ -11,20 +11,27 @@ import com.roche.ambassador.model.project.Project
11
11
import com.roche.ambassador.model.source.ProjectSources
12
12
import com.roche.ambassador.storage.project.ProjectEntity
13
13
import com.roche.ambassador.storage.project.ProjectEntityRepository
14
+ import com.roche.ambassador.storage.project.ProjectStatisticsHistory
15
+ import com.roche.ambassador.storage.project.ProjectStatisticsHistoryRepository
14
16
import kotlinx.coroutines.CoroutineScope
15
17
import kotlinx.coroutines.launch
16
18
import org.springframework.stereotype.Service
19
+ import org.springframework.transaction.PlatformTransactionManager
17
20
import org.springframework.transaction.annotation.Propagation
18
21
import org.springframework.transaction.annotation.Transactional
22
+ import org.springframework.transaction.support.TransactionTemplate
19
23
20
24
@Service
21
25
internal class AnalysisService (
22
26
private val scorecardConfiguration : ScorecardConfiguration ,
23
27
private val projectEntityRepository : ProjectEntityRepository ,
28
+ private val projectStatisticsHistoryRepository : ProjectStatisticsHistoryRepository ,
24
29
private val projectSources : ProjectSources ,
30
+ platformTransactionManager : PlatformTransactionManager ,
25
31
concurrencyProvider : ConcurrencyProvider
26
32
) {
27
33
34
+ private val transactionTemplate: TransactionTemplate = TransactionTemplate (platformTransactionManager)
28
35
private val analysisScope: CoroutineScope = CoroutineScope (concurrencyProvider.getSupportingDispatcher())
29
36
private val calculator: ScorecardCalculator = ScorecardCalculator (scorecardConfiguration)
30
37
@@ -45,7 +52,7 @@ internal class AnalysisService(
45
52
}
46
53
47
54
// TODO run async
48
- @Transactional(propagation = Propagation . REQUIRES_NEW )
55
+ @Transactional
49
56
fun analyzeAll () {
50
57
val total = projectEntityRepository.countAllBySubscribed(true )
51
58
val progressMonitor: ProgressMonitor = LoggingProgressMonitor (total, 2 , AnalysisService ::class )
@@ -57,16 +64,19 @@ internal class AnalysisService(
57
64
}
58
65
59
66
private fun analyze (entity : ProjectEntity , progressMonitor : ProgressMonitor ) {
60
- analysisScope.launch {
61
- try {
62
- entity.project = analyze(entity.project)
63
- entity.updateScore(entity.project)
64
- entity.recordStatistics()
65
- projectEntityRepository.save(entity)
66
- progressMonitor.success()
67
- } catch (e: Throwable ) {
68
- log.error(" Failed to analyze project '{}' (id={})." , entity.project.fullName, entity.project.id, e)
69
- progressMonitor.failure()
67
+ transactionTemplate.execute {
68
+ analysisScope.launch {
69
+ try {
70
+ entity.project = analyze(entity.project)
71
+ entity.updateScore(entity.project)
72
+ val savedEntity = projectEntityRepository.save(entity)
73
+ val historyEntry = ProjectStatisticsHistory .from(savedEntity)
74
+ projectStatisticsHistoryRepository.save(historyEntry)
75
+ progressMonitor.success()
76
+ } catch (e: Throwable ) {
77
+ log.error(" Failed to analyze project '{}' (id={})." , entity.project.fullName, entity.project.id, e)
78
+ progressMonitor.failure()
79
+ }
70
80
}
71
81
}
72
82
}
0 commit comments