diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/Main.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/Main.java index cad96fc..2c120a5 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/Main.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/Main.java @@ -2,7 +2,6 @@ package land.chipmunk.chayapak.chomens_bot; import com.google.common.util.concurrent.ThreadFactoryBuilder; import land.chipmunk.chayapak.chomens_bot.plugins.ConsolePlugin; -import land.chipmunk.chayapak.chomens_bot.util.ExceptionUtilities; import land.chipmunk.chayapak.chomens_bot.util.HttpUtilities; import land.chipmunk.chayapak.chomens_bot.util.LoggerUtilities; import net.dv8tion.jda.api.JDA; @@ -21,6 +20,7 @@ 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; @@ -42,6 +42,8 @@ public class Main { private static boolean alreadyStarted = false; + private static final List alreadyAddedThreads = new ArrayList<>(); + public static void main(String[] args) throws IOException { final Path configPath = Path.of("config.yml"); @@ -83,18 +85,21 @@ public class Main { } }, 0, 1, TimeUnit.MINUTES); - Thread.currentThread().setUncaughtExceptionHandler((thread, throwable) -> { - LoggerUtilities.error( - String.format( - "Caught an uncaught exception in thread %s!\n%s", + executor.scheduleAtFixedRate(() -> { + final Set threads = Thread.getAllStackTraces().keySet(); - thread.getName(), - ExceptionUtilities.getStacktrace(throwable) - ) - ); + for (Thread thread : threads) { + final Thread.UncaughtExceptionHandler oldHandler = thread.getUncaughtExceptionHandler(); - if (throwable instanceof OutOfMemoryError) System.exit(1); - }); + 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(); diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/InfoCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/InfoCommand.java index adde6fe..e8e20e7 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/InfoCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/InfoCommand.java @@ -120,6 +120,7 @@ public class InfoCommand extends Command { OS name: %s CPU cores: %s CPU model: %s + Threads: %s Heap memory usage: %s""", Component.text(localHost == null ? "N/A" : localHost.getHostName()).color(color), Component.text(System.getProperty("user.dir")).color(color), @@ -128,6 +129,7 @@ public class InfoCommand extends Command { Component.text(os.getName()).color(color), Component.text(String.valueOf(Runtime.getRuntime().availableProcessors())).color(color), cpuModel, + Component.text(String.valueOf(Thread.activeCount())).color(color), Component .translatable( "%s MB / %s MB",