impl pre shutdown saving

This commit is contained in:
Lee 2024-07-01 01:33:52 +01:00
parent b1f5db9b2d
commit b1785ce373
5 changed files with 40 additions and 3 deletions

View File

@ -1,9 +1,12 @@
package cc.fascinated.bat;
import cc.fascinated.bat.config.Config;
import cc.fascinated.bat.event.EventListener;
import cc.fascinated.bat.service.EventService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.mongock.runner.springboot.EnableMongock;
import jakarta.annotation.PreDestroy;
import lombok.NonNull;
import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2;
@ -41,4 +44,12 @@ public class BatApplication {
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();
}
}
}

View File

@ -104,4 +104,10 @@ public interface EventListener {
*/
default void onGuildMemberUpdateNickname(@NonNull BatGuild guild, @NonNull BatUser user, String oldName, String newName, @NonNull GuildMemberUpdateNicknameEvent event) {
}
/**
* Called when Spring is shutting down
*/
default void onSpringShutdown() {
}
}

View File

@ -54,7 +54,7 @@ public class DisableSubCommand extends BatSubCommand {
}
featureProfile.disableFeature(feature);
interaction.replyEmbeds(EmbedUtils.successEmbed()
interaction.replyEmbeds(EmbedUtils.errorEmbed()
.setDescription("Successfully disabled the `%s` feature".formatted(feature.getName()))
.build()).queue();
}

View File

@ -1,6 +1,7 @@
package cc.fascinated.bat.service;
import cc.fascinated.bat.common.TimerUtils;
import cc.fascinated.bat.event.EventListener;
import cc.fascinated.bat.model.BatGuild;
import cc.fascinated.bat.premium.PremiumProfile;
import com.mongodb.client.model.Filters;
@ -28,7 +29,7 @@ import java.util.concurrent.TimeUnit;
@Log4j2(topic = "Guild Service")
@Getter
@DependsOn({"discordService", "mongoService"})
public class GuildService extends ListenerAdapter {
public class GuildService extends ListenerAdapter implements EventListener {
private static final long SAVE_INTERVAL = TimeUnit.MINUTES.toMillis(5);
/**
@ -101,4 +102,13 @@ public class GuildService extends ListenerAdapter {
guild.save();
guilds.remove(guild.getId());
}
@Override
public void onSpringShutdown() {
log.info("Saving all guilds before shutdown...");
for (BatGuild guild : guilds.values()) {
guild.save();
}
log.info("Saved all guilds.");
}
}

View File

@ -1,6 +1,7 @@
package cc.fascinated.bat.service;
import cc.fascinated.bat.common.TimerUtils;
import cc.fascinated.bat.event.EventListener;
import cc.fascinated.bat.model.BatUser;
import com.mongodb.client.model.Filters;
import lombok.Getter;
@ -22,7 +23,7 @@ import java.util.concurrent.TimeUnit;
@Log4j2(topic = "User Service")
@Getter
@DependsOn({"discordService", "mongoService"})
public class UserService {
public class UserService implements EventListener {
private static final long SAVE_INTERVAL = TimeUnit.MINUTES.toMillis(5);
/**
@ -67,4 +68,13 @@ public class UserService {
log.info("Created user \"{}\" - \"{}\"", user.getName(), user.getId());
return user;
}
@Override
public void onSpringShutdown() {
log.info("Saving all users before shutdown...");
for (BatUser user : users.values()) {
user.save();
}
log.info("Saved all users.");
}
}