Skip to content

Commit

Permalink
Merge pull request #25376 from Postremus/issues/25331
Browse files Browse the repository at this point in the history
Trigger restart when deleting previously existing watched file
  • Loading branch information
aloubyansky authored May 10, 2022
2 parents 8ff90f0 + 169510b commit bd8d92b
Show file tree
Hide file tree
Showing 9 changed files with 330 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
import io.quarkus.deployment.builditem.ServiceStartBuildItem;
import io.quarkus.dev.testing.TestWatchedFiles;

public class HotDeploymentConfigFileBuildStep {
public class HotDeploymentWatchedFileBuildStep {

@BuildStep
ServiceStartBuildItem setupConfigFileHotDeployment(List<HotDeploymentWatchedFileBuildItem> files,
ServiceStartBuildItem setupWatchedFileHotDeployment(List<HotDeploymentWatchedFileBuildItem> files,
LaunchModeBuildItem launchModeBuildItem) {
// TODO: this should really be an output of the RuntimeRunner
RuntimeUpdatesProcessor processor = RuntimeUpdatesProcessor.INSTANCE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,7 @@ Set<String> checkForFileChange(Function<DevModeContext.ModuleInfo, DevModeContex
if (rootPaths.isEmpty() || outputPath == null) {
continue;
}
Path outputDir = Paths.get(outputPath);
final List<Path> roots = rootPaths.stream()
.filter(Files::exists)
.filter(Files::isReadable)
Expand All @@ -883,7 +884,6 @@ Set<String> checkForFileChange(Function<DevModeContext.ModuleInfo, DevModeContex
final Set<Path> seen = new HashSet<>(moduleResources);
try {
for (Path root : roots) {
Path outputDir = Paths.get(outputPath);
//since the stream is Closeable, use a try with resources so the underlying iterator is closed
try (final Stream<Path> walk = Files.walk(root)) {
walk.forEach(path -> {
Expand Down Expand Up @@ -927,11 +927,13 @@ Set<String> checkForFileChange(Function<DevModeContext.ModuleInfo, DevModeContex
}
}

for (Path root : roots) {
Path outputDir = Paths.get(outputPath);
for (String path : timestampSet.watchedFilePaths.keySet()) {
for (String path : timestampSet.watchedFilePaths.keySet()) {
boolean pathCurrentlyExisting = false;
boolean pathPreviouslyExisting = false;
for (Path root : roots) {
Path file = root.resolve(path);
if (file.toFile().exists()) {
pathCurrentlyExisting = true;
try {
long value = Files.getLastModifiedTime(file).toMillis();
Long existing = timestampSet.watchedFileTimestamps.get(file);
Expand Down Expand Up @@ -967,13 +969,20 @@ Set<String> checkForFileChange(Function<DevModeContext.ModuleInfo, DevModeContex
throw new UncheckedIOException(e);
}
} else {
timestampSet.watchedFileTimestamps.put(file, 0L);
Path target = outputDir.resolve(path);
try {
FileUtil.deleteDirectory(target);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Long prevValue = timestampSet.watchedFileTimestamps.put(file, 0L);
pathPreviouslyExisting = pathPreviouslyExisting || (prevValue != null && prevValue > 0);
}
}
if (!pathCurrentlyExisting) {
if (pathPreviouslyExisting) {
ret.add(path);
}

Path target = outputDir.resolve(path);
try {
FileUtil.deleteIfExists(target);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
Expand Down Expand Up @@ -1011,7 +1020,12 @@ Set<String> checkForFileChange(Function<DevModeContext.ModuleInfo, DevModeContex
throw new UncheckedIOException(e);
}
} else {
timestampSet.watchedFileTimestamps.put(watchedFile, 0L);

Long prevValue = timestampSet.watchedFileTimestamps.put(watchedFile, 0L);

if (prevValue != null && prevValue > 0) {
ret.add(watchedFilePath);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,29 @@

public class FileUtil {

public static void deleteIfExists(final Path path) throws IOException {
BasicFileAttributes attributes;
try {
attributes = Files.readAttributes(path, BasicFileAttributes.class);
} catch (IOException ignored) {
// Files.isDirectory is also simply returning when any IOException occurs, same behaviour is fine
return;
}
if (attributes.isDirectory()) {
deleteDirectoryIfExists(path);
} else if (attributes.isRegularFile()) {
Files.deleteIfExists(path);
}
}

public static void deleteDirectory(final Path directory) throws IOException {
if (!Files.isDirectory(directory)) {
return;
}
deleteDirectoryIfExists(directory);
}

private static void deleteDirectoryIfExists(final Path directory) throws IOException {
Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,25 @@ public void testThatNewResourcesAreServed() throws MavenInvocationException, IOE
.until(() -> DevModeTestUtils.getHttpResponse("/lorem.txt", 404));
}

@Test
public void testThatConfigFileDeletionsAreDetected() throws MavenInvocationException, IOException {
testDir = initProject("projects/dev-mode-file-deletion");
runAndCheck();

await()
.pollDelay(100, TimeUnit.MILLISECONDS)
.atMost(1, TimeUnit.MINUTES)
.until(() -> DevModeTestUtils.getHttpResponse("/app/hello/greetings").contains("Bonjour"));

File source = new File(testDir, "src/main/resources/application.properties");
FileUtils.delete(source);

await()
.pollDelay(100, TimeUnit.MILLISECONDS)
.atMost(1, TimeUnit.MINUTES)
.until(() -> DevModeTestUtils.getHttpResponse("/app/hello/greetings").contains("Guten Morgen"));
}

@Test
public void testThatMultipleResourceDirectoriesAreSupported() throws MavenInvocationException, IOException {
testDir = initProject("projects/dev-mode-multiple-resource-dirs");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.acme</groupId>
<artifactId>acme</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
<quarkus.platform.version>@project.version@</quarkus.platform.version>
<quarkus-plugin.version>@project.version@</quarkus-plugin.version>
<maven.compiler.source>11</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<!-- insert managed dependencies here -->
<dependency>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>${quarkus.platform.artifact-id}</artifactId>
<version>${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- insert test dependencies here -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>${quarkus-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>generate-code</goal>
<goal>generate-code-tests</goal>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.acme;

import org.eclipse.microprofile.config.inject.ConfigProperty;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class HelloResource {

@ConfigProperty(name = "greeting", defaultValue = "Guten Morgen")
String greeting;

@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "hello";
}

@GET
@Path("/greetings")
@Produces(MediaType.TEXT_PLAIN)
public String greetings() {
return greeting;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.acme;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/app")
public class MyApplication extends Application {

}
Loading

0 comments on commit bd8d92b

Please sign in to comment.