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

add hot reload functionnality for mock server #909

Merged
merged 2 commits into from
Oct 9, 2019
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.intuit.karate.netty;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FileChangedWatcher {

private static final Logger logger = LoggerFactory.getLogger(FileChangedWatcher.class);

private File file;
private FeatureServer server;
private Integer port;
private boolean ssl;
private File cert;
private File key;

public FileChangedWatcher(File mock, FeatureServer server, Integer port, boolean ssl, File cert, File key) {
this.file = mock;
this.server = server;
this.port = port;
this.ssl = ssl;
this.cert = cert;
this.key = key;
}

public void watch() throws InterruptedException, IOException {

try {
final Path directoryPath = file.toPath().getParent();
final WatchService watchService = FileSystems.getDefault().newWatchService();
directoryPath.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
while (true) {
final WatchKey wk = watchService.take();
for (WatchEvent<?> event : wk.pollEvents()) {
final Path fileChangedPath = (Path) event.context();
if (fileChangedPath.endsWith(file.getName())) {
onModified();
}
}
wk.reset();
}
} catch (Exception exception) {
logger.error("exception when handling change of mock file");
}
}

public void onModified() {
if (server != null) {
server.stop();
server = FeatureServer.start(file, port, ssl, cert, key, null);
}
}
}
7 changes: 6 additions & 1 deletion karate-netty/src/main/java/com/intuit/karate/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.intuit.karate.exception.KarateException;
import com.intuit.karate.job.JobExecutor;
import com.intuit.karate.netty.FeatureServer;
import com.intuit.karate.netty.FileChangedWatcher;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -199,8 +200,12 @@ public Void call() throws Exception {
key = new File(FeatureServer.DEFAULT_KEY_NAME);
}
FeatureServer server = FeatureServer.start(mock, port, ssl, cert, key, null);

// hot reload
FileChangedWatcher watcher = new FileChangedWatcher(mock, server, port, ssl, cert, key);
watcher.watch();

server.waitSync();
return null;
}

}