Skip to content

Commit 675e94a

Browse files
committed
Moved the mono instantiation from constructor to refresh method
1 parent 169c243 commit 675e94a

File tree

3 files changed

+26
-28
lines changed

3 files changed

+26
-28
lines changed

api/src/main/java/io/kafbat/ui/service/ApplicationInfoService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private List<EnabledFeaturesEnum> getEnabledFeatures() {
7373
// updating on startup and every hour
7474
@Scheduled(fixedRateString = "${github-release-info-update-rate:3600000}")
7575
public void updateGithubReleaseInfo() {
76-
githubReleaseInfo.refresh().subscribe();
76+
githubReleaseInfo.refresh();
7777
}
7878

7979
@VisibleForTesting
Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.kafbat.ui.util;
22

3-
import com.google.common.annotations.VisibleForTesting;
43
import java.time.Duration;
54
import lombok.Getter;
65
import lombok.extern.slf4j.Slf4j;
@@ -9,50 +8,48 @@
98
@Slf4j
109
public class GithubReleaseInfo {
1110
public static final String GITHUB_RELEASE_INFO_TIMEOUT = "github.release.info.timeout";
12-
1311
private static final String GITHUB_LATEST_RELEASE_RETRIEVAL_URL =
1412
"https://api.github.com/repos/kafbat/kafka-ui/releases/latest";
1513

1614
public record GithubReleaseDto(String html_url, String tag_name, String published_at) {
17-
1815
static GithubReleaseDto empty() {
1916
return new GithubReleaseDto(null, null, null);
2017
}
2118
}
2219

23-
private volatile GithubReleaseDto release = GithubReleaseDto.empty();
24-
25-
private final Mono<Void> refreshMono;
26-
2720
@Getter
2821
private final int githubApiMaxWaitTime;
22+
private volatile GithubReleaseDto release;
2923

3024
public GithubReleaseInfo(int githubApiMaxWaitTime) {
31-
this(GITHUB_LATEST_RELEASE_RETRIEVAL_URL, githubApiMaxWaitTime);
25+
this.githubApiMaxWaitTime = githubApiMaxWaitTime;
3226
}
3327

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()
3843
.get()
3944
.uri(url)
4045
.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))
4348
.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();
4851
}
4952

50-
public GithubReleaseDto get() {
51-
return release;
52-
}
5353

54-
public Mono<Void> refresh() {
55-
return refreshMono;
56-
}
5754

5855
}

api/src/test/java/io/kafbat/ui/util/GithubReleaseInfoTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ void stopMockServer() throws IOException {
2525

2626
@Test
2727
void test() {
28+
var infoHolder = new GithubReleaseInfo(10);
29+
var url = mockWebServer.url("repos/kafbat/kafka-ui/releases/latest").toString();
30+
2831
mockWebServer.enqueue(new MockResponse()
2932
.addHeader("content-type: application/json")
3033
.setBody("""
@@ -35,10 +38,8 @@ void test() {
3538
"some_unused_prop": "ololo"
3639
}
3740
"""));
38-
var url = mockWebServer.url("repos/kafbat/kafka-ui/releases/latest").toString();
3941

40-
var infoHolder = new GithubReleaseInfo(url, 10);
41-
infoHolder.refresh().block();
42+
infoHolder.refresh(url);
4243

4344
var i = infoHolder.get();
4445
assertThat(i.html_url())

0 commit comments

Comments
 (0)