Skip to content

Commit

Permalink
refactor: clear the template cache after uninstalling the theme (#7174)
Browse files Browse the repository at this point in the history
#### What type of PR is this?
/kind improvement
/area core
/milestone 2.20.x

#### What this PR does / why we need it:
卸载主题之后清理模板缓存

此问题是 #2970 的遗留问题

#### Does this PR introduce a user-facing change?

```release-note
None
```
  • Loading branch information
guqing authored Jan 3, 2025
1 parent 24f8d7b commit be6f044
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
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

0 comments on commit be6f044

Please # to comment.