Skip to content

Commit af31cea

Browse files
committed
ensure active
1 parent 0eee358 commit af31cea

File tree

11 files changed

+72
-52
lines changed

11 files changed

+72
-52
lines changed

ide-common/src/main/kotlin/org/digma/intellij/plugin/discovery/FileDiscoveryInfoBuilder.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import com.intellij.openapi.components.service
55
import com.intellij.openapi.diagnostic.thisLogger
66
import com.intellij.openapi.project.Project
77
import com.intellij.openapi.vfs.VirtualFile
8+
import kotlinx.coroutines.ensureActive
89
import org.digma.intellij.plugin.discovery.model.FileDiscoveryInfo
910
import org.digma.intellij.plugin.log.Log
1011
import org.digma.intellij.plugin.psi.LanguageServiceProvider
12+
import kotlin.coroutines.coroutineContext
1113

1214
@Service(Service.Level.PROJECT)
1315
class FileDiscoveryInfoBuilder(private val project: Project) {
@@ -30,6 +32,8 @@ class FileDiscoveryInfoBuilder(private val project: Project) {
3032
return FileDiscoveryInfo(file)
3133
}
3234

35+
coroutineContext.ensureActive()
36+
3337
Log.trace(logger, project, "LanguageService for file {} is {}", file.url, languageService.javaClass.name)
3438

3539
val discoveryProvider = languageService.getDiscoveryProvider()

jvm-common/src/main/kotlin/org/digma/intellij/plugin/idea/AbstractJvmLanguageService.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ abstract class AbstractJvmLanguageService(protected val project: Project, protec
116116
coroutineContext.ensureActive()
117117
return psiFile?.let {
118118
suspendableRetry {
119+
coroutineContext.ensureActive()
119120
codeObjectDiscovery.buildDocumentInfo(project, it, virtualFile.url, getLanguage())
120121
}
121122
}

jvm-common/src/main/kotlin/org/digma/intellij/plugin/idea/discovery/AbstractCodeObjectDiscovery.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ abstract class AbstractCodeObjectDiscovery(private val spanDiscovery: AbstractSp
102102
coroutineContext.ensureActive()
103103
val documentInfo = DocumentInfo(fileUrl, methodInfoMap, language.id)
104104
collectEndpoints(project, psiFile, documentInfo)
105+
coroutineContext.ensureActive()
105106
return documentInfo
106107
} catch (e: Throwable) {
107108
//may also be CancellationException
@@ -114,6 +115,7 @@ abstract class AbstractCodeObjectDiscovery(private val spanDiscovery: AbstractSp
114115
open suspend fun collectEndpoints(project: Project, psiFile: PsiFile, documentInfo: DocumentInfo) {
115116
val endpointDiscoveryList = getEndpointDiscoveryForLanguage(project, psiFile)
116117
for (framework in endpointDiscoveryList) {
118+
coroutineContext.ensureActive()
117119
suspendableRetry {
118120
framework.endpointDiscovery(psiFile, documentInfo)
119121
}
@@ -130,6 +132,7 @@ abstract class AbstractCodeObjectDiscovery(private val spanDiscovery: AbstractSp
130132

131133
open suspend fun collectSpans(project: Project, psiFile: PsiFile): Collection<SpanInfo> {
132134
return suspendableRetry {
135+
coroutineContext.ensureActive()
133136
spanDiscovery.discoverSpans(project, psiFile)
134137
}
135138
}
@@ -144,12 +147,14 @@ abstract class AbstractCodeObjectDiscovery(private val spanDiscovery: AbstractSp
144147
spans: Collection<SpanInfo>
145148
) {
146149
classes.forEach { uClass ->
150+
coroutineContext.ensureActive()
147151
if (isRelevantClassType(uClass)) {
148152
val methods: Collection<UMethod> = readAction {
149153
getMethodsInClass(uClass)
150154
}
151155
methods.forEach { uMethod ->
152156
suspendableRetry {
157+
coroutineContext.ensureActive()
153158
readAction {
154159
val id: String = createMethodCodeObjectId(uMethod)
155160
val name: String = uMethod.name

jvm-common/src/main/kotlin/org/digma/intellij/plugin/idea/discovery/MicrometerTracingFramework.kt

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ import com.intellij.psi.PsiMethod
99
import com.intellij.psi.SmartPsiElementPointer
1010
import com.intellij.psi.search.GlobalSearchScope
1111
import com.intellij.psi.util.PsiTreeUtil
12+
import kotlinx.coroutines.ensureActive
1213
import org.digma.intellij.plugin.common.firstPart
1314
import org.digma.intellij.plugin.common.suspendableRetry
1415
import org.digma.intellij.plugin.idea.psi.java.JavaLanguageUtils
15-
import org.digma.intellij.plugin.log.Log
1616
import org.digma.intellij.plugin.model.discovery.SpanInfo
1717
import org.digma.intellij.plugin.psi.PsiUtils
18+
import kotlin.coroutines.coroutineContext
1819

1920
class MicrometerTracingFramework {
2021

@@ -89,6 +90,8 @@ class MicrometerTracingFramework {
8990
spanInfos.addAll(newSpanAnnotationSpans)
9091
}
9192

93+
coroutineContext.ensureActive()
94+
9295
suspendableRetry {
9396
val observedAnnotationSpans = observedAnnotationSpanDiscovery(project, psiFile)
9497
spanInfos.addAll(observedAnnotationSpans)
@@ -109,16 +112,13 @@ class MicrometerTracingFramework {
109112
val annotatedMethods = psiPointers.getPsiClassPointer(project, NEW_SPAN_FQN)?.let { newSpanClassPointer ->
110113
findAnnotatedMethods(project, newSpanClassPointer) { GlobalSearchScope.fileScope(psiFile) }
111114
}
112-
115+
coroutineContext.ensureActive()
113116
annotatedMethods?.forEach { annotatedMethod: SmartPsiElementPointer<PsiMethod> ->
117+
coroutineContext.ensureActive()
114118
smartReadAction(project) {
115119
annotatedMethod.element?.let {
116-
val spanInfo = try {
120+
val spanInfo =
117121
getSpanInfoFromNewSpanAnnotatedMethod(it)
118-
} catch (e: Throwable) {
119-
Log.warnWithException(logger, e, "Error in newSpanAnnotationSpanDiscovery for method {}", it.name)
120-
null
121-
}
122122
spanInfo?.let { si ->
123123
spanInfos.add(si)
124124
}
@@ -142,17 +142,14 @@ class MicrometerTracingFramework {
142142
val annotatedMethods = psiPointers.getPsiClassPointer(project, OBSERVED_FQN)?.let { observedAnnotationClassPointer ->
143143
findAnnotatedMethods(project, observedAnnotationClassPointer) { GlobalSearchScope.fileScope(psiFile) }
144144
}
145-
145+
coroutineContext.ensureActive()
146146
annotatedMethods?.forEach { annotatedMethod: SmartPsiElementPointer<PsiMethod> ->
147-
147+
coroutineContext.ensureActive()
148148
smartReadAction(project) {
149149
annotatedMethod.element?.let {
150-
val spanInfo = try {
150+
val spanInfo =
151151
getSpanInfoFromObservedAnnotatedMethod(it)
152-
} catch (e: Throwable) {
153-
Log.warnWithException(logger, e, "Error in observedAnnotationSpanDiscovery for method {}", it.name)
154-
null
155-
}
152+
156153
spanInfo?.let { si ->
157154
spanInfos.add(si)
158155
}
@@ -170,7 +167,7 @@ class MicrometerTracingFramework {
170167
val containingFile = PsiTreeUtil.getParentOfType(psiMethod, PsiFile::class.java)
171168
val containingClass = psiMethod.containingClass
172169

173-
//withSpanAnnotation,containingFile and containingClass must not be null because we found this annotation in a search.
170+
//withSpanAnnotation, containingFile and containingClass must not be null because we found this annotation in a search.
174171
// a method in java must have a containing class. (psiMethod.getContainingClass may return null because
175172
// it supports other languages like groovy and kotlin)
176173
if (newSpanAnnotation != null && containingFile != null && containingClass != null) {
@@ -181,11 +178,11 @@ class MicrometerTracingFramework {
181178
val spanName = tmpSpanName ?: (containingClass.name + "." + psiMethod.name)
182179
return SpanInfo(
183180
JavaLanguageUtils.createSpanIdFromInstLibraryAndSpanName(MICRO_METER_INST_LIB, spanName),
184-
spanName, methodId, containingFileUri,psiMethod.textOffset
181+
spanName, methodId, containingFileUri, psiMethod.textOffset
185182
)
186183
}
187184

188-
//if here then we couldn't completely discover the span
185+
//if here, then we couldn't completely discover the span
189186
return null
190187
}
191188

@@ -194,7 +191,7 @@ class MicrometerTracingFramework {
194191
val containingFile = PsiTreeUtil.getParentOfType(psiMethod, PsiFile::class.java)
195192
//TODO: see if class itself is annotated with OBSERVED_FQN - then all its methods as well
196193

197-
// observedAnnotationOnMethod,containingFile must not be null because we found this annotation in a search.
194+
// observedAnnotationOnMethod, containingFile must not be null because we found this annotation in a search.
198195
if (observedAnnotationOnMethod != null && containingFile != null) {
199196
val methodId = createPsiMethodCodeObjectId(psiMethod)
200197
val containingFileUri = PsiUtils.psiFileToUri(containingFile)
@@ -210,11 +207,11 @@ class MicrometerTracingFramework {
210207

211208
return SpanInfo(
212209
JavaLanguageUtils.createSpanIdFromInstLibraryAndSpanName(OBSERVED_INST_LIB, spanName),
213-
spanName, methodId, containingFileUri,psiMethod.textOffset
210+
spanName, methodId, containingFileUri, psiMethod.textOffset
214211
)
215212
}
216213

217-
//if here then we couldn't completely discover the span
214+
//if here, then we couldn't completely discover the span
218215
return null
219216
}
220217

jvm-common/src/main/kotlin/org/digma/intellij/plugin/idea/discovery/endpoint/AbstractJaxrsFrameworkEndpointDiscover.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import com.intellij.psi.search.impl.VirtualFileEnumeration
2020
import com.intellij.psi.search.searches.AnnotatedElementsSearch
2121
import com.intellij.psi.search.searches.OverridingMethodsSearch
2222
import com.intellij.psi.util.PsiTreeUtil
23+
import kotlinx.coroutines.ensureActive
2324
import org.digma.intellij.plugin.common.SearchScopeProvider
2425
import org.digma.intellij.plugin.common.TextRangeUtils
2526
import org.digma.intellij.plugin.common.isProjectValid
@@ -35,6 +36,7 @@ import org.digma.intellij.plugin.model.discovery.EndpointFramework
3536
import org.digma.intellij.plugin.model.discovery.EndpointInfo
3637
import org.digma.intellij.plugin.psi.PsiUtils
3738
import java.util.Locale
39+
import kotlin.coroutines.coroutineContext
3840

3941

4042
abstract class AbstractJaxrsFrameworkEndpointDiscover(private val project: Project, jaxRsPackageName: String) : EndpointDiscovery() {
@@ -80,6 +82,7 @@ abstract class AbstractJaxrsFrameworkEndpointDiscover(private val project: Proje
8082

8183
val endpointInfos = mutableListOf<EndpointInfo>()
8284

85+
coroutineContext.ensureActive()
8386
val allClassesInFile: List<SmartPsiElementPointer<PsiClass>> =
8487
smartReadAction(project) {
8588
val classes = PsiTreeUtil.findChildrenOfType(psiFile, PsiClass::class.java)
@@ -88,13 +91,14 @@ abstract class AbstractJaxrsFrameworkEndpointDiscover(private val project: Proje
8891

8992

9093
allClassesInFile.forEach { psiClassPointer ->
91-
94+
coroutineContext.ensureActive()
9295
val psiClass = readAction { psiClassPointer.element }
9396

9497
psiClass?.let { cls ->
9598
val methodsInClass = readAction { getMethodsInClass(cls) }
9699

97100
methodsInClass.forEach { psiMethod ->
101+
coroutineContext.ensureActive()
98102
smartReadAction(project) {
99103
val candidateMethods = mutableSetOf<PsiMethod>()
100104
val methodPathAnnotation = findNearestAnnotation(psiMethod, jaxRsPathAnnotationStr)

jvm-common/src/main/kotlin/org/digma/intellij/plugin/idea/discovery/endpoint/GrpcFrameworkEndpointDiscovery.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.intellij.psi.PsiMethod
1010
import com.intellij.psi.SmartPointerManager
1111
import com.intellij.psi.SmartPsiElementPointer
1212
import com.intellij.psi.search.searches.ClassInheritorsSearch
13+
import kotlinx.coroutines.ensureActive
1314
import org.digma.intellij.plugin.common.SearchScopeProvider
1415
import org.digma.intellij.plugin.common.TextRangeUtils
1516
import org.digma.intellij.plugin.common.isProjectValid
@@ -22,6 +23,7 @@ import org.digma.intellij.plugin.idea.discovery.toFileUri
2223
import org.digma.intellij.plugin.log.Log
2324
import org.digma.intellij.plugin.model.discovery.EndpointFramework
2425
import org.digma.intellij.plugin.model.discovery.EndpointInfo
26+
import kotlin.coroutines.coroutineContext
2527

2628
private const val BINDABLE_SERVICE_ANNOTATION_STR = "io.grpc.BindableService"
2729
private const val DIGMA_UNKNOWN_SERVICE_NAME = "Digma.Unknown.Grpc.Service"
@@ -51,7 +53,7 @@ open class GrpcFrameworkEndpointDiscovery(private val project: Project) : Endpoi
5153
}
5254

5355
val result = mutableListOf<EndpointInfo>()
54-
56+
coroutineContext.ensureActive()
5557
val grpcServerInheritorsPointers: List<SmartPsiElementPointer<PsiClass>> = smartReadAction(project) {
5658
getBindableServiceAnnotationClass()?.let { psiClass ->
5759
val inheritors = ClassInheritorsSearch.search(psiClass, searchScopeProvider.get(), true)
@@ -61,6 +63,7 @@ open class GrpcFrameworkEndpointDiscovery(private val project: Project) : Endpoi
6163

6264

6365
grpcServerInheritorsPointers.forEach { currGrpcInheritorPointer ->
66+
coroutineContext.ensureActive()
6467
readAction {
6568
val psiClass = currGrpcInheritorPointer.element
6669
psiClass?.takeIf { psiClass.isValid && !isBaseClass(psiClass) }?.let { grpcInheritor ->

jvm-common/src/main/kotlin/org/digma/intellij/plugin/idea/discovery/endpoint/MicronautFrameworkEndpointDiscovery.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import com.intellij.psi.PsiClass
1212
import com.intellij.psi.PsiMethod
1313
import com.intellij.psi.SmartPsiElementPointer
1414
import com.intellij.util.text.VersionComparatorUtil
15+
import kotlinx.coroutines.ensureActive
1516
import org.digma.intellij.plugin.common.SearchScopeProvider
1617
import org.digma.intellij.plugin.common.TextRangeUtils
1718
import org.digma.intellij.plugin.idea.deps.ModulesDepsService.Companion.toUnifiedCoordinates
@@ -25,6 +26,7 @@ import org.digma.intellij.plugin.model.discovery.EndpointFramework
2526
import org.digma.intellij.plugin.model.discovery.EndpointInfo
2627
import java.util.Arrays
2728
import java.util.Locale
29+
import kotlin.coroutines.coroutineContext
2830

2931
private const val CONTROLLER_ANNOTATION_STR = "io.micronaut.http.annotation.Controller"
3032
private const val HTTP_DELETE_ANNOTATION_STR = "io.micronaut.http.annotation.Delete"
@@ -84,13 +86,18 @@ class MicronautFrameworkEndpointDiscovery(private val project: Project) : Endpoi
8486

8587
val resultEndpoints = mutableListOf<EndpointInfo>()
8688

89+
coroutineContext.ensureActive()
90+
8791
HTTP_METHODS_ANNOTATION_STR_LIST.forEach { annotationFqn ->
92+
coroutineContext.ensureActive()
93+
8894
psiPointers.getPsiClass(project, annotationFqn)?.let {
8995
val annotatedMethods = psiPointers.getPsiClassPointer(project, annotationFqn)?.let { annotationClassPointer ->
9096
findAnnotatedMethods(project, annotationClassPointer, searchScopeProvider)
9197
}
9298

9399
annotatedMethods?.forEach { annotatedMethod: SmartPsiElementPointer<PsiMethod> ->
100+
coroutineContext.ensureActive()
94101
//start read access for each annotated method
95102
readAction {
96103
buildEndpointsForAnnotatedMethod(resultEndpoints, annotationFqn, annotatedMethod)

jvm-common/src/main/kotlin/org/digma/intellij/plugin/idea/discovery/endpoint/SpringBootFrameworkEndpointDiscovery.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.intellij.psi.PsiAnnotation
77
import com.intellij.psi.PsiClass
88
import com.intellij.psi.PsiMethod
99
import com.intellij.psi.SmartPsiElementPointer
10+
import kotlinx.coroutines.ensureActive
1011
import org.digma.intellij.plugin.common.SearchScopeProvider
1112
import org.digma.intellij.plugin.common.TextRangeUtils
1213
import org.digma.intellij.plugin.idea.discovery.PsiPointers
@@ -18,6 +19,7 @@ import org.digma.intellij.plugin.idea.psi.java.JavaLanguageUtils
1819
import org.digma.intellij.plugin.model.discovery.EndpointFramework
1920
import org.digma.intellij.plugin.model.discovery.EndpointInfo
2021
import java.util.Locale
22+
import kotlin.coroutines.coroutineContext
2123

2224
private const val CONTROLLER_ANNOTATION_STR = "org.springframework.stereotype.Controller"
2325
private const val REST_CONTROLLER_ANNOTATION_STR = "org.springframework.web.bind.annotation.RestController"
@@ -82,15 +84,17 @@ class SpringBootFrameworkEndpointDiscovery(private val project: Project) : Endpo
8284

8385
//the iterations catch exceptions and continue to the next iterations so not the whole
8486
// process is crashed
85-
87+
coroutineContext.ensureActive()
8688
HTTP_METHODS_ANNOTATION_STR_LIST.forEach { annotationFqn ->
89+
coroutineContext.ensureActive()
8790

8891
psiPointers.getPsiClass(project, annotationFqn)?.let {
8992
val annotatedMethods = psiPointers.getPsiClassPointer(project, annotationFqn)?.let { annotationClassPointer ->
9093
findAnnotatedMethods(project, annotationClassPointer, searchScopeProvider)
9194
}
9295

9396
annotatedMethods?.forEach { annotatedMethod: SmartPsiElementPointer<PsiMethod> ->
97+
coroutineContext.ensureActive()
9498
//start read access for each annotated method
9599
readAction {
96100
buildEndpointsForAnnotatedMethod(resultEndpoints, annotationFqn, annotatedMethod)

0 commit comments

Comments
 (0)