diff --git a/Dockerfile b/Dockerfile index 5a793bd..b811561 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,7 @@ FROM gradle:jdk21-alpine as build COPY . . -RUN gradle clean build --no-daemon +RUN gradle clean :bot:build :plugin-paper:build --no-daemon # We use a jammy image because we need some more stuff than alpine provides FROM eclipse-temurin:21-jammy as runtime @@ -26,4 +26,6 @@ COPY docker/resources/docker-entrypoint.sh . EXPOSE 8080 +HEALTHCHECK CMD curl --fail http://localhost:8080 || exit 1 + ENTRYPOINT ["bash", "docker-entrypoint.sh"] diff --git a/bot/src/main/java/de/chojo/gamejam/Bot.java b/bot/src/main/java/de/chojo/gamejam/Bot.java index e891f39..c85b2dc 100644 --- a/bot/src/main/java/de/chojo/gamejam/Bot.java +++ b/bot/src/main/java/de/chojo/gamejam/Bot.java @@ -6,6 +6,7 @@ package de.chojo.gamejam; +import com.zaxxer.hikari.HikariDataSource; import de.chojo.gamejam.api.Api; import de.chojo.gamejam.commands.jamadmin.JamAdmin; import de.chojo.gamejam.commands.register.Register; @@ -40,6 +41,7 @@ import net.dv8tion.jda.api.sharding.DefaultShardManagerBuilder; import net.dv8tion.jda.api.sharding.ShardManager; import net.dv8tion.jda.api.utils.MemberCachePolicy; +import org.apache.logging.log4j.LogManager; import org.slf4j.Logger; import javax.security.auth.login.LoginException; @@ -74,12 +76,13 @@ public class Bot { } private Configuration configuration; - private DataSource dataSource; + private HikariDataSource dataSource; private ILocalizer localizer; private ShardManager shardManager; private Guilds guilds; private ServerService serverService; private Teams teams; + private Api api; private static ThreadFactory createThreadFactory(String name) { return createThreadFactory(new ThreadGroup(name)); @@ -130,9 +133,22 @@ public void start() throws IOException, SQLException, LoginException { log.info("Initializing Commands"); buildCommands(); + buildShutdownHook(); + log.info("Starting api"); - Api.create(configuration, shardManager, guilds, teams, serverService); + api = Api.create(configuration, shardManager, guilds, teams, serverService); + + } + private void buildShutdownHook() { + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + log.info("Received SIGTERM. Shutdown hook activated."); + api.shutdown(); + shardManager.shutdown(); + serverService.shutdown(); + dataSource.close(); + LogManager.shutdown(); + })); } private void buildLocale() { @@ -164,6 +180,7 @@ private void buildCommands() { private void initBot() { shardManager = DefaultShardManagerBuilder.createDefault(configuration.baseSettings().token()) + .setEnableShutdownHook(false) .enableIntents( GatewayIntent.GUILD_MEMBERS, GatewayIntent.DIRECT_MESSAGES, diff --git a/bot/src/main/java/de/chojo/gamejam/api/Api.java b/bot/src/main/java/de/chojo/gamejam/api/Api.java index 38b2b1e..2cce371 100644 --- a/bot/src/main/java/de/chojo/gamejam/api/Api.java +++ b/bot/src/main/java/de/chojo/gamejam/api/Api.java @@ -114,4 +114,8 @@ private static OpenApiPlugin getConfiguredOpenApiPlugin() { }); return new OpenApiPlugin(options); } + + public void shutdown() { + app.stop(); + } } diff --git a/bot/src/main/java/de/chojo/gamejam/server/ServerService.java b/bot/src/main/java/de/chojo/gamejam/server/ServerService.java index cefd77c..81a7b82 100644 --- a/bot/src/main/java/de/chojo/gamejam/server/ServerService.java +++ b/bot/src/main/java/de/chojo/gamejam/server/ServerService.java @@ -49,6 +49,12 @@ private ServerService(Configuration configuration) { .forEach(freePorts::add); } + public void shutdown() { + server.forEach((team, teamServer) -> { + teamServer.stop(); + }); + } + @Override public void run() { for (var value : server.values()) {