all about threads
"IDK whats gonna happen but it should work"
This commit is contained in:
parent
411be7ce29
commit
f75f76b9d4
2 changed files with 18 additions and 11 deletions
|
@ -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<Thread> 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<Thread> 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();
|
||||
|
|
|
@ -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",
|
||||
|
|
Loading…
Reference in a new issue