21
21
import java .util .function .ToIntFunction ;
22
22
import java .util .regex .Matcher ;
23
23
import java .util .stream .Stream ;
24
+ import java .util .concurrent .CompletableFuture ;
25
+
26
+ import org .slf4j .Logger ;
27
+ import org .slf4j .LoggerFactory ;
24
28
25
29
/**
26
30
* 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 {
41
45
};
42
46
43
47
private static final String TITLE_OPTION = "title" ;
48
+ private static final Logger logger = LoggerFactory .getLogger (GitHubCommand .class );
44
49
45
50
private final GitHubReference reference ;
46
51
47
- private Instant lastCacheUpdate ;
48
- private List <String > autocompleteGHIssueCache ;
52
+ private Instant lastCacheUpdate = Instant . EPOCH ;
53
+ private List <String > autocompleteGHIssueCache = List . of () ;
49
54
50
55
/**
51
56
* Constructs an instance of GitHubCommand.
@@ -66,7 +71,14 @@ public GitHubCommand(GitHubReference reference) {
66
71
getData ().addOption (OptionType .STRING , TITLE_OPTION ,
67
72
"Title of the issue you're looking for" , true , true );
68
73
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
+
70
82
}
71
83
72
84
@ Override
@@ -111,7 +123,7 @@ public void onAutoComplete(CommandAutoCompleteInteractionEvent event) {
111
123
event .replyChoiceStrings (choices ).queue ();
112
124
}
113
125
114
- if (lastCacheUpdate . isAfter ( Instant . now (). minus ( CACHE_EXPIRES_AFTER ) )) {
126
+ if (isCacheExpired ( )) {
115
127
updateCache ();
116
128
}
117
129
}
@@ -121,12 +133,20 @@ private ToIntFunction<String> suggestionScorer(String title) {
121
133
return s -> StringDistances .editDistance (title , s .replaceFirst ("\\ [#\\ d+] " , "" ));
122
134
}
123
135
136
+ private boolean isCacheExpired () {
137
+ Instant cacheExpiresAt = lastCacheUpdate .plus (CACHE_EXPIRES_AFTER );
138
+ return Instant .now ().isAfter (cacheExpiresAt );
139
+ }
140
+
124
141
private void updateCache () {
142
+ logger .debug ("GitHub Autocomplete cache update started" );
143
+
125
144
autocompleteGHIssueCache = reference .getRepositories ().stream ().map (repo -> {
126
145
try {
127
146
return repo .getIssues (GHIssueState .ALL );
128
147
} catch (IOException ex ) {
129
- throw new UncheckedIOException (ex );
148
+ throw new UncheckedIOException ("Error fetching issues from repo " + repo .getName (),
149
+ ex );
130
150
}
131
151
})
132
152
.flatMap (List ::stream )
@@ -135,5 +155,8 @@ private void updateCache() {
135
155
.toList ();
136
156
137
157
lastCacheUpdate = Instant .now ();
158
+
159
+ logger .debug ("GitHub autocomplete cache update completed successfully. Cached {} issues." ,
160
+ autocompleteGHIssueCache .size ());
138
161
}
139
162
}
0 commit comments