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

KOGITO-9393 [SWF Dev UI] Hot reload custom dashboards #1784

Merged
merged 4 commits into from
Jul 3, 2023
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
Expand Up @@ -93,6 +93,11 @@
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,23 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import javax.enterprise.context.ApplicationScoped;
Expand All @@ -43,6 +53,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;

@ApplicationScoped
public class CustomDashboardStorageImpl implements CustomDashboardStorage {

Expand Down Expand Up @@ -74,18 +88,25 @@ private void start(final URL classLoaderCustomDashboardUrl, final URL customDash
} catch (Exception ex) {
LOGGER.warn("Couldn't properly initialize CustomDashboardStorageImpl");
} finally {
init();
init(readCustomDashboardResources());
String storageUrl = getStorageUrl(classLoaderCustomDashboardUrl);
Thread t = new Thread(new DashboardFilesWatcher(reload(), storageUrl));
t.start();
}
}

protected String getStorageUrl(URL classLoaderCustomDashboardUrl) {
return ConfigProvider.getConfig()
.getOptionalValue(PROJECT_CUSTOM_DASHBOARD_STORAGE_PROP, String.class)
.orElseGet(() -> classLoaderCustomDashboardUrl.getFile());
}

private URL getCustomDashboardStorageUrl(URL classLoaderCustomDashboardUrl) {
if (classLoaderCustomDashboardUrl == null) {
return null;
}

String storageUrl = ConfigProvider.getConfig()
.getOptionalValue(PROJECT_CUSTOM_DASHBOARD_STORAGE_PROP, String.class)
.orElseGet(() -> classLoaderCustomDashboardUrl.getFile());
String storageUrl = getStorageUrl(classLoaderCustomDashboardUrl);

File customDashStorageeFolder = new File(storageUrl);

Expand Down Expand Up @@ -133,8 +154,9 @@ public void updateCustomDashboard(String content) {

}

private void init() {
readCustomDashboardResources().stream()
private void init(Collection<File> files) {
customDashboardInfoMap.clear();
files.stream()
.forEach(file -> {
LocalDateTime lastModified = LocalDateTime.ofInstant(Instant.ofEpochMilli(file.lastModified()), TimeZone.getDefault().toZoneId());
customDashboardInfoMap.put(file.getName(),
Expand All @@ -150,4 +172,49 @@ private Collection<File> readCustomDashboardResources() {
}
return Collections.emptyList();
}

private Consumer<Collection<File>> reload() {
return this::init;
}

private class DashboardFilesWatcher implements Runnable {

private final Map<WatchKey, Path> keys = new HashMap<>();
private Consumer<Collection<File>> consumer;
private String folder;

public DashboardFilesWatcher(Consumer<Collection<File>> consumer, String folder) {
this.consumer = consumer;
this.folder = folder;
}

@Override
public void run() {
try (WatchService ws = FileSystems.getDefault().newWatchService()) {
Path path = Path.of(folder);
keys.put(path.register(ws, ENTRY_MODIFY, ENTRY_CREATE), path);

Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
keys.put(dir.register(ws, ENTRY_MODIFY, ENTRY_CREATE, ENTRY_DELETE), dir);
return FileVisitResult.CONTINUE;
}
});
WatchKey key;
while ((key = ws.take()) != null) {
for (WatchEvent<?> event : key.pollEvents()) {
LOGGER.warn("Event kind: {}. File affected: {}", event.kind(), event.context());
consumer.accept(readCustomDashboardResources());
}
key.reset();
}
} catch (InterruptedException e) {
LOGGER.warn("Exception in custom dashboard folder watcher for folder: {}, message: {}", folder, e.getMessage(), e);
Thread.currentThread().interrupt();
} catch (IOException ex) {
LOGGER.warn("Exception in custom dashboard folder watcher for folder: {}, message: {}", folder, ex.getMessage(), ex);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,23 @@

package org.kie.kogito.swf.tools.custom.dashboard.impl;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.kie.kogito.swf.tools.custom.dashboard.CustomDashboardStorage;
import org.kie.kogito.swf.tools.custom.dashboard.model.CustomDashboardFilter;
import org.kie.kogito.swf.tools.custom.dashboard.model.CustomDashboardInfo;

import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

Expand All @@ -39,10 +43,11 @@ public class CustomDashboardStorageTest {
private static String DASHBOARD_NAME = "age.dash.yml";

private CustomDashboardStorage customDashboardStorage;
private URL tempFolder;

@BeforeAll
public void init() {
URL tempFolder = Thread.currentThread().getContextClassLoader().getResource("custom/dashboards/");
tempFolder = Thread.currentThread().getContextClassLoader().getResource("custom/dashboards/");

customDashboardStorage = new CustomDashboardStorageImpl(tempFolder);
}
Expand All @@ -64,6 +69,42 @@ public void testGetFormInfoList() {
assertEquals(2, formInfos.size());
}

@Test
public void testHotReloading() throws IOException {
String storageUrl = Thread.currentThread().getContextClassLoader().getResource("custom/dashboards/").getFile();
File srcFile = new File(storageUrl + "products.dash.yaml");
File targetFile = new File(storageUrl + "copy.dash.yml");

assertEquals(false, targetFile.exists());
FileUtils.copyFile(srcFile, targetFile);
assertEquals(true, targetFile.exists());
await().atMost(20, TimeUnit.SECONDS).until(() -> testBeforeDelete());
Collection<CustomDashboardInfo> customDashboardInfoFilterAllBeforeDelete = customDashboardStorage.getCustomDashboardFiles(null);
assertEquals(3, customDashboardInfoFilterAllBeforeDelete.size());

assertEquals(true, targetFile.exists());
FileUtils.delete(targetFile);
assertEquals(false, targetFile.exists());
await().atMost(20, TimeUnit.SECONDS).until(() -> testAfterDelete());

Collection<CustomDashboardInfo> customDashboardInfoFilterAllAfterDelete = customDashboardStorage.getCustomDashboardFiles(null);
assertEquals(2, customDashboardInfoFilterAllAfterDelete.size());
}

private boolean testBeforeDelete() {
if (customDashboardStorage.getCustomDashboardFiles(null).size() == 3) {
return true;
}
return false;
}

private boolean testAfterDelete() {
if (customDashboardStorage.getCustomDashboardFiles(null).size() == 2) {
return true;
}
return false;
}

@Test
public void testGetFormContent() throws IOException {
String content = customDashboardStorage.getCustomDashboardFileContent(DASHBOARD_NAME);
Expand Down