From fb215e06cd84615d9d86e496e2af4d0586c0a4ba Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Wed, 13 Sep 2023 00:33:47 -0400 Subject: [PATCH] Start an auto-save task which runs every 5 minutes this should be configurable --- src/main/java/emu/grasscutter/Grasscutter.java | 18 +++++++++++------- .../emu/grasscutter/database/Database.java | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/main/java/emu/grasscutter/Grasscutter.java b/src/main/java/emu/grasscutter/Grasscutter.java index e649b537c..0bb6e1be8 100644 --- a/src/main/java/emu/grasscutter/Grasscutter.java +++ b/src/main/java/emu/grasscutter/Grasscutter.java @@ -1,8 +1,5 @@ package emu.grasscutter; -import static emu.grasscutter.config.Configuration.SERVER; -import static emu.grasscutter.utils.lang.Language.translate; - import ch.qos.logback.classic.*; import emu.grasscutter.auth.*; import emu.grasscutter.command.*; @@ -21,16 +18,20 @@ import emu.grasscutter.tools.Tools; import emu.grasscutter.utils.*; import emu.grasscutter.utils.lang.Language; import io.netty.util.concurrent.FastThreadLocalThread; -import java.io.*; -import java.util.Calendar; -import java.util.concurrent.*; -import javax.annotation.Nullable; import lombok.*; import org.jline.reader.*; import org.jline.terminal.*; import org.reflections.Reflections; import org.slf4j.LoggerFactory; +import javax.annotation.Nullable; +import java.io.*; +import java.util.Calendar; +import java.util.concurrent.*; + +import static emu.grasscutter.config.Configuration.SERVER; +import static emu.grasscutter.utils.lang.Language.translate; + public final class Grasscutter { public static final File configFile = new File("./config.json"); public static final Reflections reflector = new Reflections("emu.grasscutter"); @@ -181,6 +182,9 @@ public final class Grasscutter { // Hook into shutdown event. Runtime.getRuntime().addShutdownHook(new Thread(Grasscutter::onShutdown)); + // Start database heartbeat. + Database.startSaveThread(); + // Open console. Grasscutter.startConsole(); } diff --git a/src/main/java/emu/grasscutter/database/Database.java b/src/main/java/emu/grasscutter/database/Database.java index d9e26eb83..3521e631d 100644 --- a/src/main/java/emu/grasscutter/database/Database.java +++ b/src/main/java/emu/grasscutter/database/Database.java @@ -68,4 +68,18 @@ public interface Database { }); } } + + /** + * Starts the auto-save thread. + * Runs every 5 minutes. + */ + static void startSaveThread() { + var timer = new Timer(); + timer.scheduleAtFixedRate(new TimerTask() { + @Override + public void run() { + Database.saveAll(); + } + }, 0, 1000 * 60 * 5); + } }