Skip to content

Commit b905efb

Browse files
authored
Made GitHub cache update async and fixed cache refresh check. Fixes #… (#1316)
* Made GitHub cache update async and fixed cache refresh check. Fixes #1315 * Apply Spotless formatting
1 parent 637c616 commit b905efb

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

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

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
55
import net.dv8tion.jda.api.interactions.commands.OptionType;
66
import org.kohsuke.github.GHIssue;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
79

810
import org.togetherjava.tjbot.features.CommandVisibility;
911
import org.togetherjava.tjbot.features.SlashCommandAdapter;
@@ -17,6 +19,7 @@
1719
import java.util.List;
1820
import java.util.PriorityQueue;
1921
import java.util.Queue;
22+
import java.util.concurrent.CompletableFuture;
2023
import java.util.function.ToIntFunction;
2124
import java.util.regex.Matcher;
2225
import java.util.stream.Stream;
@@ -40,11 +43,12 @@ public final class GitHubCommand extends SlashCommandAdapter {
4043
};
4144

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

4448
private final GitHubReference reference;
4549

46-
private Instant lastCacheUpdate;
47-
private List<String> autocompleteGHIssueCache;
50+
private Instant lastCacheUpdate = Instant.EPOCH;
51+
private List<String> autocompleteGHIssueCache = List.of();
4852

4953
/**
5054
* Constructs an instance of GitHubCommand.
@@ -65,7 +69,14 @@ public GitHubCommand(GitHubReference reference) {
6569
getData().addOption(OptionType.STRING, TITLE_OPTION,
6670
"Title of the issue you're looking for", true, true);
6771

68-
updateCache();
72+
CompletableFuture.runAsync(() -> {
73+
try {
74+
updateCache();
75+
} catch (Exception e) {
76+
logger.error("Unknown error updating the GitHub cache", e);
77+
}
78+
});
79+
6980
}
7081

7182
@Override
@@ -110,7 +121,7 @@ public void onAutoComplete(CommandAutoCompleteInteractionEvent event) {
110121
event.replyChoiceStrings(choices).queue();
111122
}
112123

113-
if (lastCacheUpdate.isAfter(Instant.now().minus(CACHE_EXPIRES_AFTER))) {
124+
if (isCacheExpired()) {
114125
updateCache();
115126
}
116127
}
@@ -120,12 +131,20 @@ private ToIntFunction<String> suggestionScorer(String title) {
120131
return s -> StringDistances.editDistance(title, s.replaceFirst("\\[#\\d+] ", ""));
121132
}
122133

134+
private boolean isCacheExpired() {
135+
Instant cacheExpiresAt = lastCacheUpdate.plus(CACHE_EXPIRES_AFTER);
136+
return Instant.now().isAfter(cacheExpiresAt);
137+
}
138+
123139
private void updateCache() {
140+
logger.debug("GitHub Autocomplete cache update started");
141+
124142
autocompleteGHIssueCache = reference.getRepositories().stream().map(repo -> {
125143
try {
126144
return repo.queryIssues().pageSize(1000).list().toList();
127145
} catch (IOException ex) {
128-
throw new UncheckedIOException(ex);
146+
throw new UncheckedIOException("Error fetching issues from repo " + repo.getName(),
147+
ex);
129148
}
130149
})
131150
.flatMap(List::stream)
@@ -134,5 +153,8 @@ private void updateCache() {
134153
.toList();
135154

136155
lastCacheUpdate = Instant.now();
156+
157+
logger.debug("GitHub autocomplete cache update completed successfully. Cached {} issues.",
158+
autocompleteGHIssueCache.size());
137159
}
138160
}

0 commit comments

Comments
 (0)