-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
External file scan when --watch is set was broken for a while
- Loading branch information
1 parent
5abc29a
commit 7e7b601
Showing
6 changed files
with
230 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/main/java/io/github/azagniotov/stubby4j/filesystem/MainIncludedYamlScanner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package io.github.azagniotov.stubby4j.filesystem; | ||
|
||
import io.github.azagniotov.stubby4j.cli.ANSITerminal; | ||
import io.github.azagniotov.stubby4j.stubs.StubRepository; | ||
import io.github.azagniotov.stubby4j.utils.DateTimeUtils; | ||
import io.github.azagniotov.stubby4j.utils.FileUtils; | ||
import io.github.azagniotov.stubby4j.yaml.YamlParser; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.File; | ||
import java.io.InputStream; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static io.github.azagniotov.stubby4j.utils.FileUtils.BR; | ||
|
||
public final class MainIncludedYamlScanner implements Runnable { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(MainIncludedYamlScanner.class); | ||
|
||
private final long sleepTime; | ||
private final StubRepository stubRepository; | ||
|
||
public MainIncludedYamlScanner(final StubRepository stubRepository, final long sleepTime) { | ||
this.sleepTime = sleepTime; | ||
this.stubRepository = stubRepository; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
|
||
final YamlParser yamlParser = new YamlParser(); | ||
|
||
try { | ||
final File mainDataYaml = stubRepository.getYamlConfig(); | ||
final String mainYamlParentDirectory = mainDataYaml.getParent(); | ||
final InputStream mainConfigInputStream = FileUtils.constructInputStream(mainDataYaml); | ||
final Object rawYamlConfig = yamlParser.loadRawYamlConfig(mainConfigInputStream); | ||
|
||
// This means that our main YAML config does not include other files, i.e.: | ||
// | ||
// includes: | ||
// - service-1-stubs.yaml | ||
// - service-2-stubs.yaml | ||
// - service-3-stubs.yaml | ||
// | ||
if (!yamlParser.isMainYamlHasIncludes(rawYamlConfig)) { | ||
return; | ||
} | ||
|
||
ANSITerminal.status(String.format("Main YAML with included YAMLs scan enabled, watching included YAMLs referenced from %s", stubRepository.getYamlConfigCanonicalPath())); | ||
LOGGER.debug("Main YAML with included YAMLs scan enabled, watching included YAMLs referenced from {}.", | ||
stubRepository.getYamlConfigCanonicalPath()); | ||
|
||
final List<File> yamlIncludes = yamlParser.getYamlIncludes(mainYamlParentDirectory, rawYamlConfig); | ||
final Map<File, Long> yamlIncludeFiles = new HashMap<>(); | ||
for (final File include : yamlIncludes) { | ||
yamlIncludeFiles.put(include, include.lastModified()); | ||
} | ||
|
||
while (!Thread.currentThread().isInterrupted()) { | ||
|
||
Thread.sleep(sleepTime); | ||
|
||
boolean isContinue = true; | ||
String offendingFilename = ""; | ||
for (Map.Entry<File, Long> entry : yamlIncludeFiles.entrySet()) { | ||
final File file = entry.getKey(); | ||
final long lastModified = entry.getValue(); | ||
final long currentFileModified = file.lastModified(); | ||
|
||
if (lastModified < currentFileModified) { | ||
yamlIncludeFiles.put(file, currentFileModified); | ||
isContinue = false; | ||
offendingFilename = file.getAbsolutePath(); | ||
break; | ||
} | ||
} | ||
|
||
if (isContinue) { | ||
continue; | ||
} | ||
|
||
ANSITerminal.info(String.format("%sMain YAML included YAMLs scan detected change in %s%s", BR, offendingFilename, BR)); | ||
LOGGER.info("Main YAML included YAMLs scan detected change in {}.", offendingFilename); | ||
|
||
try { | ||
stubRepository.refreshStubsFromYamlConfig(yamlParser); | ||
|
||
ANSITerminal.ok(String.format("%sSuccessfully performed live refresh of main YAML with included YAMLs from: %s on [" + DateTimeUtils.systemDefault() + "]%s", | ||
BR, stubRepository.getYamlConfig(), BR)); | ||
LOGGER.info("Successfully performed live refresh of main YAML with included YAMLs from: {}.", | ||
stubRepository.getYamlConfig()); | ||
} catch (final Exception ex) { | ||
ANSITerminal.error("Could not refresh YAML configuration, previously loaded stubs remain untouched." + ex.toString()); | ||
LOGGER.error("Could not refresh YAML configuration, previously loaded stubs remain untouched.", ex); | ||
} | ||
} | ||
|
||
} catch (final Exception ex) { | ||
ex.printStackTrace(); | ||
LOGGER.error("Could not perform live main YAML scan with included YAMLs.", ex); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/test/java/io/github/azagniotov/stubby4j/yaml/YamlParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package io.github.azagniotov.stubby4j.yaml; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
public class YamlParserTest { | ||
|
||
@Test | ||
public void isMainYamlHasIncludesFoundAsTrue() { | ||
final YamlParser yamlParser = new YamlParser(); | ||
|
||
final Map<String, List<String>> loadedYamlConfig = new HashMap<>(); | ||
loadedYamlConfig.put("includes", new ArrayList<>()); | ||
|
||
assertThat(yamlParser.isMainYamlHasIncludes(loadedYamlConfig)).isTrue(); | ||
} | ||
|
||
@Test | ||
public void isMainYamlHasIncludesFoundAsFalse() { | ||
final YamlParser yamlParser = new YamlParser(); | ||
|
||
final Map<String, List<String>> loadedYamlConfig = new HashMap<>(); | ||
loadedYamlConfig.put("unexpectedKey", new ArrayList<>()); | ||
|
||
assertThat(yamlParser.isMainYamlHasIncludes(loadedYamlConfig)).isFalse(); | ||
} | ||
|
||
@Test | ||
public void isMainYamlHasIncludesFoundAsFalseWhenNotMap() { | ||
final YamlParser yamlParser = new YamlParser(); | ||
|
||
assertThat(yamlParser.isMainYamlHasIncludes(new ArrayList<>())).isFalse(); | ||
} | ||
} |