31
31
import java .time .temporal .ChronoUnit ;
32
32
import java .util .List ;
33
33
import java .util .Optional ;
34
+ import java .util .concurrent .CompletableFuture ;
34
35
import java .util .concurrent .TimeUnit ;
35
36
import java .util .stream .Collectors ;
36
37
@@ -71,10 +72,16 @@ public void init() {
71
72
72
73
this .httpServer = createHttpServer ();
73
74
74
- LocalDateTime start = LocalDateTime .now ().truncatedTo (ChronoUnit .MINUTES ).plusMinutes (1 );
75
- long startDelay = Duration .between (LocalDateTime .now (), start ).plusMillis (500 ).toMillis (); // Add 0.5s to ensure we are not in the previous hour
75
+ // Add a random start delay to prevent important load on shared web hosts
76
+ // caused by many servers sending request at the same time
77
+ LocalDateTime start = LocalDateTime .now ()
78
+ .truncatedTo (ChronoUnit .MINUTES )
79
+ .plusMinutes (1 )
80
+ .plusSeconds (1 + (long ) (Math .random () * 30 ));
81
+ long startDelay = Duration .between (LocalDateTime .now (), start ).toMillis ();
82
+ long repeatDelay = TimeUnit .MINUTES .toMillis (1 );
76
83
77
- getScheduler ().executeAsyncRepeating (this .fetcherTask , startDelay , TimeUnit . MINUTES . toMillis ( 1 ) , TimeUnit .MILLISECONDS );
84
+ getScheduler ().executeAsyncRepeating (this .fetcherTask , startDelay , repeatDelay , TimeUnit .MILLISECONDS );
78
85
79
86
if (!this .config .isValid ()) {
80
87
getLogger ().warn ("Invalid configuration, please use '/azlink' to setup the plugin." );
@@ -91,15 +98,13 @@ public void init() {
91
98
getScheduler ().executeAsync (updateChecker ::checkUpdates );
92
99
}
93
100
94
- getScheduler ().executeAsync (() -> {
95
- try {
96
- this .httpClient .verifyStatus ();
101
+ this .httpClient .verifyStatus ()
102
+ .thenRun (() -> getLogger ().info ("Successfully connected to " + this .config .getSiteUrl ()))
103
+ .exceptionally (ex -> {
104
+ getLogger ().warn ("Unable to verify the website connection: " + ex .getMessage ());
97
105
98
- getLogger ().info ("Successfully connected to " + this .config .getSiteUrl ());
99
- } catch (IOException e ) {
100
- getLogger ().warn ("Unable to verify the website connection: " + e .getMessage () + " - " + e .getClass ().getName ());
101
- }
102
- });
106
+ return null ;
107
+ });
103
108
}
104
109
105
110
public void restartHttpServer () {
@@ -127,10 +132,6 @@ public void shutdown() {
127
132
}
128
133
}
129
134
130
- public void setConfig (PluginConfig config ) {
131
- this .config = config ;
132
- }
133
-
134
135
public void saveConfig () throws IOException {
135
136
if (!Files .isDirectory (this .platform .getDataDirectory ())) {
136
137
Files .createDirectories (this .platform .getDataDirectory ());
@@ -161,8 +162,8 @@ public ServerData getServerData(boolean fullData) {
161
162
return new ServerData (platformData , version , players , max , system , world , fullData );
162
163
}
163
164
164
- public void fetchNow () {
165
- getScheduler (). executeAsync ( this .fetcherTask );
165
+ public CompletableFuture < Void > fetch () {
166
+ return this .fetcherTask . fetch ( );
166
167
}
167
168
168
169
public LoggerAdapter getLogger () {
@@ -189,14 +190,6 @@ public HttpServer getHttpServer() {
189
190
return this .httpServer ;
190
191
}
191
192
192
- public static Gson getGson () {
193
- return GSON ;
194
- }
195
-
196
- public static Gson getGsonPrettyPrint () {
197
- return GSON_PRETTY_PRINT ;
198
- }
199
-
200
193
public Optional <UserInfo > getUser (String name ) {
201
194
return this .fetcherTask .getUser (name );
202
195
}
@@ -217,4 +210,12 @@ private double getCpuUsage() {
217
210
}
218
211
return -1 ;
219
212
}
213
+
214
+ public static Gson getGson () {
215
+ return GSON ;
216
+ }
217
+
218
+ public static Gson getGsonPrettyPrint () {
219
+ return GSON_PRETTY_PRINT ;
220
+ }
220
221
}
0 commit comments