refactor: some refactors in Main, backup and config
This commit is contained in:
parent
60a1d069c7
commit
f7187e560f
4 changed files with 43 additions and 89 deletions
|
@ -1 +1 @@
|
||||||
1111
|
1120
|
|
@ -12,9 +12,6 @@ public class Configuration {
|
||||||
public String consoleCommandPrefix;
|
public String consoleCommandPrefix;
|
||||||
|
|
||||||
public Keys keys = new Keys();
|
public Keys keys = new Keys();
|
||||||
|
|
||||||
public InternetCheck internetCheck = new InternetCheck();
|
|
||||||
|
|
||||||
public Backup backup = new Backup();
|
public Backup backup = new Backup();
|
||||||
|
|
||||||
public String weatherApiKey;
|
public String weatherApiKey;
|
||||||
|
@ -52,14 +49,11 @@ public class Configuration {
|
||||||
public int timeout = 6000;
|
public int timeout = 6000;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class InternetCheck {
|
|
||||||
public boolean enabled = false;
|
|
||||||
public String address = "https://sus.red";
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Backup {
|
public static class Backup {
|
||||||
public boolean enabled = false;
|
public boolean enabled = false;
|
||||||
public String address = "http://fard.sex/check";
|
public String address = "http://fard.sex/check";
|
||||||
|
public int interval = 1000;
|
||||||
|
public int failTimes = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Keys {
|
public static class Keys {
|
||||||
|
|
|
@ -13,13 +13,10 @@ import org.yaml.snakeyaml.constructor.Constructor;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLConnection;
|
|
||||||
import java.net.UnknownHostException;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
@ -43,7 +40,7 @@ public class Main {
|
||||||
|
|
||||||
private static boolean stopping = false;
|
private static boolean stopping = false;
|
||||||
|
|
||||||
private static final List<Thread> alreadyAddedThreads = new ArrayList<>();
|
private static int backupFailTimes = 0;
|
||||||
|
|
||||||
private static DiscordPlugin discord;
|
private static DiscordPlugin discord;
|
||||||
|
|
||||||
|
@ -72,7 +69,9 @@ public class Main {
|
||||||
configWriter.write(defaultConfig);
|
configWriter.write(defaultConfig);
|
||||||
configWriter.close();
|
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);
|
InputStream opt = Files.newInputStream(configPath);
|
||||||
|
@ -80,54 +79,37 @@ public class Main {
|
||||||
|
|
||||||
config = yaml.load(reader);
|
config = yaml.load(reader);
|
||||||
|
|
||||||
executor.scheduleAtFixedRate(() -> {
|
|
||||||
try {
|
|
||||||
checkInternet();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}, 0, 1, TimeUnit.MINUTES);
|
|
||||||
|
|
||||||
executor.scheduleAtFixedRate(() -> {
|
|
||||||
final Set<Thread> 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) {
|
if (!config.backup.enabled) {
|
||||||
initializeBots();
|
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() {
|
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
|
// most of these are stolen from HBot
|
||||||
public static void stop () {
|
public static void stop () {
|
||||||
if (stopping) return;
|
if (stopping) return;
|
||||||
|
@ -232,9 +192,9 @@ public class Main {
|
||||||
botIndex++;
|
botIndex++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (discord.jda != null) discord.jda.shutdown();
|
|
||||||
|
|
||||||
if (discordEnabled) {
|
if (discordEnabled) {
|
||||||
|
discord.jda.shutdown();
|
||||||
|
|
||||||
for (int i = 0; i < 150; i++) {
|
for (int i = 0; i < 150; i++) {
|
||||||
try {
|
try {
|
||||||
if (!ArrayUtilities.isAllTrue(stoppedDiscord)) Thread.sleep(50);
|
if (!ArrayUtilities.isAllTrue(stoppedDiscord)) Thread.sleep(50);
|
||||||
|
|
|
@ -9,19 +9,19 @@ commandSpyPrefixes:
|
||||||
|
|
||||||
consoleCommandPrefix: '.'
|
consoleCommandPrefix: '.'
|
||||||
|
|
||||||
internetCheck:
|
# how backup works is that it makes a http request for the address
|
||||||
enabled: false
|
|
||||||
address: 'https://sus.red'
|
|
||||||
|
|
||||||
# how backup works is that it checks for the address every 1 minute,
|
|
||||||
# if the address is reachable it will not start the bot
|
# if the address is reachable it will not start the bot
|
||||||
# if the address is not reachable then it will 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
|
# 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:
|
backup:
|
||||||
enabled: false
|
enabled: false
|
||||||
address: 'https://fard.sex/check'
|
address: 'https://fard.sex/check'
|
||||||
|
interval: 1000 # in milliseconds
|
||||||
|
failTimes: 2
|
||||||
|
|
||||||
discord:
|
discord:
|
||||||
enabled: false
|
enabled: false
|
||||||
|
|
Loading…
Reference in a new issue