[Gradle|Sync] cleanup: use CancellationToken instead of CancellationTokenSource

CancellationToken is an abstraction that represents the process cancellation state. But it can't be canceled from the consumer's code. So we need to use the CancellationTokenSource in the controller, and CancellationToken in the process that the controller should control.

GitOrigin-RevId: 0b7a17fc72a90db7ea1ee57ee6e045dbedfe953a
This commit is contained in:
Sergei Vorobyov
2024-03-20 23:27:34 +00:00
committed by intellij-monorepo-bot
parent 0631be0f50
commit c8af8fea88
8 changed files with 49 additions and 100 deletions
@@ -62,7 +62,7 @@ class GradleBuildActionResultHandler(
onProjectLoaded(state)
}
catch (e: ProcessCanceledException) {
resolverCtx.cancellationTokenSource.cancel()
resolverCtx.cancel()
}
}
}
@@ -116,7 +116,7 @@ public class GradleExecutionHelper {
@Nullable GradleExecutionSettings settings,
@Nullable ExternalSystemTaskId taskId,
@Nullable ExternalSystemTaskNotificationListener listener,
@Nullable CancellationTokenSource cancellationTokenSource,
@Nullable CancellationToken cancellationToken,
@NotNull Function<? super ProjectConnection, ? extends T> f) {
String projectDir;
File projectPathFile = new File(projectPath);
@@ -132,7 +132,6 @@ public class GradleExecutionHelper {
else {
projectDir = projectPath;
}
CancellationToken cancellationToken = cancellationTokenSource != null ? cancellationTokenSource.token() : null;
return withGradleConnection(
projectDir, taskId, settings, listener, cancellationToken,
connection -> {
@@ -326,15 +325,14 @@ public class GradleExecutionHelper {
return () -> FileUtil.loadFileOrNull(fileWithPathToProperties);
}
@Nullable
public static BuildEnvironment getBuildEnvironment(ProjectResolverContext projectResolverContext) {
CancellationTokenSource cancellationTokenSource = projectResolverContext.getCancellationTokenSource();
CancellationToken cancellationToken = cancellationTokenSource != null ? cancellationTokenSource.token() : null;
return getBuildEnvironment(projectResolverContext.getConnection(),
projectResolverContext.getExternalSystemTaskId(),
projectResolverContext.getListener(),
cancellationToken,
projectResolverContext.getSettings());
public static @Nullable BuildEnvironment getBuildEnvironment(@NotNull ProjectResolverContext projectResolverContext) {
return getBuildEnvironment(
projectResolverContext.getConnection(),
projectResolverContext.getExternalSystemTaskId(),
projectResolverContext.getListener(),
projectResolverContext.getCancellationTokenSource().token(),
projectResolverContext.getSettings()
);
}
public static void prepare(
@@ -763,30 +761,6 @@ public class GradleExecutionHelper {
}
}
@Nullable
public static GradleVersion getGradleVersion(@NotNull ProjectConnection connection,
@NotNull ExternalSystemTaskId taskId,
@NotNull ExternalSystemTaskNotificationListener listener,
@Nullable CancellationTokenSource cancellationTokenSource) {
final BuildEnvironment buildEnvironment = getBuildEnvironment(connection, taskId, listener, cancellationTokenSource, null);
GradleVersion gradleVersion = null;
if (buildEnvironment != null) {
gradleVersion = GradleVersion.version(buildEnvironment.getGradle().getGradleVersion());
}
return gradleVersion;
}
@Nullable
public static BuildEnvironment getBuildEnvironment(@NotNull ProjectConnection connection,
@NotNull ExternalSystemTaskId taskId,
@NotNull ExternalSystemTaskNotificationListener listener,
@Nullable CancellationTokenSource cancellationTokenSource,
@Nullable GradleExecutionSettings settings) {
CancellationToken cancellationToken = cancellationTokenSource != null ? cancellationTokenSource.token() : null;
return getBuildEnvironment(connection, taskId, listener, cancellationToken, settings);
}
private static @Nullable BuildEnvironment getBuildEnvironment(
@NotNull ProjectConnection connection,
@NotNull ExternalSystemTaskId taskId,
@@ -1,35 +0,0 @@
/*
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.plugins.gradle.service.execution;
import com.intellij.openapi.externalSystem.service.execution.NotSupportedException;
import org.gradle.tooling.CancellationToken;
import org.gradle.tooling.CancellationTokenSource;
/**
* @author Vladislav.Soroka
*/
public class UnsupportedCancellationToken implements CancellationTokenSource {
@Override
public void cancel() {
throw new NotSupportedException("Configured version of Gradle does not support cancellation. \nPlease, use Gradle 2.1 or newer.");
}
@Override
public CancellationToken token() {
return null;
}
}
@@ -102,6 +102,23 @@ public class DefaultProjectResolverContext extends UserDataHolderBase implements
return myCancellationTokenSource;
}
@Override
public boolean isCancellationRequested() {
return myCancellationTokenSource.token().isCancellationRequested();
}
@Override
public void cancel() {
myCancellationTokenSource.cancel();
}
@Override
public void checkCancelled() {
if (isCancellationRequested()) {
throw new ProcessCanceledException();
}
}
@NotNull
@Override
public ExternalSystemTaskNotificationListener getListener() {
@@ -175,13 +192,6 @@ public class DefaultProjectResolverContext extends UserDataHolderBase implements
return getModels().hasModulesWithModel(modelClass);
}
@Override
public void checkCancelled() {
if (myCancellationTokenSource.token().isCancellationRequested()) {
throw new ProcessCanceledException();
}
}
@Override
public String getProjectGradleVersion() {
if (myProjectGradleVersion == null) {
@@ -39,6 +39,7 @@ import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import org.gradle.api.ProjectConfigurationException;
import org.gradle.tooling.BuildActionFailureException;
import org.gradle.tooling.CancellationToken;
import org.gradle.tooling.CancellationTokenSource;
import org.gradle.tooling.ProjectConnection;
import org.gradle.tooling.model.ProjectModel;
@@ -137,7 +138,8 @@ public class GradleProjectResolver implements ExternalSystemProjectResolver<Grad
DefaultProjectResolverContext resolverContext =
new DefaultProjectResolverContext(syncTaskId, projectPath, settings, listener, gradleResolverPolicy);
final CancellationTokenSource cancellationTokenSource = resolverContext.getCancellationTokenSource();
CancellationTokenSource cancellationTokenSource = resolverContext.getCancellationTokenSource();
CancellationToken cancellationToken = cancellationTokenSource.token();
myCancellationMap.putValue(resolverContext.getExternalSystemTaskId(), cancellationTokenSource);
final long activityId = resolverContext.getExternalSystemTaskId().getId();
@@ -148,14 +150,14 @@ public class GradleProjectResolver implements ExternalSystemProjectResolver<Grad
.startSpan();
try (Scope ignore = gradleExecutionSpan.makeCurrent()) {
if (settings != null) {
myHelper.ensureInstalledWrapper(syncTaskId, projectPath, settings, listener, cancellationTokenSource.token());
myHelper.ensureInstalledWrapper(syncTaskId, projectPath, settings, listener, cancellationToken);
}
Predicate<GradleProjectResolverExtension> extensionsFilter =
gradleResolverPolicy != null ? gradleResolverPolicy.getExtensionsFilter() : null;
final GradleProjectResolverExtension projectResolverChain = createProjectResolverChain(resolverContext, extensionsFilter);
final DataNode<ProjectData> projectDataNode = myHelper.execute(
projectPath, settings, syncTaskId, listener, cancellationTokenSource,
projectPath, settings, syncTaskId, listener, cancellationToken,
getProjectDataFunction(resolverContext, projectResolverChain, false));
// auto-discover buildSrc projects of the main and included builds
@@ -40,9 +40,15 @@ public interface ProjectResolverContext extends UserDataHolderEx {
@NotNull
ProjectConnection getConnection();
@Nullable
@NotNull
CancellationTokenSource getCancellationTokenSource();
boolean isCancellationRequested();
void cancel();
void checkCancelled() throws ProcessCanceledException;
@NotNull
ExternalSystemTaskNotificationListener getListener();
@@ -96,8 +102,6 @@ public interface ProjectResolverContext extends UserDataHolderEx {
boolean hasModulesWithModel(@NotNull Class<?> modelClass);
void checkCancelled() throws ProcessCanceledException;
@Nullable
String getProjectGradleVersion();
@@ -104,15 +104,15 @@ public class GradleTaskManager implements ExternalSystemTaskManager<GradleExecut
settings == null ? new GradleExecutionSettings(null, null, DistributionType.BUNDLED, false) : settings;
CancellationTokenSource cancellationTokenSource = GradleConnector.newCancellationTokenSource();
CancellationToken cancellationToken = cancellationTokenSource.token();
myCancellationMap.put(id, cancellationTokenSource);
try {
if (effectiveSettings.getDistributionType() == DistributionType.WRAPPED) {
String rootProjectPath = determineRootProject(projectPath);
CancellationToken cancellationToken = cancellationTokenSource.token();
myHelper.ensureInstalledWrapper(id, rootProjectPath, effectiveSettings, listener, cancellationToken);
}
myHelper.execute(projectPath, effectiveSettings, id, listener, cancellationTokenSource, connection -> {
executeTasks(id, tasks, projectPath, effectiveSettings, jvmParametersSetup, listener, connection, cancellationTokenSource);
myHelper.execute(projectPath, effectiveSettings, id, listener, cancellationToken, connection -> {
executeTasks(id, tasks, projectPath, effectiveSettings, jvmParametersSetup, listener, connection, cancellationToken);
return null;
});
}
@@ -128,10 +128,10 @@ public class GradleTaskManager implements ExternalSystemTaskManager<GradleExecut
@Nullable String jvmParametersSetup,
@NotNull ExternalSystemTaskNotificationListener listener,
@NotNull ProjectConnection connection,
@NotNull CancellationTokenSource cancellationTokenSource) {
@NotNull CancellationToken cancellationToken) {
BuildEnvironment buildEnvironment = null;
try {
buildEnvironment = GradleExecutionHelper.getBuildEnvironment(connection, id, listener, cancellationTokenSource, settings);
buildEnvironment = GradleExecutionHelper.getBuildEnvironment(connection, id, listener, cancellationToken, settings);
var gradleVersion = getGradleVersion(buildEnvironment);
setupGradleScriptDebugging(settings);
@@ -153,12 +153,12 @@ public class GradleTaskManager implements ExternalSystemTaskManager<GradleExecut
if (isApplicableTestLauncher(id, projectPath, tasks, settings, gradleVersion)) {
TestLauncher launcher = myHelper.getTestLauncher(connection, id, tasks, settings, listener);
launcher.withCancellationToken(cancellationTokenSource.token());
launcher.withCancellationToken(cancellationToken);
launcher.run();
}
else {
BuildLauncher launcher = myHelper.getBuildLauncher(connection, id, tasks, settings, listener);
launcher.withCancellationToken(cancellationTokenSource.token());
launcher.withCancellationToken(cancellationToken);
launcher.run();
}
GradleTaskResultListener.EP_NAME.forEachExtensionSafe(ext -> ext.onSuccess(id, projectPath));
@@ -3,18 +3,16 @@
package org.jetbrains.kotlin.idea.gradleJava.scripting.importing
import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskId
import com.intellij.openapi.progress.ProcessCanceledException
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.NlsSafe
import com.intellij.openapi.util.io.FileUtil.toSystemIndependentName
import com.intellij.openapi.vfs.VfsUtil
import org.gradle.tooling.model.kotlin.dsl.EditorReportSeverity
import org.gradle.tooling.model.kotlin.dsl.KotlinDslScriptsModel
import org.jetbrains.kotlin.idea.gradleTooling.BrokenKotlinDslScriptsModel
import org.jetbrains.kotlin.idea.gradle.KotlinIdeaGradleBundle
import org.jetbrains.kotlin.idea.gradle.scripting.importing.*
import org.jetbrains.kotlin.idea.gradle.scripting.importing.LOG
import org.jetbrains.kotlin.idea.gradleJava.scripting.getGradleScriptInputsStamp
import org.jetbrains.kotlin.idea.gradleJava.scripting.roots.GradleBuildRootsManager
import org.jetbrains.kotlin.idea.gradleTooling.BrokenKotlinDslScriptsModel
import org.jetbrains.plugins.gradle.model.GradleBuildScriptClasspathModel
import org.jetbrains.plugins.gradle.service.project.ProjectResolverContext
import java.io.File
@@ -71,11 +69,7 @@ fun processScriptModel(
it.failed = true
}
}
resolverCtx.cancellationTokenSource?.cancel() ?: throw ProcessCanceledException(
IllegalStateException(KotlinIdeaGradleBundle.message("title.kotlin.build.script")
+ ":\n"
+ errors.joinToString("\n") { it.text + "\n" + it.details })
)
resolverCtx.cancel()
}
errors.isEmpty()
}