fix shutdown db saving

This commit is contained in:
Lee 2024-07-04 02:25:26 +01:00
parent 78daf4531b
commit 96486bb3a1
5 changed files with 13 additions and 14 deletions

@ -39,16 +39,15 @@ public class BatApplication {
} }
log.info("Found configuration at '{}'", config.getAbsolutePath()); // Log the found config log.info("Found configuration at '{}'", config.getAbsolutePath()); // Log the found config
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
log.info("Shutting down...");
for (EventListener listener : EventService.LISTENERS) {
listener.onShutdown();
}
}));
// Start the app // Start the app
SpringApplication.run(BatApplication.class, args); SpringApplication.run(BatApplication.class, args);
log.info("APP IS RUNNING IN %s MODE!!!!!!!!!".formatted(Config.isProduction() ? "PRODUCTION" : "DEVELOPMENT")); log.info("APP IS RUNNING IN %s MODE!!!!!!!!!".formatted(Config.isProduction() ? "PRODUCTION" : "DEVELOPMENT"));
} }
@PreDestroy
public void onShutdown() {
log.info("Shutting down...");
for (EventListener listener : EventService.LISTENERS) {
listener.onSpringShutdown();
}
}
} }

@ -243,8 +243,8 @@ public interface EventListener {
} }
/** /**
* Called when Spring is shutting down * Called when Bat is shutting down
*/ */
default void onSpringShutdown() { default void onShutdown() {
} }
} }

@ -80,7 +80,7 @@ public class ChannelListener implements EventListener {
if (!joined) { if (!joined) {
lastVoiceChannel.remove(user); lastVoiceChannel.remove(user);
} }
log.info("User \"{}\" {} voice channel \"{}\" in guild \"{}\"", user.getId(), joined ? "joined" : "left", voiceChannel.getName(), guild.getName()); log.info("User \"{}\" {} voice channel \"{}\" in guild \"{}\"", user.getName(), joined ? "joined" : "left", voiceChannel.getName(), guild.getName());
String description = new EmbedDescriptionBuilder("User %s Voice Channel".formatted(joined ? "Joined" : "Left")) String description = new EmbedDescriptionBuilder("User %s Voice Channel".formatted(joined ? "Joined" : "Left"))
.appendLine("User: %s".formatted(user.getDiscordUser().getAsMention()), true) .appendLine("User: %s".formatted(user.getDiscordUser().getAsMention()), true)
.appendLine("Channel: %s".formatted(voiceChannel.getAsMention()), true) .appendLine("Channel: %s".formatted(voiceChannel.getAsMention()), true)

@ -114,7 +114,7 @@ public class GuildService extends ListenerAdapter implements EventListener {
} }
@Override @Override
public void onSpringShutdown() { public void onShutdown() {
log.info("Saving all guilds before shutdown..."); log.info("Saving all guilds before shutdown...");
for (BatGuild guild : guilds.values()) { for (BatGuild guild : guilds.values()) {
guild.save(); guild.save();

@ -83,7 +83,7 @@ public class UserService implements EventListener {
} }
@Override @Override
public void onSpringShutdown() { public void onShutdown() {
log.info("Saving all users before shutdown..."); log.info("Saving all users before shutdown...");
for (BatUser user : users.values()) { for (BatUser user : users.values()) {
user.save(); user.save();
@ -93,7 +93,7 @@ public class UserService implements EventListener {
@Override @Override
public void onGuildMemberJoin(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull GuildMemberJoinEvent event) { public void onGuildMemberJoin(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull GuildMemberJoinEvent event) {
user.setGlobalName(event.getUser().getName()); // Ensure the user's name is up-to-date user.setGlobalName(event.getUser().getGlobalName()); // Ensure the user's name is up-to-date
} }
@Override @Override