Skip to content

Commit

Permalink
Support timeout configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
45gfg9 committed May 10, 2020
1 parent 428ccfb commit 58587cb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = 'net.im45.bot'
version = '0.2.2'
version = '0.2.4'

repositories {
maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/net/im45/bot/watcher/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public class Request implements Runnable {
private String token;
private String tokenBuf;

private int timeout;

static {
try {
ENDPOINT = new URL("https://api.github.com/graphql");
Expand Down Expand Up @@ -97,12 +99,12 @@ private String buildQueryString(Set<RepoId> repos) {
return String.format("{\"query\": \"%s\"}", sb.toString());
}

private static HttpsURLConnection newConnection(String token) throws IOException {
private HttpsURLConnection newConnection(String token) throws IOException {
HttpsURLConnection connection = (HttpsURLConnection) ENDPOINT.openConnection();
connection.setRequestProperty("User-Agent", "45gfg9/16.42");
connection.setRequestProperty("Accept-Encoding", "gzip");
connection.setRequestProperty("Authorization", "bearer " + token);
connection.setReadTimeout(10000);
connection.setReadTimeout(timeout);
return connection;
}

Expand All @@ -120,6 +122,14 @@ public void setErr(Consumer<String> err) {
this.err = err;
}

public void setTimeout(int timeout) {
this.timeout = timeout;
}

public int getTimeout() {
return timeout;
}

public boolean hasVerifiedToken() {
return token != null;
}
Expand Down Expand Up @@ -343,5 +353,6 @@ void dump() {

debug.accept("token: " + token);
debug.accept("tokenBuf: " + tokenBuf);
debug.accept("timeout: " + timeout);
}
}
20 changes: 16 additions & 4 deletions src/main/java/net/im45/bot/watcher/Watcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,12 @@ public void onLoad() {
settings = loadConfig("settings.yml");
settings.setIfAbsent("token", "Not set");
settings.setIfAbsent("interval", 30 * 1000); // 30s default
settings.setIfAbsent("timeout", 15 * 1000); // 15s default
settings.setIfAbsent("autostart", false);

String token = settings.getString("token");
intervalMs = settings.getInt("interval");
request.setTimeout(settings.getInt("timeout"));

tempFuture = getScheduler().async(() -> request.setToken(token));

Expand Down Expand Up @@ -150,7 +152,7 @@ public boolean onCommandBlocking(@NotNull CommandSender sender, @NotNull List<St
} else if ("interval".equals(name)) {
try {
intervalMs = Integer.parseInt(arg);
sender.sendMessageBlocking("Interval set to " + arg);
sender.sendMessageBlocking("Interval set to " + arg + "ms");
} catch (NumberFormatException e) {
sender.sendMessageBlocking("Not a valid number: " + arg);
}
Expand All @@ -169,12 +171,19 @@ public boolean onCommandBlocking(@NotNull CommandSender sender, @NotNull List<St
Bot bot = Bot.getInstance(qq);
request.setConsumers(bot);
sender.sendMessageBlocking("Bot set.");
} else if ("timeout".equals(name)) {
try {
request.setTimeout(Integer.parseInt(arg));
sender.sendMessageBlocking("Timeout set to: " + arg + "ms");
} catch (NumberFormatException e) {
sender.sendMessageBlocking("Not a valid number: " + arg);
}
} else return false;
} else if ("dump".equals(sub)) {
request.dump();

logger.debug("Interval: " + intervalMs);
logger.debug("RepeatTask: " + repeatTask);

request.dump();
} else return false;

return true;
Expand All @@ -186,7 +195,7 @@ public boolean onCommandBlocking(@NotNull CommandSender sender, @NotNull List<St
Group subject = e.getSubject();
List<String> msg = new ArrayList<>(Arrays.asList(e.getMessage()
.toString()
.replaceFirst("\\[mirai:source:.*?]", "")
.replaceFirst("\\[mirai:source:\\d+,\\d+]", "")
.split(" ")));
msg.removeIf(String::isBlank);
if (msg.size() == 0) return;
Expand All @@ -201,6 +210,7 @@ public boolean onCommandBlocking(@NotNull CommandSender sender, @NotNull List<St
i++;
}
}
if (i != 0) request.save(watchers);
String s = "Added " + i + " repositor" + (i == 1 ? "y" : "ies") + ".";
subject.sendMessage(s);
} else if ("/unwatch-release".equals(cmd)) {
Expand All @@ -210,6 +220,7 @@ public boolean onCommandBlocking(@NotNull CommandSender sender, @NotNull List<St
i++;
}
}
if (i != 0) request.save(watchers);
String s = "Removed " + i + " repositor" + (i == 1 ? "y" : "ies") + ".";
subject.sendMessage(s);
} else if ("/watch-list".equals(cmd)) {
Expand Down Expand Up @@ -248,6 +259,7 @@ public void onDisable() {
// Maybe will not fix...
settings.set("token", request.hasVerifiedToken() ? request.getToken() : "Not set");
settings.set("interval", intervalMs);
settings.set("timeout", request.getTimeout());
settings.save();

request.save(watchers);
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: "release-watcher"
author: "45gfg9"
version: "0.2.2"
version: "0.2.4"
main: "net.im45.bot.watcher.Watcher"
info: ""
depends: []

0 comments on commit 58587cb

Please sign in to comment.