impl pre shutdown saving
All checks were successful
Deploy to Dokku / docker (ubuntu-latest) (push) Successful in 37s

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

@ -1,9 +1,12 @@
package cc.fascinated.bat; package cc.fascinated.bat;
import cc.fascinated.bat.config.Config; 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.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import io.mongock.runner.springboot.EnableMongock; import io.mongock.runner.springboot.EnableMongock;
import jakarta.annotation.PreDestroy;
import lombok.NonNull; import lombok.NonNull;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2; 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")); 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();
}
}
} }

@ -104,4 +104,10 @@ public interface EventListener {
*/ */
default void onGuildMemberUpdateNickname(@NonNull BatGuild guild, @NonNull BatUser user, String oldName, String newName, @NonNull GuildMemberUpdateNicknameEvent event) { 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() {
}
} }

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

@ -1,6 +1,7 @@
package cc.fascinated.bat.service; package cc.fascinated.bat.service;
import cc.fascinated.bat.common.TimerUtils; import cc.fascinated.bat.common.TimerUtils;
import cc.fascinated.bat.event.EventListener;
import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatGuild;
import cc.fascinated.bat.premium.PremiumProfile; import cc.fascinated.bat.premium.PremiumProfile;
import com.mongodb.client.model.Filters; import com.mongodb.client.model.Filters;
@ -28,7 +29,7 @@ import java.util.concurrent.TimeUnit;
@Log4j2(topic = "Guild Service") @Log4j2(topic = "Guild Service")
@Getter @Getter
@DependsOn({"discordService", "mongoService"}) @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); private static final long SAVE_INTERVAL = TimeUnit.MINUTES.toMillis(5);
/** /**
@ -101,4 +102,13 @@ public class GuildService extends ListenerAdapter {
guild.save(); guild.save();
guilds.remove(guild.getId()); 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.");
}
} }

@ -1,6 +1,7 @@
package cc.fascinated.bat.service; package cc.fascinated.bat.service;
import cc.fascinated.bat.common.TimerUtils; import cc.fascinated.bat.common.TimerUtils;
import cc.fascinated.bat.event.EventListener;
import cc.fascinated.bat.model.BatUser; import cc.fascinated.bat.model.BatUser;
import com.mongodb.client.model.Filters; import com.mongodb.client.model.Filters;
import lombok.Getter; import lombok.Getter;
@ -22,7 +23,7 @@ import java.util.concurrent.TimeUnit;
@Log4j2(topic = "User Service") @Log4j2(topic = "User Service")
@Getter @Getter
@DependsOn({"discordService", "mongoService"}) @DependsOn({"discordService", "mongoService"})
public class UserService { public class UserService implements EventListener {
private static final long SAVE_INTERVAL = TimeUnit.MINUTES.toMillis(5); 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()); log.info("Created user \"{}\" - \"{}\"", user.getName(), user.getId());
return user; 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.");
}
} }