Search in sources :

Example 1 with LoggingUncaughtExceptionHandler

use of net.robinfriedli.aiode.exceptions.handler.handlers.LoggingUncaughtExceptionHandler in project aiode by robinfriedli.

the class HttpServerManager method start.

public void start() throws IOException {
    httpServer = HttpServer.create(new InetSocketAddress(port), 0);
    ExecutorService executorService = new ThreadPoolExecutor(5, 50, 30, TimeUnit.SECONDS, new SynchronousQueue<>(), new ThreadFactory() {

        final AtomicLong threadId = new AtomicLong(1);

        @Override
        public Thread newThread(@NotNull Runnable r) {
            Thread thread = new Thread(r);
            thread.setName("http-server-thread-" + threadId.getAndIncrement());
            thread.setUncaughtExceptionHandler(new LoggingUncaughtExceptionHandler());
            return thread;
        }
    });
    httpServer.setExecutor(executorService);
    for (HttpHandlerContribution contribution : httpHandlersContext.getInstancesOf(HttpHandlerContribution.class)) {
        httpServer.createContext(contribution.getAttribute("path").getValue(), contribution.instantiate());
    }
    httpServer.start();
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) AtomicLong(java.util.concurrent.atomic.AtomicLong) InetSocketAddress(java.net.InetSocketAddress) ExecutorService(java.util.concurrent.ExecutorService) LoggingUncaughtExceptionHandler(net.robinfriedli.aiode.exceptions.handler.handlers.LoggingUncaughtExceptionHandler) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) HttpHandlerContribution(net.robinfriedli.aiode.entities.xml.HttpHandlerContribution)

Example 2 with LoggingUncaughtExceptionHandler

use of net.robinfriedli.aiode.exceptions.handler.handlers.LoggingUncaughtExceptionHandler in project aiode by robinfriedli.

the class AudioTrafficSimulationCommand method runAdmin.

@Override
public void runAdmin() throws Exception {
    Aiode aiode = Aiode.get();
    AudioManager audioManager = aiode.getAudioManager();
    AudioPlayerManager playerManager = audioManager.getPlayerManager();
    PlayableFactory playableFactory = audioManager.createPlayableFactory(getSpotifyService(), new BlockingTrackLoadingExecutor());
    CommandContext context = getContext();
    Guild guild = context.getGuild();
    ThreadExecutionQueue threadExecutionQueue = aiode.getExecutionQueueManager().getForGuild(guild);
    SpotifyApi spotifyApi = context.getSpotifyApi();
    AudioTrackLoader audioTrackLoader = new AudioTrackLoader(playerManager);
    int streams = getArgumentValueOrElse("streams", DEFAULT_STREAM_COUNT);
    int durationSecs = getArgumentValueOrElse("duration", DEFAULT_DURATION);
    String playbackUrl = getArgumentValueOrElse("url", DEFAULT_PLAYBACK_URL);
    Integer nativeBuffer = getArgumentValueWithTypeOrElse("nativeBuffer", Integer.class, null);
    List<Playable> playables = playableFactory.createPlayables(playbackUrl, spotifyApi, true);
    if (playables.isEmpty()) {
        throw new InvalidCommandException("No playables found for url " + playbackUrl);
    }
    Playable playable = playables.get(0);
    AudioItem audioItem = audioTrackLoader.loadByIdentifier(playable.getPlaybackUrl());
    AudioTrack track;
    if (audioItem instanceof AudioTrack) {
        track = (AudioTrack) audioItem;
    } else {
        throw new IllegalStateException("Could not get AudioTrack for Playable " + playable);
    }
    LoopTrackListener loopTrackListener = new LoopTrackListener(track);
    List<Thread> playbackThreads = nativeBuffer != null ? null : Lists.newArrayListWithCapacity(streams);
    List<Tuple2<IAudioSendSystem, AudioPlayer>> audioSendSystems = nativeBuffer != null ? Lists.newArrayListWithCapacity(streams) : null;
    NativeAudioSendFactory nativeAudioSendFactory = nativeBuffer != null ? new NativeAudioSendFactory(nativeBuffer) : null;
    LoggingUncaughtExceptionHandler eh = new LoggingUncaughtExceptionHandler();
    for (int i = 0; i < streams; i++) {
        AudioPlayer player = playerManager.createPlayer();
        player.addListener(loopTrackListener);
        player.playTrack(track.makeClone());
        if (nativeAudioSendFactory != null) {
            IAudioSendSystem sendSystem = nativeAudioSendFactory.createSendSystem(new FakePacketProvider(player));
            audioSendSystems.add(new Tuple2<>(sendSystem, player));
        } else {
            Thread playbackThread = new Thread(new PlayerPollingRunnable(player));
            playbackThread.setDaemon(true);
            playbackThread.setUncaughtExceptionHandler(eh);
            playbackThread.setName("simulated-playback-thread-" + i);
            playbackThreads.add(playbackThread);
        }
    }
    QueuedTask playbackThreadsManagementTask = new QueuedTask(threadExecutionQueue, new FakePlayerManagementTask(playbackThreads, audioSendSystems, durationSecs)) {

        @Override
        protected boolean isPrivileged() {
            return true;
        }
    };
    playbackThreadsManagementTask.setName("simulated-playback-management-task");
    threadExecutionQueue.add(playbackThreadsManagementTask, false);
}
Also used : CommandContext(net.robinfriedli.aiode.command.CommandContext) NativeAudioSendFactory(com.sedmelluq.discord.lavaplayer.jdaudp.NativeAudioSendFactory) ThreadExecutionQueue(net.robinfriedli.aiode.concurrent.ThreadExecutionQueue) AudioTrackLoader(net.robinfriedli.aiode.audio.AudioTrackLoader) AudioPlayer(com.sedmelluq.discord.lavaplayer.player.AudioPlayer) Guild(net.dv8tion.jda.api.entities.Guild) AudioItem(com.sedmelluq.discord.lavaplayer.track.AudioItem) AudioManager(net.robinfriedli.aiode.audio.AudioManager) SpotifyApi(se.michaelthelin.spotify.SpotifyApi) LoggingUncaughtExceptionHandler(net.robinfriedli.aiode.exceptions.handler.handlers.LoggingUncaughtExceptionHandler) BlockingTrackLoadingExecutor(net.robinfriedli.aiode.audio.exec.BlockingTrackLoadingExecutor) PlayableFactory(net.robinfriedli.aiode.audio.PlayableFactory) IAudioSendSystem(net.dv8tion.jda.api.audio.factory.IAudioSendSystem) Aiode(net.robinfriedli.aiode.Aiode) QueuedTask(net.robinfriedli.aiode.concurrent.QueuedTask) AudioPlayerManager(com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) Playable(net.robinfriedli.aiode.audio.Playable) Tuple2(groovy.lang.Tuple2) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack)

Example 3 with LoggingUncaughtExceptionHandler

use of net.robinfriedli.aiode.exceptions.handler.handlers.LoggingUncaughtExceptionHandler in project aiode by robinfriedli.

the class AbortCommand method doRun.

@Override
public void doRun() {
    CommandExecutionTask commandExecutionTask = getTask();
    Thread abortThread = new Thread(() -> {
        if (commandExecutionTask != null) {
            try {
                commandExecutionTask.await();
            } catch (InterruptedException ignored) {
            }
        }
        CommandExecutionQueueManager executionQueueManager = Aiode.get().getExecutionQueueManager();
        GuildContext guildContext = getContext().getGuildContext();
        ThreadExecutionQueue executionQueue = executionQueueManager.getForGuild(getContext().getGuild());
        PooledTrackLoadingExecutor pooledTrackLoadingExecutor = guildContext.getPooledTrackLoadingExecutor();
        ReplaceableTrackLoadingExecutor replaceableTrackLoadingExecutor = guildContext.getReplaceableTrackLoadingExecutor();
        if (executionQueue.isIdle() && pooledTrackLoadingExecutor.isIdle() && replaceableTrackLoadingExecutor.isIdle()) {
            EmbedBuilder embedBuilder = new EmbedBuilder();
            embedBuilder.setDescription("No commands are currently running");
            getMessageService().sendTemporary(embedBuilder, getContext().getChannel());
            setFailed(true);
        } else {
            executionQueue.abortAll();
            pooledTrackLoadingExecutor.abortAll();
            replaceableTrackLoadingExecutor.abort();
            sendSuccess("Sent all currently running commands an interrupt signal and cancelled queued commands.");
        }
    });
    abortThread.setName("aiode abort thread");
    abortThread.setUncaughtExceptionHandler(new LoggingUncaughtExceptionHandler());
    abortThread.start();
}
Also used : CommandExecutionTask(net.robinfriedli.aiode.concurrent.CommandExecutionTask) GuildContext(net.robinfriedli.aiode.discord.GuildContext) PooledTrackLoadingExecutor(net.robinfriedli.aiode.audio.exec.PooledTrackLoadingExecutor) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) ReplaceableTrackLoadingExecutor(net.robinfriedli.aiode.audio.exec.ReplaceableTrackLoadingExecutor) ThreadExecutionQueue(net.robinfriedli.aiode.concurrent.ThreadExecutionQueue) LoggingUncaughtExceptionHandler(net.robinfriedli.aiode.exceptions.handler.handlers.LoggingUncaughtExceptionHandler) CommandExecutionQueueManager(net.robinfriedli.aiode.concurrent.CommandExecutionQueueManager)

Example 4 with LoggingUncaughtExceptionHandler

use of net.robinfriedli.aiode.exceptions.handler.handlers.LoggingUncaughtExceptionHandler in project aiode by robinfriedli.

the class LoggingThreadFactory method newThread.

@Override
public Thread newThread(@NotNull Runnable r) {
    Thread thread = new Thread(r);
    thread.setName(poolName + "-thread-" + threadNumber.getAndIncrement());
    thread.setUncaughtExceptionHandler(new LoggingUncaughtExceptionHandler());
    return thread;
}
Also used : LoggingUncaughtExceptionHandler(net.robinfriedli.aiode.exceptions.handler.handlers.LoggingUncaughtExceptionHandler)

Aggregations

LoggingUncaughtExceptionHandler (net.robinfriedli.aiode.exceptions.handler.handlers.LoggingUncaughtExceptionHandler)4 ThreadExecutionQueue (net.robinfriedli.aiode.concurrent.ThreadExecutionQueue)2 NativeAudioSendFactory (com.sedmelluq.discord.lavaplayer.jdaudp.NativeAudioSendFactory)1 AudioPlayer (com.sedmelluq.discord.lavaplayer.player.AudioPlayer)1 AudioPlayerManager (com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager)1 AudioItem (com.sedmelluq.discord.lavaplayer.track.AudioItem)1 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)1 Tuple2 (groovy.lang.Tuple2)1 InetSocketAddress (java.net.InetSocketAddress)1 ExecutorService (java.util.concurrent.ExecutorService)1 ThreadFactory (java.util.concurrent.ThreadFactory)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)1 IAudioSendSystem (net.dv8tion.jda.api.audio.factory.IAudioSendSystem)1 Guild (net.dv8tion.jda.api.entities.Guild)1 Aiode (net.robinfriedli.aiode.Aiode)1 AudioManager (net.robinfriedli.aiode.audio.AudioManager)1 AudioTrackLoader (net.robinfriedli.aiode.audio.AudioTrackLoader)1 Playable (net.robinfriedli.aiode.audio.Playable)1