From 8b5581da44341b463d9e69b551a6d2ca7491ca29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Fri, 13 Oct 2023 12:47:31 +0200 Subject: [PATCH] Suspend the manager, then wait for any jobs and cancel remaining --- .../eclipse/tycho/apitools/ApiAnalysis.java | 53 ++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/ApiAnalysis.java b/tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/ApiAnalysis.java index d0c0ca3117..884bbb8cdb 100644 --- a/tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/ApiAnalysis.java +++ b/tycho-apitools-plugin/src/main/java/org/eclipse/tycho/apitools/ApiAnalysis.java @@ -38,6 +38,9 @@ import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.jobs.IJobChangeEvent; +import org.eclipse.core.runtime.jobs.IJobChangeListener; +import org.eclipse.core.runtime.jobs.IJobManager; import org.eclipse.core.runtime.jobs.Job; import org.eclipse.jdt.core.IClasspathEntry; import org.eclipse.jdt.core.IJavaProject; @@ -94,6 +97,39 @@ public ApiAnalysis(Collection baselineBundles, Collection dependency public ApiAnalysisResult call() throws Exception { ApiAnalysisResult result = new ApiAnalysisResult(); Platform.addLogListener((status, plugin) -> debug(status.toString())); + IJobManager jobManager = Job.getJobManager(); + jobManager.addJobChangeListener(new IJobChangeListener() { + + @Override + public void sleeping(IJobChangeEvent event) { + debug("Job " + event.getJob() + " sleeping..."); + } + + @Override + public void scheduled(IJobChangeEvent event) { + debug("Job " + event.getJob() + " scheduled..."); + } + + @Override + public void running(IJobChangeEvent event) { + debug("Job " + event.getJob() + " running..."); + } + + @Override + public void done(IJobChangeEvent event) { + debug("Job " + event.getJob() + " done..."); + } + + @Override + public void awake(IJobChangeEvent event) { + debug("Job " + event.getJob() + " awake..."); + } + + @Override + public void aboutToRun(IJobChangeEvent event) { + debug("Job " + event.getJob() + " aboutToRun..."); + } + }); printVersion(); disableAutoBuild(); setTargetPlatform(); @@ -125,6 +161,20 @@ public ApiAnalysisResult call() throws Exception { return result; } + private void waitForAnyJobs() { + IJobManager manager = Job.getJobManager(); + debug("Suspend JobManager..."); + manager.suspend(); + debug("Wait for JobManager become idle..."); + while (!manager.isIdle()) { + Thread.onSpinWait(); + } + // See https://github.com/eclipse-platform/eclipse.platform/issues/741 + debug("Cancel remaining jobs..."); + manager.cancel(null); + debug("JobManager is idle... no new jobs will run!"); + } + private BundleComponent importProject() throws CoreException, IOException { IPath projectPath = IPath.fromOSString(projectDir); IPath projectDescriptionFile = projectPath.append(IProjectDescription.DESCRIPTION_FILE_NAME); @@ -140,6 +190,7 @@ private BundleComponent importProject() throws CoreException, IOException { project.create(projectDescription, new NullProgressMonitor()); project.open(new NullProgressMonitor()); createOutputFolder(project, projectPath); + waitForAnyJobs(); IApiBaseline workspaceBaseline = ApiPlugin.getDefault().getApiBaselineManager().getWorkspaceBaseline(); IApiComponent component = workspaceBaseline.getApiComponent(project); if (component instanceof ProjectComponent projectComponent) { @@ -203,7 +254,7 @@ private void disableAutoBuild() throws CoreException { IWorkspaceDescription desc = workspace.getDescription(); desc.setAutoBuilding(false); workspace.setDescription(desc); - PDECore.getDefault().getPreferencesManager().setValue(ICoreConstants.DISABLE_API_ANALYSIS_BUILDER, false); + PDECore.getDefault().getPreferencesManager().setValue(ICoreConstants.DISABLE_API_ANALYSIS_BUILDER, true); PDECore.getDefault().getPreferencesManager().setValue(ICoreConstants.RUN_API_ANALYSIS_AS_JOB, false); }