Skip to content

Commit

Permalink
refactor: optimize watch task to prevent occasional connection closed…
Browse files Browse the repository at this point in the history
… exception (#33)

### What this PR does?
优化 Watch 任务偶尔会出现 connection closed 异常信息的问题

```release-note
优化 Watch 任务偶尔会出现 connection closed 异常信息的问题
```
  • Loading branch information
guqing authored Nov 5, 2024
1 parent a8f621f commit 46ecbe3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 29 deletions.
19 changes: 9 additions & 10 deletions src/main/java/run/halo/gradle/watch/WatchTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,15 @@ public void runRemoteCommand() {
});

WatchExecutionParameters parameters = getParameters(List.of("build"));
try (WatchTaskRunner runner = new WatchTaskRunner(getProject());) {
watcher.addListener(changeSet -> {
System.out.println("File changed......" + changeSet);
runner.run(parameters);
pluginClient.reloadPlugin();
});
watcher.start();
// start docker container and waiting
super.runRemoteCommand();
}
WatchTaskRunner runner = new WatchTaskRunner(getProject());
watcher.addListener(changeSet -> {
System.out.println("File changed......" + changeSet);
runner.run(parameters);
pluginClient.reloadPlugin();
});
watcher.start();
// start docker container and waiting
super.runRemoteCommand();
}

private void configWatchFiles(FileSystemWatcher watcher) {
Expand Down
36 changes: 17 additions & 19 deletions src/main/java/run/halo/gradle/watch/WatchTaskRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,49 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import org.gradle.StartParameter;
import org.gradle.api.Project;
import org.gradle.api.logging.Logging;
import org.gradle.internal.classpath.ClassPath;
import org.gradle.tooling.BuildException;
import org.gradle.tooling.GradleConnector;
import org.gradle.tooling.ProjectConnection;
import org.gradle.tooling.internal.consumer.DefaultBuildLauncher;
import org.gradle.tooling.internal.consumer.DefaultGradleConnector;
import org.gradle.wrapper.GradleUserHomeLookup;
import org.slf4j.Logger;
import run.halo.gradle.WatchExecutionParameters;
import run.halo.gradle.utils.Assert;

/**
* @author guqing
* @since 2.0.0
*/
public class WatchTaskRunner implements AutoCloseable {
public class WatchTaskRunner {

private static final Logger LOG = Logging.getLogger(WatchTaskRunner.class);
private final ProjectConnection connection;
private final GradleConnector gradleConnector;

public WatchTaskRunner(Project project) {
// StartParameter parameter = project.getGradle().getStartParameter();
DefaultGradleConnector gradleConnector =
(DefaultGradleConnector) GradleConnector.newConnector();
gradleConnector.useGradleUserHomeDir(project.getGradle().getGradleUserHomeDir());
gradleConnector.useDistributionBaseDir(GradleUserHomeLookup.gradleUserHome());
connection = gradleConnector.forProjectDirectory(project.getProjectDir())
.connect();
this.gradleConnector = gradleConnector.forProjectDirectory(project.getProjectDir());
}

public void run(WatchExecutionParameters parameters) {
Assert.notNull(parameters, "WatchExecutionParameters must not be null");
try (var connection = gradleConnector.connect()) {
var launcher = createBuildLauncher(parameters, connection);
launcher.run();
} catch (BuildException e) {
System.err.println(e.getMessage());
}
}

@Nonnull
private static DefaultBuildLauncher createBuildLauncher(
WatchExecutionParameters parameters, ProjectConnection connection) {
DefaultBuildLauncher launcher = (DefaultBuildLauncher) connection
.newBuild()
.setStandardOutput(new NoCloseOutputStream(parameters.getStandardOutput()))
Expand All @@ -58,20 +66,10 @@ public void run(WatchExecutionParameters parameters) {

launcher.setJvmArguments(parameters.getJvmArgs().toArray(new String[0]));
launcher.setEnvironmentVariables(parameters.getEnvironment());

try {
launcher.run();
} catch (BuildException e) {
// ignore...
e.printStackTrace();
}
}

public void close() {
connection.close();
return launcher;
}

private String[] getArguments(StartParameter parameter) {
static String[] getArguments(StartParameter parameter) {
List<String> args = new ArrayList<>();
for (Map.Entry<String, String> e : parameter.getProjectProperties().entrySet()) {
args.add("-P" + e.getKey() + "=" + e.getValue());
Expand Down

0 comments on commit 46ecbe3

Please sign in to comment.