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,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.");
}
}