package dev.nincodedo.ninbot;

import dev.nincodedo.ninbot.common.Schedulable;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.sharding.ShardManager;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

@Slf4j
@SpringBootApplication
@EnableCaching
@EnableScheduling
@EnableJpaAuditing
public class NinbotRunner {

    private final ShardManager shardManager;

    private final List<Schedulable> schedulableList;

    public NinbotRunner(ShardManager shardManager, List<Schedulable> schedulableList) {
        this.shardManager = shardManager;
        this.schedulableList = schedulableList;
    }

    public static void main(String[] args) {
        //TODO remove once openjdk docker image is updated to Java 17.0.2+
        // https://github.com/docker-library/openjdk/commits/master
        final int cores = Runtime.getRuntime().availableProcessors();
        log.trace("Available Cores: {}", cores);
        if (cores <= 1) {
            log.info("Only one core available, setting Parallelism Flag");
            System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "1");
        }
        SpringApplication.run(NinbotRunner.class, args);
    }

    private static void waitForShardStartup(JDA jda) {
        try {
            jda.awaitReady();
            log.info("Shard ID {}: Connected to {} server(s)", jda.getShardInfo().getShardId(), jda.getGuilds().size());
            jda.getGuilds().forEach(guild ->
                    log.info("Server ID: {}, Server Name: {}, Owner ID: {}, Owner Name: {}", guild.getId(),
                            guild.getName(), guild.getOwnerId(), guild.getOwner()
                                    .getEffectiveName()));
        } catch (InterruptedException e) {
            log.error("Failed to wait for shard to start", e);
        }
    }

    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext context) {
        return args -> {
            var shards = shardManager.getShards();
            log.info("Starting Ninbot with {} shard(s)", shards.size());
            shardManager.getShards().forEach(NinbotRunner::waitForShardStartup);
            schedulableList.forEach(schedule -> schedule.scheduleAll(shardManager));
            setNinbotActivity();

            Runtime.getRuntime().addShutdownHook(new Thread(() -> {
                shardManager.setActivity(Activity.playing("Shutting down, be back in a few!"));
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    log.error("Failed to sleep before shutdown", e);
                }
            }));
        };
    }

    @Scheduled(fixedRate = 3600000)
    private void setNinbotActivity() {
        shardManager.setActivity(Activity.playing("Check out my fancy new slash commands!"));
    }
}

@@ -1241,474 +1241,8 @@
 ) %7B%0A
-        //TODO remove once openjdk docker image is updated to Java 17.0.2+%0A        // https://github.com/docker-library/openjdk/commits/master%0A        final int cores = Runtime.getRuntime().availableProcessors();%0A        log.trace(%22Available Cores: %7B%7D%22, cores);%0A        if (cores %3C= 1) %7B%0A            log.info(%22Only one core available, setting Parallelism Flag%22);%0A            System.setProperty(%22java.util.concurrent.ForkJoinPool.common.parallelism%22, %221%22);%0A        %7D%0A
     

Remove temporary workaround