Skip to content

Commit

Permalink
Try this one out.
Browse files Browse the repository at this point in the history
  • Loading branch information
e3ndr committed Dec 10, 2024
1 parent 7cc5bdb commit f4aceb0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
17 changes: 15 additions & 2 deletions src/main/java/co/casterlabs/caffeinated/updater/Updater.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.nio.file.Files;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

import co.casterlabs.caffeinated.updater.target.Target;
import co.casterlabs.caffeinated.updater.util.FileUtil;
Expand Down Expand Up @@ -74,6 +74,17 @@ public static int needsUpdate() {
return 1;
}

Thread currentThread = Thread.currentThread();
Thread watchdog = new Thread(() -> {
try {
currentThread.join(TimeUnit.SECONDS.toMillis(15));
currentThread.interrupt();
} catch (InterruptedException ignored) {
// Everything succeeded :D
}
});
watchdog.start();

try {
JsonObject buildInfo = Rson.DEFAULT.fromJson(FileUtil.readFile(buildInfoFile), JsonObject.class);

Expand All @@ -82,7 +93,6 @@ public static int needsUpdate() {
String remoteCommit = WebUtil.sendHttpRequest(
HttpRequest.newBuilder()
.uri(URI.create(CHANNEL_COMMIT_URL))
.timeout(Duration.ofSeconds(15))
).trim();
if (!remoteCommit.equals(installedCommit)) return 1;

Expand All @@ -91,6 +101,9 @@ public static int needsUpdate() {
if (!installedChannel.equals(CHANNEL)) return 1;
} catch (IOException | InterruptedException e) {
return 2;
} finally {
Thread.interrupted(); // Clear interrupted status.
watchdog.interrupt();
}

return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandler;
import java.net.http.HttpResponse.BodyHandlers;
import java.time.Duration;

import lombok.NonNull;

public class WebUtil {
private static final HttpClient client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(15))
.build();
private static final HttpClient client = HttpClient.newHttpClient();

public static <T> HttpResponse<T> sendRawHttpRequest(@NonNull HttpRequest.Builder builder, @NonNull BodyHandler<T> handler) throws IOException, InterruptedException {
return client.send(builder.build(), handler);
Expand Down

0 comments on commit f4aceb0

Please sign in to comment.