-
Notifications
You must be signed in to change notification settings - Fork 3
Moved dependency ready check here from dd-dataverse-ingest. #29
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
b7e7deb
Moved dependency ready check here from dd-datavers-ingest.
janvanmansum 18b2286
Update src/main/java/nl/knaw/dans/lib/util/healthcheck/HealthChecksDe…
janvanmansum 9ee9e92
Update src/main/java/nl/knaw/dans/lib/util/healthcheck/HealthChecksDe…
janvanmansum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/main/java/nl/knaw/dans/lib/util/healthcheck/DependenciesReadyCheck.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /* | ||
| * Copyright (C) 2021 DANS - Data Archiving and Networked Services (info@dans.knaw.nl) | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package nl.knaw.dans.lib.util.healthcheck; | ||
|
|
||
| /** | ||
| * Interface for checking if all dependencies are ready. | ||
| */ | ||
| public interface DependenciesReadyCheck { | ||
| /** | ||
| * Wait until all dependencies are ready. This method should block until all dependencies are ready. | ||
| * | ||
| * @param checks the names of the checks to wait for; if empty, all checks are waited for | ||
| */ | ||
| void waitUntilReady(String... checks); | ||
| } |
33 changes: 33 additions & 0 deletions
33
src/main/java/nl/knaw/dans/lib/util/healthcheck/DependenciesReadyCheckConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| * Copyright (C) 2021 DANS - Data Archiving and Networked Services (info@dans.knaw.nl) | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package nl.knaw.dans.lib.util.healthcheck; | ||
|
|
||
| import io.dropwizard.util.Duration; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Data; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Data | ||
| @Builder | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class DependenciesReadyCheckConfig { | ||
| private List<String> healthChecks; | ||
| private Duration pollInterval; | ||
| } |
106 changes: 106 additions & 0 deletions
106
src/main/java/nl/knaw/dans/lib/util/healthcheck/HealthChecksDependenciesReadyCheck.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /* | ||
| * Copyright (C) 2021 DANS - Data Archiving and Networked Services (info@dans.knaw.nl) | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package nl.knaw.dans.lib.util.healthcheck; | ||
|
|
||
| import com.codahale.metrics.health.HealthCheck; | ||
| import io.dropwizard.core.setup.Environment; | ||
| import io.dropwizard.lifecycle.Managed; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import static java.lang.Thread.sleep; | ||
|
|
||
| /** | ||
| * Implementation of {@link DependenciesReadyCheck} that waits until all health checks are healthy. | ||
| */ | ||
| @Slf4j | ||
| public class HealthChecksDependenciesReadyCheck implements DependenciesReadyCheck, Managed { | ||
| private final List<HealthCheck> healthChecks = new ArrayList<>(); | ||
| private final Environment environment; | ||
| private final DependenciesReadyCheckConfig config; | ||
|
|
||
| private long pollInterval; | ||
| private boolean running = false; | ||
|
|
||
| public HealthChecksDependenciesReadyCheck(Environment environment, DependenciesReadyCheckConfig config) { | ||
| this.environment = environment; | ||
| this.config = config; | ||
| } | ||
|
|
||
| @Override | ||
| public void start() throws Exception { | ||
| for (var name : config.getHealthChecks()) { | ||
| var healthCheck = environment.healthChecks().getHealthCheck(name); | ||
| if (healthCheck == null) { | ||
| throw new IllegalArgumentException("Health check with name " + name + " not found"); | ||
| } | ||
| healthChecks.add(healthCheck); | ||
| } | ||
| pollInterval = config.getPollInterval().toMilliseconds(); | ||
| running = true; | ||
| } | ||
|
|
||
| @Override | ||
| public void stop() { | ||
| running = false; | ||
| } | ||
|
|
||
| @Override | ||
| public void waitUntilReady(String... checks) { | ||
| List<HealthCheck> checksToWaitFor = getChecksToWaitFor(checks); | ||
| while (running && !allHealthy(checksToWaitFor)) { | ||
| log.warn("Not all health checks are healthy yet, waiting for {} ms", pollInterval); | ||
| try { | ||
| sleep(pollInterval); | ||
| } | ||
| catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| log.warn("Interrupted while waiting for health checks to become healthy, stopping wait"); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private List<HealthCheck> getChecksToWaitFor(String... checks) { | ||
| if (checks.length == 0) { | ||
| return healthChecks; | ||
| } | ||
| var checkNames = Arrays.asList(checks); | ||
| var registeredChecks = environment.healthChecks(); | ||
| return checkNames.stream() | ||
| .map(name -> { | ||
| var healthCheck = registeredChecks.getHealthCheck(name); | ||
| if (healthCheck == null) { | ||
| throw new IllegalArgumentException("Health check with name " + name + " not found"); | ||
| } | ||
| return healthCheck; | ||
| }) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private boolean allHealthy(List<HealthCheck> checksToWaitFor) { | ||
| for (var healthCheck : checksToWaitFor) { | ||
| if (!healthCheck.execute().isHealthy()) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.