44import net .dv8tion .jda .api .events .interaction .command .SlashCommandInteractionEvent ;
55import net .dv8tion .jda .api .interactions .commands .OptionType ;
66import org .kohsuke .github .GHIssue ;
7+ import org .slf4j .Logger ;
8+ import org .slf4j .LoggerFactory ;
79
810import org .togetherjava .tjbot .features .CommandVisibility ;
911import org .togetherjava .tjbot .features .SlashCommandAdapter ;
1719import java .util .List ;
1820import java .util .PriorityQueue ;
1921import java .util .Queue ;
22+ import java .util .concurrent .CompletableFuture ;
2023import java .util .function .ToIntFunction ;
2124import java .util .regex .Matcher ;
2225import 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