diff --git a/build-number.txt b/build-number.txt index d1d06ad..a2998a8 100644 --- a/build-number.txt +++ b/build-number.txt @@ -1 +1 @@ -1111 \ No newline at end of file +1120 \ No newline at end of file diff --git a/src/main/java/me/chayapak1/chomens_bot/Configuration.java b/src/main/java/me/chayapak1/chomens_bot/Configuration.java index 071a501..d73f939 100644 --- a/src/main/java/me/chayapak1/chomens_bot/Configuration.java +++ b/src/main/java/me/chayapak1/chomens_bot/Configuration.java @@ -12,9 +12,6 @@ public class Configuration { public String consoleCommandPrefix; public Keys keys = new Keys(); - - public InternetCheck internetCheck = new InternetCheck(); - public Backup backup = new Backup(); public String weatherApiKey; @@ -52,14 +49,11 @@ public class Configuration { public int timeout = 6000; } - public static class InternetCheck { - public boolean enabled = false; - public String address = "https://sus.red"; - } - public static class Backup { public boolean enabled = false; public String address = "http://fard.sex/check"; + public int interval = 1000; + public int failTimes = 2; } public static class Keys { diff --git a/src/main/java/me/chayapak1/chomens_bot/Main.java b/src/main/java/me/chayapak1/chomens_bot/Main.java index 8596f2e..f93f803 100644 --- a/src/main/java/me/chayapak1/chomens_bot/Main.java +++ b/src/main/java/me/chayapak1/chomens_bot/Main.java @@ -13,13 +13,10 @@ import org.yaml.snakeyaml.constructor.Constructor; import java.io.*; import java.net.URL; -import java.net.URLConnection; -import java.net.UnknownHostException; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; -import java.util.Set; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -43,7 +40,7 @@ public class Main { private static boolean stopping = false; - private static final List alreadyAddedThreads = new ArrayList<>(); + private static int backupFailTimes = 0; private static DiscordPlugin discord; @@ -72,7 +69,9 @@ public class Main { configWriter.write(defaultConfig); configWriter.close(); - LoggerUtilities.info("config.yml file not found, so the default one was created"); + LoggerUtilities.info("config.yml file was not found, so the default one was created. Please modify it to your needs."); + + System.exit(1); } InputStream opt = Files.newInputStream(configPath); @@ -80,54 +79,37 @@ public class Main { config = yaml.load(reader); - executor.scheduleAtFixedRate(() -> { - try { - checkInternet(); - } catch (IOException e) { - e.printStackTrace(); - } - }, 0, 1, TimeUnit.MINUTES); - - executor.scheduleAtFixedRate(() -> { - final Set threads = Thread.getAllStackTraces().keySet(); - - for (Thread thread : threads) { - final Thread.UncaughtExceptionHandler oldHandler = thread.getUncaughtExceptionHandler(); - - thread.setUncaughtExceptionHandler((_thread, throwable) -> { - if (!alreadyAddedThreads.contains(thread) && throwable instanceof OutOfMemoryError) System.exit(1); - - alreadyAddedThreads.add(thread); - - oldHandler.uncaughtException(_thread, throwable); - }); - } - }, 0, 30, TimeUnit.SECONDS); - if (!config.backup.enabled) { initializeBots(); - return; + } else { + executor.scheduleAtFixedRate(() -> { + boolean reachable; + + try { + HttpUtilities.getRequest(new URL(config.backup.address)); + + reachable = true; + } catch (Exception e) { + reachable = false; + } + + if (!reachable && !alreadyStarted) { + backupFailTimes++; + + if (backupFailTimes > config.backup.failTimes) { + LoggerUtilities.info("Main instance is down! Starting backup instance"); + + initializeBots(); + } + } else if (reachable && alreadyStarted) { + LoggerUtilities.info("Main instance is back up! Now stopping"); + + // no need to reset backupFailTimes because we are stopping anyway + + stop(); + } + }, 0, config.backup.interval, TimeUnit.MILLISECONDS); } - - executor.scheduleAtFixedRate(() -> { - boolean reachable; - - try { - HttpUtilities.getRequest(new URL(config.backup.address)); - - reachable = true; - } catch (Exception e) { - reachable = false; - } - - if (!reachable && !alreadyStarted) { - LoggerUtilities.info("Main instance is down! Starting backup instance"); - - initializeBots(); - } else if (reachable && alreadyStarted) { - System.exit(1); - } - }, 0, 1, TimeUnit.MINUTES); } public static void initializeBots() { @@ -160,28 +142,6 @@ public class Main { } } - private static void checkInternet () throws IOException { - if (!config.internetCheck.enabled) return; - - boolean reachable = false; - - try { - final URL url = new URL(config.internetCheck.address); - - final URLConnection connection = url.openConnection(); - - connection.connect(); - - reachable = true; - } catch (UnknownHostException ignored) {} - - if (!reachable) { - LoggerUtilities.error("No internet, exiting"); - - System.exit(1); - } - } - // most of these are stolen from HBot public static void stop () { if (stopping) return; @@ -232,9 +192,9 @@ public class Main { botIndex++; } - if (discord.jda != null) discord.jda.shutdown(); - if (discordEnabled) { + discord.jda.shutdown(); + for (int i = 0; i < 150; i++) { try { if (!ArrayUtilities.isAllTrue(stoppedDiscord)) Thread.sleep(50); diff --git a/src/main/resources/default-config.yml b/src/main/resources/default-config.yml index 6f79cf6..6fc1422 100644 --- a/src/main/resources/default-config.yml +++ b/src/main/resources/default-config.yml @@ -9,19 +9,19 @@ commandSpyPrefixes: consoleCommandPrefix: '.' -internetCheck: - enabled: false - address: 'https://sus.red' - -# how backup works is that it checks for the address every 1 minute, +# how backup works is that it makes a http request for the address # if the address is reachable it will not start the bot # if the address is not reachable then it will start the bot +# *** it doesn't care about the status code + # if the bot has already been started and the address is back up it -# will stop the bot (using System.exit(1)) +# will stop the bot backup: enabled: false address: 'https://fard.sex/check' + interval: 1000 # in milliseconds + failTimes: 2 discord: enabled: false