|
1 | 1 | package io.kafbat.ui.util; |
2 | 2 |
|
3 | | -import com.google.common.annotations.VisibleForTesting; |
4 | 3 | import java.time.Duration; |
5 | 4 | import lombok.Getter; |
6 | 5 | import lombok.extern.slf4j.Slf4j; |
|
9 | 8 | @Slf4j |
10 | 9 | public class GithubReleaseInfo { |
11 | 10 | public static final String GITHUB_RELEASE_INFO_TIMEOUT = "github.release.info.timeout"; |
12 | | - |
13 | 11 | private static final String GITHUB_LATEST_RELEASE_RETRIEVAL_URL = |
14 | 12 | "https://api.github.com/repos/kafbat/kafka-ui/releases/latest"; |
15 | 13 |
|
16 | 14 | public record GithubReleaseDto(String html_url, String tag_name, String published_at) { |
17 | | - |
18 | 15 | static GithubReleaseDto empty() { |
19 | 16 | return new GithubReleaseDto(null, null, null); |
20 | 17 | } |
21 | 18 | } |
22 | 19 |
|
23 | | - private volatile GithubReleaseDto release = GithubReleaseDto.empty(); |
24 | | - |
25 | | - private final Mono<Void> refreshMono; |
26 | | - |
27 | 20 | @Getter |
28 | 21 | private final int githubApiMaxWaitTime; |
| 22 | + private volatile GithubReleaseDto release; |
29 | 23 |
|
30 | 24 | public GithubReleaseInfo(int githubApiMaxWaitTime) { |
31 | | - this(GITHUB_LATEST_RELEASE_RETRIEVAL_URL, githubApiMaxWaitTime); |
| 25 | + this.githubApiMaxWaitTime = githubApiMaxWaitTime; |
32 | 26 | } |
33 | 27 |
|
34 | | - @VisibleForTesting |
35 | | - GithubReleaseInfo(String url, int githubApiMaxWaitTime) { |
36 | | - this.githubApiMaxWaitTime = githubApiMaxWaitTime; |
37 | | - this.refreshMono = new WebClientConfigurator().build() |
| 28 | + public GithubReleaseDto get() { |
| 29 | + if (release != null) { |
| 30 | + return release; |
| 31 | + } |
| 32 | + |
| 33 | + refresh(); |
| 34 | + return release == null ? GithubReleaseDto.empty() : release; |
| 35 | + } |
| 36 | + |
| 37 | + public void refresh() { |
| 38 | + refresh(GITHUB_LATEST_RELEASE_RETRIEVAL_URL); |
| 39 | + } |
| 40 | + |
| 41 | + public void refresh(String url) { |
| 42 | + new WebClientConfigurator().build() |
38 | 43 | .get() |
39 | 44 | .uri(url) |
40 | 45 | .exchangeToMono(resp -> resp.bodyToMono(GithubReleaseDto.class)) |
41 | | - .timeout(Duration.ofSeconds(this.githubApiMaxWaitTime)) |
42 | | - .doOnError(th -> log.trace("Error getting latest github release info", th)) |
| 46 | + .timeout(Duration.ofSeconds(githubApiMaxWaitTime)) |
| 47 | + .doOnError(th -> log.error("Failed to retrieve latest release info", th)) |
43 | 48 | .onErrorResume(th -> true, th -> Mono.just(GithubReleaseDto.empty())) |
44 | | - .doOnNext(release -> this.release = release) |
45 | | - .then(); |
46 | | - |
47 | | - this.refreshMono.block(); |
| 49 | + .doOnNext(r -> this.release = r) |
| 50 | + .block(); |
48 | 51 | } |
49 | 52 |
|
50 | | - public GithubReleaseDto get() { |
51 | | - return release; |
52 | | - } |
53 | 53 |
|
54 | | - public Mono<Void> refresh() { |
55 | | - return refreshMono; |
56 | | - } |
57 | 54 |
|
58 | 55 | } |
0 commit comments