Search in sources :

Example 1 with Command

use of net.robinfriedli.aiode.command.Command in project aiode by robinfriedli.

the class CleanDbCommand method runAdmin.

@Override
public void runAdmin() {
    Aiode.shutdownListeners();
    try {
        if (!argumentSet("silent")) {
            EmbedBuilder embedBuilder = new EmbedBuilder();
            embedBuilder.setTitle("Scheduled cleanup");
            embedBuilder.setDescription("Aiode is suspending for a few seconds to clean the database.");
            sendToActiveGuilds(embedBuilder.build());
        }
        CommandExecutionQueueManager executionQueueManager = Aiode.get().getExecutionQueueManager();
        Command command = this;
        Thread cleanupThread = new Thread(() -> {
            ThreadContext.Current.install(command);
            try {
                try {
                    executionQueueManager.joinAll(60000);
                } catch (InterruptedException e) {
                    return;
                }
                StaticSessionProvider.consumeSession(this::doClean);
            } finally {
                Aiode.registerListeners();
            }
        });
        cleanupThread.setName("database-cleanup-thread-" + command.getContext().toString());
        Thread.UncaughtExceptionHandler commandExecutionExceptionHandler = Thread.currentThread().getUncaughtExceptionHandler();
        if (commandExecutionExceptionHandler != null) {
            cleanupThread.setUncaughtExceptionHandler(commandExecutionExceptionHandler);
        }
        cleanupThread.start();
    } catch (Throwable e) {
        Aiode.registerListeners();
        throw e;
    }
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Command(net.robinfriedli.aiode.command.Command) AbstractAdminCommand(net.robinfriedli.aiode.command.AbstractAdminCommand) CommandExecutionQueueManager(net.robinfriedli.aiode.concurrent.CommandExecutionQueueManager)

Example 2 with Command

use of net.robinfriedli.aiode.command.Command in project aiode by robinfriedli.

the class CommandUncaughtExceptionHandler method uncaughtException.

@Override
public void uncaughtException(Thread t, Throwable e) {
    Command command = ThreadContext.Current.get(Command.class);
    if (command != null) {
        try {
            ExceptionUtils.handleCommandException(e, command, logger);
            return;
        } catch (Exception e1) {
            logger.error("Exception while calling ExceptionUtils#handleCommandException, falling back to logging error", e1);
        }
    }
    logger.error("Exception in command handler thread", e);
}
Also used : Command(net.robinfriedli.aiode.command.Command)

Aggregations

Command (net.robinfriedli.aiode.command.Command)2 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)1 AbstractAdminCommand (net.robinfriedli.aiode.command.AbstractAdminCommand)1 CommandExecutionQueueManager (net.robinfriedli.aiode.concurrent.CommandExecutionQueueManager)1