From 11671eacb28ce9a4734dab2c9b93dc459bf31d99 Mon Sep 17 00:00:00 2001 From: Mehak Walia Date: Wed, 3 Sep 2025 05:05:26 -0700 Subject: [PATCH 1/2] Made GitHub cache update async and fixed cache refresh check. Fixes #1315 --- .../tjbot/features/github/GitHubCommand.java | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/github/GitHubCommand.java b/application/src/main/java/org/togetherjava/tjbot/features/github/GitHubCommand.java index 53a7eed187..d4ecf77227 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/github/GitHubCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/github/GitHubCommand.java @@ -21,6 +21,10 @@ import java.util.function.ToIntFunction; import java.util.regex.Matcher; import java.util.stream.Stream; +import java.util.concurrent.CompletableFuture; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * 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 { }; private static final String TITLE_OPTION = "title"; + private static final Logger logger = LoggerFactory.getLogger(GitHubCommand.class); private final GitHubReference reference; - private Instant lastCacheUpdate; - private List autocompleteGHIssueCache; + private Instant lastCacheUpdate = Instant.EPOCH; + private List autocompleteGHIssueCache = List.of(); /** * Constructs an instance of GitHubCommand. @@ -66,7 +71,14 @@ public GitHubCommand(GitHubReference reference) { getData().addOption(OptionType.STRING, TITLE_OPTION, "Title of the issue you're looking for", true, true); - updateCache(); + CompletableFuture.runAsync(() -> { + try { + updateCache(); + } catch (Exception e) { + logger.error("Unknown error updating the GitHub cache", e); + } + }); + } @Override @@ -111,7 +123,7 @@ public void onAutoComplete(CommandAutoCompleteInteractionEvent event) { event.replyChoiceStrings(choices).queue(); } - if (lastCacheUpdate.isAfter(Instant.now().minus(CACHE_EXPIRES_AFTER))) { + if (isCacheExpired()) { updateCache(); } } @@ -121,12 +133,20 @@ private ToIntFunction suggestionScorer(String title) { return s -> StringDistances.editDistance(title, s.replaceFirst("\\[#\\d+] ", "")); } + private boolean isCacheExpired() { + Instant cacheExpiresAt = lastCacheUpdate.plus(CACHE_EXPIRES_AFTER); + return Instant.now().isAfter(cacheExpiresAt); + } + private void updateCache() { + logger.debug("GitHub Autocomplete cache update started"); + autocompleteGHIssueCache = reference.getRepositories().stream().map(repo -> { try { return repo.getIssues(GHIssueState.ALL); } catch (IOException ex) { - throw new UncheckedIOException(ex); + throw new UncheckedIOException("Error fetching issues from repo " + repo.getName(), + ex); } }) .flatMap(List::stream) @@ -135,5 +155,8 @@ private void updateCache() { .toList(); lastCacheUpdate = Instant.now(); + + logger.debug("GitHub autocomplete cache update completed successfully. Cached {} issues.", + autocompleteGHIssueCache.size()); } } From 20a27b5d469d2f04e9d162c7048fb006598bb9b5 Mon Sep 17 00:00:00 2001 From: Mehak Walia Date: Fri, 5 Sep 2025 02:28:23 -0700 Subject: [PATCH 2/2] Apply Spotless formatting --- .../togetherjava/tjbot/features/github/GitHubCommand.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/github/GitHubCommand.java b/application/src/main/java/org/togetherjava/tjbot/features/github/GitHubCommand.java index 81c0ab151c..b52e79550d 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/github/GitHubCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/github/GitHubCommand.java @@ -4,6 +4,8 @@ import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.interactions.commands.OptionType; import org.kohsuke.github.GHIssue; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.features.CommandVisibility; import org.togetherjava.tjbot.features.SlashCommandAdapter; @@ -17,13 +19,10 @@ import java.util.List; import java.util.PriorityQueue; import java.util.Queue; +import java.util.concurrent.CompletableFuture; import java.util.function.ToIntFunction; import java.util.regex.Matcher; import java.util.stream.Stream; -import java.util.concurrent.CompletableFuture; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * Slash command (/github-search) used to search for an issue in one of the repositories listed in