Implement heartbeat for uptime monitoring
All checks were successful
continuous-integration/drone/push Build is passing
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:
parent
0bcb5d58f4
commit
a5ddbf0d2e
@ -30,6 +30,10 @@ public class Configuration
|
|||||||
// used to count eg. uptime
|
// used to count eg. uptime
|
||||||
private static LocalDateTime startupTime;
|
private static LocalDateTime startupTime;
|
||||||
|
|
||||||
|
// todo: allow people to set their own url
|
||||||
|
private static final String heartbeatLink = "https://status.beatrice.wtf/api/push/%apikey%?status=up&msg=OK&ping=";
|
||||||
|
private static String heartbeatApiKey = "";
|
||||||
|
|
||||||
|
|
||||||
private static final String botVersion = "0.1.4-slash"; // we should probably find a way to make this consistent with Maven
|
private static final String botVersion = "0.1.4-slash"; // we should probably find a way to make this consistent with Maven
|
||||||
private static final String botName = "HidekoBot";
|
private static final String botName = "HidekoBot";
|
||||||
@ -211,4 +215,15 @@ public class Configuration
|
|||||||
*/
|
*/
|
||||||
public static LocalDateTime getStartupTime() { return startupTime; }
|
public static LocalDateTime getStartupTime() { return startupTime; }
|
||||||
|
|
||||||
|
public static String getFullHeartBeatLink() {
|
||||||
|
return heartbeatLink.replace("%apikey%", heartbeatApiKey);
|
||||||
|
}
|
||||||
|
//todo javadocs
|
||||||
|
public static void setHeartBeatApiKey(String key) {
|
||||||
|
heartbeatApiKey = key;
|
||||||
|
}
|
||||||
|
public static String getHeartBeatApiKey() {
|
||||||
|
return heartbeatApiKey;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@ import wtf.beatrice.hidekobot.listeners.SlashCommandCompleter;
|
|||||||
import wtf.beatrice.hidekobot.listeners.SlashCommandListener;
|
import wtf.beatrice.hidekobot.listeners.SlashCommandListener;
|
||||||
import wtf.beatrice.hidekobot.runnables.CommandsUpdateTask;
|
import wtf.beatrice.hidekobot.runnables.CommandsUpdateTask;
|
||||||
import wtf.beatrice.hidekobot.runnables.ExpiredMessageTask;
|
import wtf.beatrice.hidekobot.runnables.ExpiredMessageTask;
|
||||||
|
import wtf.beatrice.hidekobot.runnables.HeartBeatTask;
|
||||||
import wtf.beatrice.hidekobot.utils.Logger;
|
import wtf.beatrice.hidekobot.utils.Logger;
|
||||||
import wtf.beatrice.hidekobot.utils.SlashCommandUtil;
|
import wtf.beatrice.hidekobot.utils.SlashCommandUtil;
|
||||||
|
|
||||||
@ -79,8 +80,19 @@ public class HidekoBot
|
|||||||
if(args.length > 1) {
|
if(args.length > 1) {
|
||||||
List<String> argsList = new ArrayList<>(Arrays.asList(args).subList(1, args.length));
|
List<String> argsList = new ArrayList<>(Arrays.asList(args).subList(1, args.length));
|
||||||
|
|
||||||
if(argsList.contains("verbose")) Configuration.setVerbose(true);
|
for(int i = 0; i < argsList.size(); i++)
|
||||||
if(argsList.contains("refresh")) forceUpdateCommands = true;
|
{
|
||||||
|
String arg = argsList.get(i);
|
||||||
|
|
||||||
|
if(arg.equals("verbose")) Configuration.setVerbose(true);
|
||||||
|
if(arg.equals("refresh")) forceUpdateCommands = true;
|
||||||
|
if(arg.startsWith("heartbeat="))
|
||||||
|
{
|
||||||
|
String apiKey = arg.replaceAll(".*=", ""); //remove the "heartbeat=" part
|
||||||
|
Configuration.setHeartBeatApiKey(apiKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// register listeners
|
// register listeners
|
||||||
@ -120,6 +132,8 @@ public class HidekoBot
|
|||||||
scheduler.scheduleAtFixedRate(expiredMessageTask, 5, 5, TimeUnit.SECONDS); //every 5 seconds
|
scheduler.scheduleAtFixedRate(expiredMessageTask, 5, 5, TimeUnit.SECONDS); //every 5 seconds
|
||||||
CommandsUpdateTask commandsUpdateTask = new CommandsUpdateTask();
|
CommandsUpdateTask commandsUpdateTask = new CommandsUpdateTask();
|
||||||
scheduler.scheduleAtFixedRate(commandsUpdateTask, 10, 300, TimeUnit.SECONDS); //every 5 minutes
|
scheduler.scheduleAtFixedRate(commandsUpdateTask, 10, 300, TimeUnit.SECONDS); //every 5 minutes
|
||||||
|
HeartBeatTask heartBeatTask = new HeartBeatTask();
|
||||||
|
scheduler.scheduleAtFixedRate(heartBeatTask, 10, 30, TimeUnit.SECONDS); //every 30 seconds
|
||||||
|
|
||||||
// register shutdown interrupt signal listener for proper shutdown.
|
// register shutdown interrupt signal listener for proper shutdown.
|
||||||
Signal.handle(new Signal("INT"), signal -> shutdown());
|
Signal.handle(new Signal("INT"), signal -> shutdown());
|
||||||
|
@ -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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user