Search in sources :

Example 1 with StartupTaskContribution

use of net.robinfriedli.aiode.entities.xml.StartupTaskContribution in project aiode by robinfriedli.

the class SpringBootstrap method run.

@Override
public void run(String... args) {
    Logger logger = LoggerFactory.getLogger(SpringBootstrap.class);
    logger.info("Using java version " + System.getProperty("java.runtime.version"));
    try {
        Aiode aiode = Aiode.get();
        CommandManager commandManager = aiode.getCommandManager();
        HttpServerManager serverManager = aiode.getHttpServerManager();
        JxpBackend jxpBackend = aiode.getJxpBackend();
        CronJobService cronJobService = aiode.getCronJobService();
        commandManager.initializeInterceptorChain();
        serverManager.start();
        // run startup tasks
        InputStream startupTasksFile = getClass().getResourceAsStream("/xml-contributions/startupTasks.xml");
        Context context = jxpBackend.createContext(startupTasksFile);
        for (StartupTaskContribution element : context.getInstancesOf(StartupTaskContribution.class)) {
            if (!aiode.isMainInstance() && element.getAttribute("mainInstanceOnly").getBool()) {
                continue;
            }
            if (!element.getAttribute("runForEachShard").getBool()) {
                element.instantiate().runTask(null);
            }
        }
        cronJobService.scheduleAll();
        Aiode.registerListeners();
        logger.info("All starters done");
    } catch (Throwable e) {
        logger.error("Exception in starter. Application will terminate.", e);
        System.exit(1);
    }
}
Also used : CronJobService(net.robinfriedli.aiode.cron.CronJobService) Context(net.robinfriedli.jxp.persist.Context) CommandManager(net.robinfriedli.aiode.command.CommandManager) InputStream(java.io.InputStream) JxpBackend(net.robinfriedli.jxp.api.JxpBackend) StartupTaskContribution(net.robinfriedli.aiode.entities.xml.StartupTaskContribution) Logger(org.slf4j.Logger) Aiode(net.robinfriedli.aiode.Aiode) HttpServerManager(net.robinfriedli.aiode.servers.HttpServerManager)

Example 2 with StartupTaskContribution

use of net.robinfriedli.aiode.entities.xml.StartupTaskContribution in project aiode by robinfriedli.

the class StartupListener method onReady.

@Override
public void onReady(@NotNull ReadyEvent event) {
    STARTUP_TASK_EXECUTOR.execute(() -> {
        JDA jda = event.getJDA();
        if (discordBotListAPI != null) {
            try {
                JDA.ShardInfo shardInfo = jda.getShardInfo();
                discordBotListAPI.setStats(shardInfo.getShardId(), shardInfo.getShardTotal(), (int) jda.getGuildCache().size());
            } catch (Exception e) {
                logger.error("Exception setting discordBotListAPI stats", e);
            }
        }
        CommandExecutionQueueManager executionQueueManager = aiode.getExecutionQueueManager();
        GuildManager guildManager = aiode.getGuildManager();
        StaticSessionProvider.consumeSession(session -> {
            // setup current thread session and handle all guilds within one session instead of opening a new session for each
            for (Guild guild : jda.getGuilds()) {
                executionQueueManager.addGuild(guild);
                guildManager.addGuild(guild);
            }
        });
        for (StartupTaskContribution element : startupTaskContributions) {
            if (element.getAttribute("runForEachShard").getBool()) {
                try {
                    element.instantiate().runTask(jda);
                } catch (Exception e) {
                    String msg = String.format("Startup task %s has thrown an exception for shard %s", element.getAttribute("implementation").getValue(), jda);
                    logger.error(msg, e);
                }
            }
        }
    });
}
Also used : GuildManager(net.robinfriedli.aiode.discord.GuildManager) JDA(net.dv8tion.jda.api.JDA) StartupTaskContribution(net.robinfriedli.aiode.entities.xml.StartupTaskContribution) Guild(net.dv8tion.jda.api.entities.Guild) CommandExecutionQueueManager(net.robinfriedli.aiode.concurrent.CommandExecutionQueueManager)

Aggregations

StartupTaskContribution (net.robinfriedli.aiode.entities.xml.StartupTaskContribution)2 InputStream (java.io.InputStream)1 JDA (net.dv8tion.jda.api.JDA)1 Guild (net.dv8tion.jda.api.entities.Guild)1 Aiode (net.robinfriedli.aiode.Aiode)1 CommandManager (net.robinfriedli.aiode.command.CommandManager)1 CommandExecutionQueueManager (net.robinfriedli.aiode.concurrent.CommandExecutionQueueManager)1 CronJobService (net.robinfriedli.aiode.cron.CronJobService)1 GuildManager (net.robinfriedli.aiode.discord.GuildManager)1 HttpServerManager (net.robinfriedli.aiode.servers.HttpServerManager)1 JxpBackend (net.robinfriedli.jxp.api.JxpBackend)1 Context (net.robinfriedli.jxp.persist.Context)1 Logger (org.slf4j.Logger)1