Search in sources :

Example 1 with SchedulerAdapter

use of me.lucko.luckperms.common.plugin.scheduler.SchedulerAdapter in project LuckPerms by lucko.

the class SqlMessenger method init.

@Override
public void init() {
    try {
        super.init();
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
    // schedule poll tasks
    SchedulerAdapter scheduler = this.plugin.getBootstrap().getScheduler();
    this.pollTask = scheduler.asyncRepeating(this::pollMessages, 1, TimeUnit.SECONDS);
    this.housekeepingTask = scheduler.asyncRepeating(this::runHousekeeping, 30, TimeUnit.SECONDS);
}
Also used : SQLException(java.sql.SQLException) SchedulerAdapter(me.lucko.luckperms.common.plugin.scheduler.SchedulerAdapter)

Example 2 with SchedulerAdapter

use of me.lucko.luckperms.common.plugin.scheduler.SchedulerAdapter in project LuckPerms by lucko.

the class CommandManager method executeCommand.

public CompletableFuture<Void> executeCommand(Sender sender, String label, List<String> args) {
    SchedulerAdapter scheduler = this.plugin.getBootstrap().getScheduler();
    List<String> argsCopy = new ArrayList<>(args);
    // if the executingCommand flag is set, there is another command executing at the moment
    if (this.executingCommand.get()) {
        Message.ALREADY_EXECUTING_COMMAND.send(sender);
    }
    // a reference to the thread being used to execute the command
    AtomicReference<Thread> executorThread = new AtomicReference<>();
    // a reference to the timeout task scheduled to catch if this command takes too long to execute
    AtomicReference<SchedulerTask> timeoutTask = new AtomicReference<>();
    // schedule the actual execution of the command using the command executor service
    CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
        // set flags
        executorThread.set(Thread.currentThread());
        this.executingCommand.set(true);
        // actually try to execute the command
        try {
            execute(sender, label, args);
        } catch (Throwable e) {
            // catch any exception
            this.plugin.getLogger().severe("Exception whilst executing command: " + args, e);
        } finally {
            // unset flags
            this.executingCommand.set(false);
            executorThread.set(null);
            // cancel the timeout task
            SchedulerTask timeout;
            if ((timeout = timeoutTask.get()) != null) {
                timeout.cancel();
            }
        }
    }, this.executor);
    // schedule another task to catch if the command doesn't complete after 10 seconds
    timeoutTask.set(scheduler.asyncLater(() -> {
        if (!future.isDone()) {
            handleCommandTimeout(executorThread, argsCopy);
        }
    }, 10, TimeUnit.SECONDS));
    return future;
}
Also used : SchedulerAdapter(me.lucko.luckperms.common.plugin.scheduler.SchedulerAdapter) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) SchedulerTask(me.lucko.luckperms.common.plugin.scheduler.SchedulerTask)

Aggregations

SchedulerAdapter (me.lucko.luckperms.common.plugin.scheduler.SchedulerAdapter)2 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 SchedulerTask (me.lucko.luckperms.common.plugin.scheduler.SchedulerTask)1