4
4
import net .dv8tion .jda .api .events .interaction .command .SlashCommandInteractionEvent ;
5
5
import net .dv8tion .jda .api .interactions .commands .OptionType ;
6
6
import org .kohsuke .github .GHIssue ;
7
+ import org .slf4j .Logger ;
8
+ import org .slf4j .LoggerFactory ;
7
9
8
10
import org .togetherjava .tjbot .features .CommandVisibility ;
9
11
import org .togetherjava .tjbot .features .SlashCommandAdapter ;
17
19
import java .util .List ;
18
20
import java .util .PriorityQueue ;
19
21
import java .util .Queue ;
22
+ import java .util .concurrent .CompletableFuture ;
20
23
import java .util .function .ToIntFunction ;
21
24
import java .util .regex .Matcher ;
22
25
import java .util .stream .Stream ;
@@ -40,11 +43,12 @@ public final class GitHubCommand extends SlashCommandAdapter {
40
43
};
41
44
42
45
private static final String TITLE_OPTION = "title" ;
46
+ private static final Logger logger = LoggerFactory .getLogger (GitHubCommand .class );
43
47
44
48
private final GitHubReference reference ;
45
49
46
- private Instant lastCacheUpdate ;
47
- private List <String > autocompleteGHIssueCache ;
50
+ private Instant lastCacheUpdate = Instant . EPOCH ;
51
+ private List <String > autocompleteGHIssueCache = List . of () ;
48
52
49
53
/**
50
54
* Constructs an instance of GitHubCommand.
@@ -65,7 +69,14 @@ public GitHubCommand(GitHubReference reference) {
65
69
getData ().addOption (OptionType .STRING , TITLE_OPTION ,
66
70
"Title of the issue you're looking for" , true , true );
67
71
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
+
69
80
}
70
81
71
82
@ Override
@@ -110,7 +121,7 @@ public void onAutoComplete(CommandAutoCompleteInteractionEvent event) {
110
121
event .replyChoiceStrings (choices ).queue ();
111
122
}
112
123
113
- if (lastCacheUpdate . isAfter ( Instant . now (). minus ( CACHE_EXPIRES_AFTER ) )) {
124
+ if (isCacheExpired ( )) {
114
125
updateCache ();
115
126
}
116
127
}
@@ -120,12 +131,20 @@ private ToIntFunction<String> suggestionScorer(String title) {
120
131
return s -> StringDistances .editDistance (title , s .replaceFirst ("\\ [#\\ d+] " , "" ));
121
132
}
122
133
134
+ private boolean isCacheExpired () {
135
+ Instant cacheExpiresAt = lastCacheUpdate .plus (CACHE_EXPIRES_AFTER );
136
+ return Instant .now ().isAfter (cacheExpiresAt );
137
+ }
138
+
123
139
private void updateCache () {
140
+ logger .debug ("GitHub Autocomplete cache update started" );
141
+
124
142
autocompleteGHIssueCache = reference .getRepositories ().stream ().map (repo -> {
125
143
try {
126
144
return repo .queryIssues ().pageSize (1000 ).list ().toList ();
127
145
} catch (IOException ex ) {
128
- throw new UncheckedIOException (ex );
146
+ throw new UncheckedIOException ("Error fetching issues from repo " + repo .getName (),
147
+ ex );
129
148
}
130
149
})
131
150
.flatMap (List ::stream )
@@ -134,5 +153,8 @@ private void updateCache() {
134
153
.toList ();
135
154
136
155
lastCacheUpdate = Instant .now ();
156
+
157
+ logger .debug ("GitHub autocomplete cache update completed successfully. Cached {} issues." ,
158
+ autocompleteGHIssueCache .size ());
137
159
}
138
160
}
0 commit comments