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 (lastCacheUpdate .isBefore (Instant .now ().minus (CACHE_EXPIRES_AFTER ))) {
115
127
updateCache ();
116
128
}
117
129
}
@@ -122,10 +134,13 @@ private ToIntFunction<String> suggestionScorer(String title) {
122
134
}
123
135
124
136
private void updateCache () {
137
+ logger .debug ("GitHub Autocomplete cache update started" ); // log start
138
+
125
139
autocompleteGHIssueCache = reference .getRepositories ().stream ().map (repo -> {
126
140
try {
127
141
return repo .getIssues (GHIssueState .ALL );
128
142
} catch (IOException ex ) {
143
+ logger .error ("Error fetching issues from repo {}" , repo .getName (), ex );
129
144
throw new UncheckedIOException (ex );
130
145
}
131
146
})
@@ -135,5 +150,7 @@ private void updateCache() {
135
150
.toList ();
136
151
137
152
lastCacheUpdate = Instant .now ();
153
+
154
+ logger .debug ("GitHub autocomplete cache update completed successfully. Cached {} issues." , autocompleteGHIssueCache .size ()); // log end
138
155
}
139
156
}
0 commit comments