Skip to content

Commit 15db2a7

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

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

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

Lines changed: 21 additions & 4 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 (lastCacheUpdate.isBefore(Instant.now().minus(CACHE_EXPIRES_AFTER))) {
115127
updateCache();
116128
}
117129
}
@@ -122,10 +134,13 @@ private ToIntFunction<String> suggestionScorer(String title) {
122134
}
123135

124136
private void updateCache() {
137+
logger.debug("GitHub Autocomplete cache update started"); // log start
138+
125139
autocompleteGHIssueCache = reference.getRepositories().stream().map(repo -> {
126140
try {
127141
return repo.getIssues(GHIssueState.ALL);
128142
} catch (IOException ex) {
143+
logger.error("Error fetching issues from repo {}", repo.getName(), ex);
129144
throw new UncheckedIOException(ex);
130145
}
131146
})
@@ -135,5 +150,7 @@ private void updateCache() {
135150
.toList();
136151

137152
lastCacheUpdate = Instant.now();
153+
154+
logger.debug("GitHub autocomplete cache update completed successfully. Cached {} issues.", autocompleteGHIssueCache.size()); // log end
138155
}
139156
}

0 commit comments

Comments
 (0)