Implement heartbeat for uptime monitoring
All checks were successful
continuous-integration/drone/push Build is passing

You can now monitor the bot's uptime via any external tool that supports push heartbeats. The bots sends a GET request every 30 seconds to show that it's online. The URL is hardcoded for the moment, but very easy to change.
This commit is contained in:
2022-11-21 19:54:49 +01:00
parent 0bcb5d58f4
commit a5ddbf0d2e
3 changed files with 84 additions and 2 deletions

View File

@@ -0,0 +1,53 @@
package wtf.beatrice.hidekobot.runnables;
import wtf.beatrice.hidekobot.Configuration;
import wtf.beatrice.hidekobot.utils.Logger;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class HeartBeatTask implements Runnable
{
private final Logger logger;
public HeartBeatTask()
{
logger = new Logger(getClass());
}
@Override
public void run()
{
String apiKey = Configuration.getHeartBeatApiKey();
if(apiKey == null || apiKey.isEmpty()) return;
String urlString = Configuration.getFullHeartBeatLink();
try {
URL heartbeatUrl = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) heartbeatUrl.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
int responseCode = connection.getResponseCode();
if(200 <= responseCode && responseCode < 300)
{
// only log ok response codes when verbosity is enabled
if(Configuration.isVerbose()) logger.log("Heartbeat response code: " + responseCode);
}
else
{
logger.log("Heartbeat returned problematic response code: " + responseCode);
}
} catch (IOException e) {
logger.log("Error while trying to push heartbeat: " + e.getMessage());
}
}
}