Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add heartbeat url ping on successful DB update #26

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/main/java/net/fabricmc/meta/FabricMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package net.fabricmc.meta;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -40,6 +42,7 @@ public class FabricMeta {
private static final Logger LOGGER = LoggerFactory.getLogger(VersionDatabase.class);
private static final Map<String, String> config = new HashMap<>();
private static boolean configInitialized;
private static URL heartbeatUrl; // URL pinged with every successful update()

public static void main(String[] args) {
Path configFile = Paths.get("config.json");
Expand All @@ -53,6 +56,12 @@ public static void main(String[] args) {
}

reader.endObject();

String heartbeatUrlString = config.get("heartbeatUrl");

if (heartbeatUrlString != null) {
heartbeatUrl = new URL(heartbeatUrlString);
}
} catch (IOException | IllegalStateException e) {
throw new RuntimeException("malformed config in "+configFile, e);
}
Expand All @@ -73,12 +82,32 @@ public static void main(String[] args) {
private static void update(){
try {
database = VersionDatabase.generate();
updateHeartbeat();
} catch (Throwable t) {
if (database == null){
throw new RuntimeException(t);
} else {
t.printStackTrace();
LOGGER.warn("update failed", t);
}
}
}

private static void updateHeartbeat() {
if (heartbeatUrl == null) return;

try {
HttpURLConnection conn = (HttpURLConnection) heartbeatUrl.openConnection();
conn.setRequestMethod("HEAD");
conn.setConnectTimeout(500);
conn.setReadTimeout(500);

int status = conn.getResponseCode();

if (status != HttpURLConnection.HTTP_OK) {
LOGGER.warn("heartbeat request failed with status {}", status);
}
} catch (IOException e) {
LOGGER.warn("heartbeat request failed: {}", e.toString());
}
}

Expand Down