22
33import com .gitblit .IStoredSettings ;
44import 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 ;
510import java .util .concurrent .Executors ;
611import java .util .concurrent .ScheduledExecutorService ;
712import java .util .concurrent .TimeUnit ;
813
14+ import static com .gitblit .utils .JsonUtils .sendJsonString ;
15+
916public class GitblitInstance
1017{
1118 private final static String STATS_URL = "https://instats.gitblit.dev/hiitsme/" ;
19+ private final static Logger LOG = LoggerFactory .getLogger (GitblitInstance .class );
1220
1321 private IRuntimeManager runtimeManager ;
1422
1523 private String instanceId ;
1624
1725 private GitblitInstanceReport report ;
1826
27+ private ScheduledExecutorService executor ;
1928
2029
2130 /**
@@ -34,6 +43,7 @@ public void init(IRuntimeManager runtimeManager) {
3443 // Initialize ID
3544 GitblitInstanceId instanceId = new GitblitInstanceId (runtimeManager .getBaseFolder ());
3645 this .instanceId = instanceId .getId ().toString ();
46+ LOG .info (this .instanceId );
3747
3848 GitblitInstanceStat instanceStat ;
3949
@@ -60,6 +70,15 @@ public void start()
6070 }
6171 }
6272
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+
6382
6483 /**
6584 * Determine if the reporting task should run.
@@ -80,8 +99,9 @@ boolean shouldRunReports()
8099
81100 // Check if we are running in a test environment
82101 IStoredSettings settings = this .runtimeManager .getSettings ();
83- if (! settings .getString ("gitblit.testReportingURL " , "" ).isEmpty ()) {
102+ if (! settings .getString ("gitblit.testReportingUrl " , "" ).isEmpty ()) {
84103 // Force reporting to run overriding any test settings
104+ LOG .debug ("Enabled reporting to test server URL: {}" , settings .getString ("gitblit.testReportingUrl" , "" ));
85105 return true ;
86106 }
87107 if (settings .getBoolean ("gitblit.testRun" , false )) {
@@ -119,17 +139,31 @@ private void startReports()
119139 {
120140 this .executor = Executors .newSingleThreadScheduledExecutor ();
121141
122- String baseUrl = STATS_URL ;
142+ String statsUrl = STATS_URL ;
123143 int delay = 24 ;
124144 int period = 24 * 60 ; // 24 hours in minutes
125145 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+ }
126158
159+ final String baseUrl = statsUrl ;
160+ final long retryIntervalFinal = retryInterval ;
127161 this .executor .scheduleAtFixedRate (new Runnable ()
128162 {
129163 @ Override
130164 public void run ()
131165 {
132- sendMyStats (baseUrl + instanceId );
166+ sendMyStats (baseUrl + instanceId , retryIntervalFinal , retryTimeout );
133167 }
134168 }, delay , period , unit );
135169 }
@@ -139,11 +173,40 @@ public void run()
139173 *
140174 * This will send a JSON object to the server with the instance report.
141175 *
142- * @param server
176+ * @param reportUrl
143177 * 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.
144182 */
145- private void sendMyStats (String server )
183+ private void sendMyStats (String reportUrl , long retryInterval , long retryTimeout )
146184 {
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+ }
147210 }
148211
149212}
0 commit comments