Skip to content

Commit 11671ea

Browse files
committed
Made GitHub cache update async and fixed cache refresh check. Fixes #1315
1 parent 844a41a commit 11671ea

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

application/src/main/java/org/togetherjava/tjbot/features/github/GitHubCommand.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
import java.util.function.ToIntFunction;
2222
import java.util.regex.Matcher;
2323
import java.util.stream.Stream;
24+
import java.util.concurrent.CompletableFuture;
25+
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
2428

2529
/**
2630
* Slash command (/github-search) used to search for an issue in one of the repositories listed in
@@ -41,11 +45,12 @@ public final class GitHubCommand extends SlashCommandAdapter {
4145
};
4246

4347
private static final String TITLE_OPTION = "title";
48+
private static final Logger logger = LoggerFactory.getLogger(GitHubCommand.class);
4449

4550
private final GitHubReference reference;
4651

47-
private Instant lastCacheUpdate;
48-
private List<String> autocompleteGHIssueCache;
52+
private Instant lastCacheUpdate = Instant.EPOCH;
53+
private List<String> autocompleteGHIssueCache = List.of();
4954

5055
/**
5156
* Constructs an instance of GitHubCommand.
@@ -66,7 +71,14 @@ public GitHubCommand(GitHubReference reference) {
6671
getData().addOption(OptionType.STRING, TITLE_OPTION,
6772
"Title of the issue you're looking for", true, true);
6873

69-
updateCache();
74+
CompletableFuture.runAsync(() -> {
75+
try {
76+
updateCache();
77+
} catch (Exception e) {
78+
logger.error("Unknown error updating the GitHub cache", e);
79+
}
80+
});
81+
7082
}
7183

7284
@Override
@@ -111,7 +123,7 @@ public void onAutoComplete(CommandAutoCompleteInteractionEvent event) {
111123
event.replyChoiceStrings(choices).queue();
112124
}
113125

114-
if (lastCacheUpdate.isAfter(Instant.now().minus(CACHE_EXPIRES_AFTER))) {
126+
if (isCacheExpired()) {
115127
updateCache();
116128
}
117129
}
@@ -121,12 +133,20 @@ private ToIntFunction<String> suggestionScorer(String title) {
121133
return s -> StringDistances.editDistance(title, s.replaceFirst("\\[#\\d+] ", ""));
122134
}
123135

136+
private boolean isCacheExpired() {
137+
Instant cacheExpiresAt = lastCacheUpdate.plus(CACHE_EXPIRES_AFTER);
138+
return Instant.now().isAfter(cacheExpiresAt);
139+
}
140+
124141
private void updateCache() {
142+
logger.debug("GitHub Autocomplete cache update started");
143+
125144
autocompleteGHIssueCache = reference.getRepositories().stream().map(repo -> {
126145
try {
127146
return repo.getIssues(GHIssueState.ALL);
128147
} catch (IOException ex) {
129-
throw new UncheckedIOException(ex);
148+
throw new UncheckedIOException("Error fetching issues from repo " + repo.getName(),
149+
ex);
130150
}
131151
})
132152
.flatMap(List::stream)
@@ -135,5 +155,8 @@ private void updateCache() {
135155
.toList();
136156

137157
lastCacheUpdate = Instant.now();
158+
159+
logger.debug("GitHub autocomplete cache update completed successfully. Cached {} issues.",
160+
autocompleteGHIssueCache.size());
138161
}
139162
}

0 commit comments

Comments
 (0)