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

Faster response to file changes. #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
26 changes: 4 additions & 22 deletions src/main/java/com/airhacks/wad/boundary/WADFlow.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@

package com.airhacks.wad.boundary;

import com.airhacks.wad.control.Builder;
import com.airhacks.wad.control.Copier;
import com.airhacks.wad.control.FolderWatchService;
import com.airhacks.wad.control.TerminalColors;
import com.airhacks.wad.control.*;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
Expand All @@ -16,7 +13,6 @@
import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.LongSummaryStatistics;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.maven.shared.invoker.InvocationResult;
Expand All @@ -41,22 +37,8 @@ public WADFlow(Path dir, Path war, Collection<Path> deploymentTargets) throws IO
this.copier = new Copier(war, deploymentTargets);
Runnable changeListener = this::buildAndDeploy;
changeListener.run();
registerEnterListener(changeListener);
FolderWatchService.listenForChanges(dir, changeListener);
}

void registerEnterListener(Runnable listener) {
InputStream in = System.in;
Runnable task = () -> {
int c;
try {
while ((c = in.read()) != -1) {
listener.run();
}
} catch (IOException ex) {
}
};
new Thread(task).start();
Watcher watcher = new Watcher(dir,changeListener);
watcher.runBlocking();
}

void buildAndDeploy() {
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/com/airhacks/wad/control/Copier.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

package com.airhacks.wad.control;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -54,7 +55,7 @@ Optional<Path> findWarInPath(Path fromDirectory) {
e.printStackTrace();
}

return null;
return Optional.empty();
}

List<Path> addWarName(Collection<Path> deploymentDirectories, String warName) {
Expand All @@ -64,16 +65,21 @@ List<Path> addWarName(Collection<Path> deploymentDirectories, String warName) {
collect(Collectors.toList());
}

Path copySingle(Path from, Path to) {
void copySingle(Path from, Path to) {
long kb;
try {
kb = Files.size(from) / 1024;
warSizes.add(kb);
System.out.printf("Copying %dkB ThinWAR %s to %s %s %s \n", kb, from.toString(), TerminalColors.FILE.value(), shortenForDisplay(to, 40), TerminalColors.RESET.value());
return Files.copy(from, to, StandardCopyOption.REPLACE_EXISTING);

Files.copy(from, to, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException ex) {
throw new IllegalStateException(ex.getMessage(), ex);
File targetFile = to.toFile();
boolean targetFileIsLocked = !targetFile.renameTo(targetFile);
if(targetFileIsLocked) {
System.err.println("Cannot copy because server has locked file \""+to+"\"");
}else{
throw new IllegalStateException(ex.getMessage(), ex);
}
}
}

Expand Down
78 changes: 0 additions & 78 deletions src/main/java/com/airhacks/wad/control/FolderWatchService.java

This file was deleted.

52 changes: 52 additions & 0 deletions src/main/java/com/airhacks/wad/control/Watcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.airhacks.wad.control;

import com.sun.nio.file.SensitivityWatchEventModifier;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAmount;

public class Watcher {
private static final TemporalAmount MIN_RUN_DELAY = Duration.ofSeconds(2);

private final WatchService watchService;
private final Runnable runnable;
private LocalDateTime lastChange = LocalDateTime.MIN;

public Watcher(Path dir, Runnable runnable) throws IOException {
watchService = FileSystems.getDefault().newWatchService();
registerAll(dir, watchService);
this.runnable = runnable;
}

private void registerAll(final Path start, WatchService watchService) throws IOException {
Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
dir.register(watchService, new WatchEvent.Kind[] {StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE}, SensitivityWatchEventModifier.HIGH);
return FileVisitResult.CONTINUE;
}
});
}

public void runBlocking() {
try{
WatchKey key;
while ((key = watchService.take()) != null) {
if(LocalDateTime.now().isAfter(lastChange.plus(MIN_RUN_DELAY))){
runnable.run();
}else{
//Not running deploy because last deploy was less than "MIN_RUN_DELAY" seconds ago.
}
key.pollEvents().clear(); //remove all pending events to prevent second deploy
lastChange = LocalDateTime.now();
key.reset();
}
}catch (InterruptedException e){
throw new RuntimeException(e);
}
}
}
84 changes: 0 additions & 84 deletions src/test/java/com/airhacks/wad/control/FolderWatchServiceTest.java

This file was deleted.

Loading