Skip to content

Commit 073ccd7

Browse files
committed
Create instance reports, with test server URL
1 parent ac50139 commit 073ccd7

File tree

3 files changed

+90
-5
lines changed

3 files changed

+90
-5
lines changed

src/main/java/com/gitblit/instance/GitblitInstance.java

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,29 @@
22

33
import com.gitblit.IStoredSettings;
44
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;
510
import java.util.concurrent.Executors;
611
import java.util.concurrent.ScheduledExecutorService;
712
import java.util.concurrent.TimeUnit;
813

14+
import static com.gitblit.utils.JsonUtils.sendJsonString;
15+
916
public 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
}

src/main/java/com/gitblit/instance/GitblitInstanceReport.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import com.google.gson.annotations.SerializedName;
44

5+
import java.text.SimpleDateFormat;
6+
import java.util.TimeZone;
7+
58
/**
69
* GitblitInstanceReport collects the static and dynamic statistics about a running
710
* Gitblit instance, pairs it with a report version and instance id.
@@ -13,11 +16,28 @@ class GitblitInstanceReport
1316
private final int reportVersion = 1;
1417
@SerializedName("instance")
1518
private final String instanceId;
19+
private final String startTs;
20+
private String lpingTs;
21+
1622
final GitblitInstanceStat instanceStat;
1723

1824
GitblitInstanceReport(String instanceId, GitblitInstanceStat instanceStat)
1925
{
2026
this.instanceId = instanceId;
2127
this.instanceStat = instanceStat;
28+
29+
// Convert the timestamp taken from instanceStat to a string in the format "yyyy-MM-dd'T'HHmmssZ" so
30+
// it can be used better in a file name. It is replicated here so that it can be directly used by the receiver.
31+
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
32+
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
33+
this.startTs = dateFormat.format(instanceStat.startTs);
34+
}
35+
36+
GitblitInstanceReport fromNow()
37+
{
38+
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
39+
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
40+
this.lpingTs = dateFormat.format(System.currentTimeMillis());
41+
return this;
2242
}
2343
}

src/main/java/com/gitblit/servlet/GitblitContext.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ protected void destroyContext(ServletContext context) {
310310
}
311311
}
312312

313+
this.instance.stop();
314+
313315
for (IManager manager : managers) {
314316
logger.debug("stopping {}", manager.getClass().getSimpleName());
315317
manager.stop();

0 commit comments

Comments
 (0)