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

feat: add index build state as readiness check indicator #6700

Merged
merged 1 commit into from
Sep 28, 2024
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 @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Extension> createExtensionIterator(Scheme scheme) {
Expand All @@ -459,6 +463,8 @@ private ExtensionIterator<Extension> 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()
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package run.halo.app.extension.availability;

import org.springframework.boot.availability.AvailabilityState;

public enum IndexBuildState implements AvailabilityState {
BUILDING,
BUILT;
}
Original file line number Diff line number Diff line change
@@ -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);
});
}
}
Loading