Add a system config changed event (#7502)

pull/7506/head
John Niang 2025-06-05 17:43:24 +08:00 committed by GitHub
commit 54fad8d59a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 38 additions and 1 deletions

View File

@ -0,0 +1,17 @@
package run.halo.app.infra;
import org.springframework.context.ApplicationEvent;
/**
* Event that is published when the system configuration changes.
*
* @author johnniang
* @since 2.21.0
*/
public class SystemConfigChangedEvent extends ApplicationEvent {
public SystemConfigChangedEvent(Object source) {
super(source);
}
}

View File

@ -12,6 +12,7 @@ import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.core.convert.ConversionService;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
@ -39,12 +40,15 @@ import run.halo.app.infra.utils.JsonUtils;
public class SystemConfigurableEnvironmentFetcher implements Reconciler<Reconciler.Request> {
private final ReactiveExtensionClient extensionClient;
private final ConversionService conversionService;
private final ApplicationEventPublisher eventPublisher;
private final AtomicReference<ConfigMap> configMapCache = new AtomicReference<>();
public SystemConfigurableEnvironmentFetcher(ReactiveExtensionClient extensionClient,
ConversionService conversionService) {
ConversionService conversionService,
ApplicationEventPublisher eventPublisher) {
this.extensionClient = extensionClient;
this.conversionService = conversionService;
this.eventPublisher = eventPublisher;
}
public <T> Mono<T> fetch(String key, Class<T> type) {
@ -172,6 +176,7 @@ public class SystemConfigurableEnvironmentFetcher implements Reconciler<Reconcil
.switchIfEmpty(Mono.error(new IllegalStateException("System configMap not found.")))
.doOnNext(configMapCache::set)
.block();
eventPublisher.publishEvent(new SystemConfigChangedEvent(this));
return Result.doNotRetry();
}

View File

@ -1,8 +1,11 @@
package run.halo.app.infra;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.verify;
import java.util.LinkedHashMap;
import org.json.JSONException;
@ -13,11 +16,13 @@ import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.skyscreamer.jsonassert.JSONAssert;
import org.springframework.context.ApplicationEventPublisher;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import run.halo.app.extension.ConfigMap;
import run.halo.app.extension.Metadata;
import run.halo.app.extension.ReactiveExtensionClient;
import run.halo.app.extension.controller.Reconciler;
import run.halo.app.infra.utils.JsonUtils;
/**
@ -32,6 +37,9 @@ class SystemConfigurableEnvironmentFetcherTest {
@Mock
private ReactiveExtensionClient client;
@Mock
ApplicationEventPublisher eventPublisher;
@InjectMocks
private SystemConfigurableEnvironmentFetcher environmentFetcher;
@ -43,6 +51,13 @@ class SystemConfigurableEnvironmentFetcherTest {
.thenReturn(Mono.just(system()));
}
@Test
void shouldPublishSystemConfigChangedEvent() {
var result = environmentFetcher.reconcile(new Reconciler.Request("system"));
assertTrue(result == null || !result.reEnqueue());
verify(eventPublisher).publishEvent(isA(SystemConfigChangedEvent.class));
}
@Test
void loadConfigMap() {
environmentFetcher.loadConfigMap()