Skip to content

Commit

Permalink
Add healthcheck and shutdown hook
Browse files Browse the repository at this point in the history
  • Loading branch information
rainbowdashlabs committed Aug 19, 2024
1 parent 9340d5b commit 9d3bc33
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"]
21 changes: 19 additions & 2 deletions bot/src/main/java/de/chojo/gamejam/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions bot/src/main/java/de/chojo/gamejam/api/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,8 @@ private static OpenApiPlugin getConfiguredOpenApiPlugin() {
});
return new OpenApiPlugin(options);
}

public void shutdown() {
app.stop();
}
}
6 changes: 6 additions & 0 deletions bot/src/main/java/de/chojo/gamejam/server/ServerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down

0 comments on commit 9d3bc33

Please sign in to comment.