summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/autobackup
diff options
context:
space:
mode:
authorKeuin <[email protected]>2021-02-15 17:52:53 +0800
committerKeuin <[email protected]>2021-02-15 17:52:53 +0800
commit6d6f0b486ac42b6bd69ddb8862ee3af7b819f3a7 (patch)
treece01c3a18b7e90af6204ff0db90a606eade81778 /src/main/java/com/keuin/kbackupfabric/autobackup
parent68b102c763b0d0b21fb71575f1238d3cc879f959 (diff)
Add tests.
Implementing auto backup and configuration (not finished)
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/autobackup')
-rw-r--r--src/main/java/com/keuin/kbackupfabric/autobackup/AutoBackupScheduler.java50
-rw-r--r--src/main/java/com/keuin/kbackupfabric/autobackup/PlayerActivityTracker.java15
2 files changed, 65 insertions, 0 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/autobackup/AutoBackupScheduler.java b/src/main/java/com/keuin/kbackupfabric/autobackup/AutoBackupScheduler.java
new file mode 100644
index 0000000..c35a35d
--- /dev/null
+++ b/src/main/java/com/keuin/kbackupfabric/autobackup/AutoBackupScheduler.java
@@ -0,0 +1,50 @@
+package com.keuin.kbackupfabric.autobackup;
+
+import java.util.Optional;
+import java.util.Timer;
+import java.util.TimerTask;
+import java.util.logging.Logger;
+
+public class AutoBackupScheduler {
+
+ private Timer timer = null;
+ private final Logger logger = Logger.getLogger(AutoBackupScheduler.class.getName());
+ private boolean skipIfNoPlayerLoggedIn;
+ private final PlayerActivityTracker playerActivityTracker;
+
+ public AutoBackupScheduler(int intervalSeconds, boolean skipIfNoPlayerLoggedIn, PlayerActivityTracker playerActivityTracker) {
+ this.skipIfNoPlayerLoggedIn = skipIfNoPlayerLoggedIn;
+ this.playerActivityTracker = playerActivityTracker;
+ if (intervalSeconds > 0)
+ setInterval(intervalSeconds);
+ }
+
+ public synchronized void setInterval(int intervalSeconds) {
+ Optional.ofNullable(timer).ifPresent(Timer::cancel);
+ Timer newTimer = new Timer("AutoBackupTimer");
+ newTimer.schedule(new TimerTask() {
+ @Override
+ public void run() {
+ toggleBackup();
+ }
+ }, 0L, intervalSeconds * 1000L);
+ timer = newTimer;
+ }
+
+ public void setSkipIfNoPlayerLoggedIn(boolean skipIfNoPlayerLoggedIn) {
+ this.skipIfNoPlayerLoggedIn = skipIfNoPlayerLoggedIn;
+ }
+
+ public synchronized void stop() {
+ timer.cancel();
+ timer = null;
+ }
+
+ private void toggleBackup() {
+ if (playerActivityTracker.getCheckpoint() || !skipIfNoPlayerLoggedIn) {
+ logger.info("Interval backup event is triggered.");
+ // TODO: perform a backup
+ }
+ }
+
+}
diff --git a/src/main/java/com/keuin/kbackupfabric/autobackup/PlayerActivityTracker.java b/src/main/java/com/keuin/kbackupfabric/autobackup/PlayerActivityTracker.java
new file mode 100644
index 0000000..ef2b85e
--- /dev/null
+++ b/src/main/java/com/keuin/kbackupfabric/autobackup/PlayerActivityTracker.java
@@ -0,0 +1,15 @@
+package com.keuin.kbackupfabric.autobackup;
+
+public interface PlayerActivityTracker {
+ /**
+ * Update the checkpoint, return accumulated result.
+ *
+ * @return if there is at least one player logged in since last checkpoint.
+ */
+ boolean getCheckpoint();
+
+ /**
+ * Mark dirty. In the next checkpoint, the backup will be performed.
+ */
+ void setCheckpoint();
+}