Skip to content

Commit

Permalink
Try to fix multi invocation problem
Browse files Browse the repository at this point in the history
  • Loading branch information
asodja committed Jan 28, 2024
1 parent 3697210 commit f23fc8c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/main/java/org/gradle/profiler/studio/StudioGradleClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
import java.io.File;
import java.nio.file.Path;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -127,9 +124,22 @@ private List<Duration> collectGradleInvocations(StudioConnections connections, A
if (invocationStarted.isPresent()) {
System.out.printf("* Gradle invocation %s has started, waiting for it to complete...%n", invocation);
agentConnection.send(new GradleInvocationParameters(gradleArgs, jvmArgs));
GradleInvocationCompleted agentCompleted = agentConnection.receiveGradleInvocationCompleted(GRADLE_INVOCATION_COMPLETED_TIMEOUT);
System.out.printf("* Gradle invocation %s has completed in: %sms%n", invocation++, agentCompleted.getDurationMillis());
durations.add(Duration.ofMillis(agentCompleted.getDurationMillis()));
Set<Integer> invocations = new HashSet<>(Collections.singletonList(invocationStarted.get().getId()));
while (!invocations.isEmpty()) {
Message message = agentConnection.receiveMessage(GRADLE_INVOCATION_COMPLETED_TIMEOUT);
if (message instanceof GradleInvocationCompleted) {
GradleInvocationCompleted agentCompleted = (GradleInvocationCompleted) message;
System.out.printf("* Gradle invocation %s has completed in: %sms%n", invocation++, agentCompleted.getDurationMillis());
durations.add(Duration.ofMillis(agentCompleted.getDurationMillis()));
invocations.remove(agentCompleted.getId());
} else if (message instanceof GradleInvocationStarted) {
invocations.add(((GradleInvocationStarted) message).getId());
System.out.printf("* Gradle invocation %s has started, waiting for it to complete...%n", invocation);
agentConnection.send(new GradleInvocationParameters(gradleArgs, jvmArgs));
} else {
throw new RuntimeException("Unexpected message, should be either GradleInvocationStarted or GradleInvocationCompleted, but got " + message);
}
}
}
}
return durations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public GradleInvocationStarted receiveGradleInvocationStarted(Duration timeout)
return protocolHandler.receive(GradleInvocationStarted.class, timeout);
}

public Message receiveMessage(Duration timeout) {
return protocolHandler.receive(Message.class, timeout);
}

public GradleInvocationCompleted receiveGradleInvocationCompleted(Duration timeout) {
return protocolHandler.receive(GradleInvocationCompleted.class, timeout);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ private void handleSyncRequest(StudioRequest request, Project project, GradleSys
// for example when we open a project for the first time.
waitOnPreviousGradleSyncFinish(project);
waitOnBackgroundProcessesFinish(project);
waitOnPreviousGradleSyncFinish(project);

LOG.info(String.format("[SYNC REQUEST %s] Sync has started%n", request.getId()));
Stopwatch stopwatch = isStartup ? startupStopwatch : Stopwatch.createStarted();
Expand Down

0 comments on commit f23fc8c

Please sign in to comment.