|
1 | 1 | package com.gitblit.instance; |
2 | 2 |
|
| 3 | +import com.gitblit.IStoredSettings; |
3 | 4 | import com.gitblit.manager.IRuntimeManager; |
| 5 | +import java.util.concurrent.Executors; |
| 6 | +import java.util.concurrent.ScheduledExecutorService; |
| 7 | +import java.util.concurrent.TimeUnit; |
4 | 8 |
|
5 | 9 | public class GitblitInstance |
6 | 10 | { |
| 11 | + private final static String STATS_URL = "https://instats.gitblit.dev/hiitsme/"; |
7 | 12 |
|
8 | 13 | private IRuntimeManager runtimeManager; |
9 | 14 |
|
@@ -50,5 +55,95 @@ else if (runtimeManager.getStatus().isGO){ |
50 | 55 |
|
51 | 56 | public void start() |
52 | 57 | { |
| 58 | + if (shouldRunReports()) { |
| 59 | + startReports(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + /** |
| 65 | + * Determine if the reporting task should run. |
| 66 | + * |
| 67 | + * We do not want to report anything, i.e. the reporting task to run, |
| 68 | + * if we are running unit tests or integration tests. |
| 69 | + * Instance reports should only be sent for production instances or released versions. |
| 70 | + * Therefore we also check if the Gitblit version is a SNAPSHOT version, |
| 71 | + * or if the docker image is not a release version, when running from a docker image. |
| 72 | + * A docker image running under GOSS should also not report anything. |
| 73 | + */ |
| 74 | + boolean shouldRunReports() |
| 75 | + { |
| 76 | + // We can only run reports when we have been initialized |
| 77 | + if (this.report == null || this.runtimeManager == null) { |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + // Check if we are running in a test environment |
| 82 | + IStoredSettings settings = this.runtimeManager.getSettings(); |
| 83 | + if (! settings.getString("gitblit.testReportingURL", "").isEmpty()) { |
| 84 | + // Force reporting to run overriding any test settings |
| 85 | + return true; |
| 86 | + } |
| 87 | + if (settings.getBoolean("gitblit.testRun", false)) { |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + // Check if we are running a SNAPSHOT version |
| 92 | + if (this.runtimeManager.getStatus().version.endsWith("SNAPSHOT")) { |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + if (this.report.instanceStat.instanceType == GitblitInstanceStat.GitblitInstanceType.DOCKER) { |
| 97 | + // Check if we are running a docker image that is not a release version |
| 98 | + if (! settings.getString("container.imageType", "").equals("release")) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + // Check if we are running a docker image under GOSS |
| 103 | + if (System.getenv("GITBLIT_GOSS_TEST") != null) { |
| 104 | + return false; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return true; |
53 | 109 | } |
| 110 | + |
| 111 | + |
| 112 | + /** |
| 113 | + * Start the reporting task. |
| 114 | + * |
| 115 | + * This will start a thread that runs once a day and sends the instance |
| 116 | + * report to the popularity report server. |
| 117 | + */ |
| 118 | + private void startReports() |
| 119 | + { |
| 120 | + this.executor = Executors.newSingleThreadScheduledExecutor(); |
| 121 | + |
| 122 | + String baseUrl = STATS_URL; |
| 123 | + int delay = 24; |
| 124 | + int period = 24 * 60; // 24 hours in minutes |
| 125 | + TimeUnit unit = TimeUnit.MINUTES; |
| 126 | + |
| 127 | + this.executor.scheduleAtFixedRate(new Runnable() |
| 128 | + { |
| 129 | + @Override |
| 130 | + public void run() |
| 131 | + { |
| 132 | + sendMyStats(baseUrl + instanceId); |
| 133 | + } |
| 134 | + }, delay, period, unit); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Send the instance report to the popularity report server. |
| 139 | + * |
| 140 | + * This will send a JSON object to the server with the instance report. |
| 141 | + * |
| 142 | + * @param server |
| 143 | + * The URL to send the report to. |
| 144 | + */ |
| 145 | + private void sendMyStats(String server) |
| 146 | + { |
| 147 | + } |
| 148 | + |
54 | 149 | } |
0 commit comments