Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

refactor: clear the template cache after uninstalling the theme #7174

Merged
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 @@ -10,6 +10,7 @@
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.stereotype.Component;
Expand All @@ -31,6 +32,7 @@
import run.halo.app.infra.utils.JsonUtils;
import run.halo.app.infra.utils.SettingUtils;
import run.halo.app.infra.utils.VersionUtils;
import run.halo.app.theme.TemplateEngineManager;

/**
* Reconciler for theme.
Expand All @@ -39,27 +41,22 @@
* @since 2.0.0
*/
@Component
@RequiredArgsConstructor
public class ThemeReconciler implements Reconciler<Request> {
private static final String FINALIZER_NAME = "theme-protection";

private final ExtensionClient client;

private final ThemeRootGetter themeRoot;
private final SystemVersionSupplier systemVersionSupplier;
private final TemplateEngineManager templateEngineManager;

private final RetryTemplate retryTemplate = RetryTemplate.builder()
.maxAttempts(20)
.fixedBackoff(300)
.retryOn(IllegalStateException.class)
.build();

public ThemeReconciler(ExtensionClient client, ThemeRootGetter themeRoot,
SystemVersionSupplier systemVersionSupplier) {
this.client = client;
this.themeRoot = themeRoot;
this.systemVersionSupplier = systemVersionSupplier;
}

@Override
public Result reconcile(Request request) {
client.fetch(Theme.class, request.name())
Expand Down Expand Up @@ -173,6 +170,7 @@ private void cleanUpResourcesAndRemoveFinalizer(String themeName) {
}

private void reconcileThemeDeletion(Theme theme) {
templateEngineManager.clearCache(theme.getMetadata().getName()).block();
deleteThemeFiles(theme);
// delete theme setting form
String settingName = theme.getSpec().getSettingName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
import org.springframework.retry.RetryException;
import org.springframework.util.FileSystemUtils;
import org.springframework.util.ResourceUtils;
import reactor.core.publisher.Mono;
import run.halo.app.core.extension.AnnotationSetting;
import run.halo.app.core.extension.Setting;
import run.halo.app.core.extension.Theme;
import run.halo.app.core.reconciler.ThemeReconciler;
import run.halo.app.extension.ConfigMap;
import run.halo.app.extension.ExtensionClient;
import run.halo.app.extension.Metadata;
Expand All @@ -44,6 +44,7 @@
import run.halo.app.infra.SystemVersionSupplier;
import run.halo.app.infra.ThemeRootGetter;
import run.halo.app.infra.utils.JsonUtils;
import run.halo.app.theme.TemplateEngineManager;

/**
* Tests for {@link ThemeReconciler}.
Expand All @@ -66,6 +67,9 @@ class ThemeReconcilerTest {
@Mock
private File defaultTheme;

@Mock
private TemplateEngineManager templateEngineManager;

@InjectMocks
ThemeReconciler themeReconciler;

Expand All @@ -76,6 +80,7 @@ class ThemeReconcilerTest {
void setUp() throws IOException {
defaultTheme = ResourceUtils.getFile("classpath:themes/default");
lenient().when(systemVersionSupplier.get()).thenReturn(Version.parse("0.0.0"));
lenient().when(templateEngineManager.clearCache(any())).thenReturn(Mono.empty());
}

@Test
Expand Down Expand Up @@ -128,7 +133,8 @@ void reconcileDeleteRetry() {
when(themeRoot.get()).thenReturn(testWorkDir);

final ThemeReconciler themeReconciler =
new ThemeReconciler(extensionClient, themeRoot, systemVersionSupplier);
new ThemeReconciler(extensionClient, themeRoot, systemVersionSupplier,
templateEngineManager);

final int[] retryFlags = {0, 0};
when(extensionClient.fetch(eq(Setting.class), eq("theme-test-setting")))
Expand Down Expand Up @@ -157,6 +163,7 @@ void reconcileDeleteRetry() {
verify(extensionClient, times(2)).fetch(eq(Theme.class), eq(metadata.getName()));
verify(extensionClient, times(3)).fetch(eq(Setting.class), eq(settingName));
verify(extensionClient, times(3)).list(eq(AnnotationSetting.class), any(), eq(null));
verify(templateEngineManager).clearCache(eq(metadata.getName()));
}

@Test
Expand All @@ -167,7 +174,8 @@ void reconcileDeleteRetryWhenThrowException() {
when(themeRoot.get()).thenReturn(testWorkDir);

final ThemeReconciler themeReconciler =
new ThemeReconciler(extensionClient, themeRoot, systemVersionSupplier);
new ThemeReconciler(extensionClient, themeRoot, systemVersionSupplier,
templateEngineManager);

final int[] retryFlags = {0};
when(extensionClient.fetch(eq(Setting.class), eq("theme-test-setting")))
Expand Down Expand Up @@ -196,7 +204,8 @@ void reconcileStatus() {
when(themeRoot.get()).thenReturn(testWorkDir);

final ThemeReconciler themeReconciler =
new ThemeReconciler(extensionClient, themeRoot, systemVersionSupplier);
new ThemeReconciler(extensionClient, themeRoot, systemVersionSupplier,
templateEngineManager);
Theme theme = fakeTheme();
theme.setStatus(null);
theme.getSpec().setRequires(">2.3.0");
Expand Down
Loading