Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: optimize watch task to prevent occasional connection closed exception #33

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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