2
2
3
3
import com .gitblit .IStoredSettings ;
4
4
import com .gitblit .manager .IRuntimeManager ;
5
+ import com .gitblit .utils .JsonUtils ;
6
+ import org .slf4j .Logger ;
7
+ import org .slf4j .LoggerFactory ;
8
+
9
+ import java .io .IOException ;
5
10
import java .util .concurrent .Executors ;
6
11
import java .util .concurrent .ScheduledExecutorService ;
7
12
import java .util .concurrent .TimeUnit ;
8
13
14
+ import static com .gitblit .utils .JsonUtils .sendJsonString ;
15
+
9
16
public class GitblitInstance
10
17
{
11
18
private final static String STATS_URL = "https://instats.gitblit.dev/hiitsme/" ;
19
+ private final static Logger LOG = LoggerFactory .getLogger (GitblitInstance .class );
12
20
13
21
private IRuntimeManager runtimeManager ;
14
22
15
23
private String instanceId ;
16
24
17
25
private GitblitInstanceReport report ;
18
26
27
+ private ScheduledExecutorService executor ;
19
28
20
29
21
30
/**
@@ -34,6 +43,7 @@ public void init(IRuntimeManager runtimeManager) {
34
43
// Initialize ID
35
44
GitblitInstanceId instanceId = new GitblitInstanceId (runtimeManager .getBaseFolder ());
36
45
this .instanceId = instanceId .getId ().toString ();
46
+ LOG .info (this .instanceId );
37
47
38
48
GitblitInstanceStat instanceStat ;
39
49
@@ -60,6 +70,15 @@ public void start()
60
70
}
61
71
}
62
72
73
+ public void stop ()
74
+ {
75
+ if (this .executor != null && !this .executor .isShutdown () && !this .executor .isTerminated ()) {
76
+ this .executor .shutdownNow ();
77
+ System .out .println ("Gitblit instance reporting task stopped." );
78
+ }
79
+ }
80
+
81
+
63
82
64
83
/**
65
84
* Determine if the reporting task should run.
@@ -80,8 +99,9 @@ boolean shouldRunReports()
80
99
81
100
// Check if we are running in a test environment
82
101
IStoredSettings settings = this .runtimeManager .getSettings ();
83
- if (! settings .getString ("gitblit.testReportingURL " , "" ).isEmpty ()) {
102
+ if (! settings .getString ("gitblit.testReportingUrl " , "" ).isEmpty ()) {
84
103
// Force reporting to run overriding any test settings
104
+ LOG .debug ("Enabled reporting to test server URL: {}" , settings .getString ("gitblit.testReportingUrl" , "" ));
85
105
return true ;
86
106
}
87
107
if (settings .getBoolean ("gitblit.testRun" , false )) {
@@ -119,17 +139,31 @@ private void startReports()
119
139
{
120
140
this .executor = Executors .newSingleThreadScheduledExecutor ();
121
141
122
- String baseUrl = STATS_URL ;
142
+ String statsUrl = STATS_URL ;
123
143
int delay = 24 ;
124
144
int period = 24 * 60 ; // 24 hours in minutes
125
145
TimeUnit unit = TimeUnit .MINUTES ;
146
+ long retryInterval = 60 * 60 * 1000 ; // 1 hour in milliseconds
147
+ final long retryTimeout = 20 * 60 * 60 * 1000 ; // 20 hours in milliseconds
148
+
149
+ // If we are running in a test environment, we will send the reports more frequently
150
+ String testUrl = this .runtimeManager .getSettings ().getString ("gitblit.testReportingUrl" , "" );
151
+ if (! testUrl .isEmpty ()) {
152
+ statsUrl = testUrl ;
153
+ delay = 10 ;
154
+ period = 24 ;
155
+ unit = TimeUnit .SECONDS ;
156
+ retryInterval = 10 * 1000 ; // 10 seconds in milliseconds
157
+ }
126
158
159
+ final String baseUrl = statsUrl ;
160
+ final long retryIntervalFinal = retryInterval ;
127
161
this .executor .scheduleAtFixedRate (new Runnable ()
128
162
{
129
163
@ Override
130
164
public void run ()
131
165
{
132
- sendMyStats (baseUrl + instanceId );
166
+ sendMyStats (baseUrl + instanceId , retryIntervalFinal , retryTimeout );
133
167
}
134
168
}, delay , period , unit );
135
169
}
@@ -139,11 +173,40 @@ public void run()
139
173
*
140
174
* This will send a JSON object to the server with the instance report.
141
175
*
142
- * @param server
176
+ * @param reportUrl
143
177
* The URL to send the report to.
178
+ * @param retryInterval
179
+ * The interval in milliseconds to wait before retrying to send the report if it failed.
180
+ * @param retryTimeout
181
+ * The timeout in milliseconds to give up sending the report if it fails repeatedly.
144
182
*/
145
- private void sendMyStats (String server )
183
+ private void sendMyStats (String reportUrl , long retryInterval , long retryTimeout )
146
184
{
185
+ // Create a HTTP POST request payload
186
+ String report = JsonUtils .toJsonString (this .report .fromNow ());
187
+
188
+ int status = 0 ;
189
+ long timeToGiveup = System .currentTimeMillis () + retryTimeout ;
190
+ while (status != 200 && System .currentTimeMillis () < timeToGiveup ) {
191
+ try {
192
+ status = sendJsonString (reportUrl , report , "gitblitta" , "countmein" .toCharArray ());
193
+ if (status != 200 ) {
194
+ LOG .debug ("Error sending stats to " + reportUrl + ": " + status );
195
+ }
196
+ }
197
+ catch (IOException e ) {
198
+ LOG .debug ("Exception sending stats to " + reportUrl + ": " + e .getMessage ());
199
+ }
200
+
201
+ if (status != 200 ) {
202
+ try {
203
+ Thread .sleep (retryInterval );
204
+ } catch (InterruptedException e ) {
205
+ Thread .currentThread ().interrupt ();
206
+ return ; // exit if interrupted
207
+ }
208
+ }
209
+ }
147
210
}
148
211
149
212
}
0 commit comments