diff --git a/application/src/main/java/run/halo/app/extension/ReactiveExtensionClientImpl.java b/application/src/main/java/run/halo/app/extension/ReactiveExtensionClientImpl.java index 0962d64de2..38377c297e 100644 --- a/application/src/main/java/run/halo/app/extension/ReactiveExtensionClientImpl.java +++ b/application/src/main/java/run/halo/app/extension/ReactiveExtensionClientImpl.java @@ -19,6 +19,8 @@ import lombok.NonNull; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.availability.AvailabilityChangeEvent; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.event.EventListener; import org.springframework.dao.DataIntegrityViolationException; @@ -30,6 +32,7 @@ import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import reactor.util.retry.Retry; +import run.halo.app.extension.availability.IndexBuildState; import run.halo.app.extension.exception.ExtensionNotFoundException; import run.halo.app.extension.index.DefaultExtensionIterator; import run.halo.app.extension.index.ExtensionIterator; @@ -443,6 +446,7 @@ class IndexBuildsManager { private final ExtensionConverter converter; private final ReactiveExtensionStoreClient client; private final SchemeWatcherManager schemeWatcherManager; + private final ApplicationEventPublisher eventPublisher; @NonNull private ExtensionIterator createExtensionIterator(Scheme scheme) { @@ -459,6 +463,8 @@ private ExtensionIterator createExtensionIterator(Scheme scheme) { @EventListener(ContextRefreshedEvent.class) public void startBuildingIndex() { + AvailabilityChangeEvent.publish(eventPublisher, this, IndexBuildState.BUILDING); + final long startTimeMs = System.currentTimeMillis(); log.info("Start building index for all extensions, please wait..."); schemeManager.schemes() @@ -474,6 +480,8 @@ public void startBuildingIndex() { indexerFactory.removeIndexer(scheme); } }); + + AvailabilityChangeEvent.publish(eventPublisher, this, IndexBuildState.BUILT); log.info("Successfully built index in {}ms, Preparing to lunch application...", System.currentTimeMillis() - startTimeMs); } diff --git a/application/src/main/java/run/halo/app/extension/availability/IndexBuildState.java b/application/src/main/java/run/halo/app/extension/availability/IndexBuildState.java new file mode 100644 index 0000000000..909d46bff8 --- /dev/null +++ b/application/src/main/java/run/halo/app/extension/availability/IndexBuildState.java @@ -0,0 +1,8 @@ +package run.halo.app.extension.availability; + +import org.springframework.boot.availability.AvailabilityState; + +public enum IndexBuildState implements AvailabilityState { + BUILDING, + BUILT; +} diff --git a/application/src/main/java/run/halo/app/extension/availability/IndexBuildStateHealthIndicator.java b/application/src/main/java/run/halo/app/extension/availability/IndexBuildStateHealthIndicator.java new file mode 100644 index 0000000000..20c5a5dbca --- /dev/null +++ b/application/src/main/java/run/halo/app/extension/availability/IndexBuildStateHealthIndicator.java @@ -0,0 +1,22 @@ +package run.halo.app.extension.availability; + +import org.springframework.boot.actuate.availability.AvailabilityStateHealthIndicator; +import org.springframework.boot.actuate.health.Status; +import org.springframework.boot.availability.ApplicationAvailability; +import org.springframework.stereotype.Component; + +@Component +public class IndexBuildStateHealthIndicator extends AvailabilityStateHealthIndicator { + /** + * Create a {@link IndexBuildStateHealthIndicator} instance by {@link ApplicationAvailability}. + * Mapping {@link IndexBuildState} to {@link Status}. + * + * @see IndexBuildState + */ + public IndexBuildStateHealthIndicator(ApplicationAvailability availability) { + super(availability, IndexBuildState.class, (statusMappings) -> { + statusMappings.add(IndexBuildState.BUILT, Status.UP); + statusMappings.add(IndexBuildState.BUILDING, Status.OUT_OF_SERVICE); + }); + } +}