summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/autobackup/AutoBackupDispatcher.java
diff options
context:
space:
mode:
authorKeuin <[email protected]>2021-02-15 18:13:44 +0800
committerKeuin <[email protected]>2021-02-15 18:13:44 +0800
commitb171d46ce07a9ed9a426779fcff92dd1f56b3dd1 (patch)
tree3df106d0000c12a7749fd24e28d2fde69983ae39 /src/main/java/com/keuin/kbackupfabric/autobackup/AutoBackupDispatcher.java
parent6d6f0b486ac42b6bd69ddb8862ee3af7b819f3a7 (diff)
Minor bugfix: incremental backup without `backups` directory created will fail.
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/autobackup/AutoBackupDispatcher.java')
-rw-r--r--src/main/java/com/keuin/kbackupfabric/autobackup/AutoBackupDispatcher.java50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/autobackup/AutoBackupDispatcher.java b/src/main/java/com/keuin/kbackupfabric/autobackup/AutoBackupDispatcher.java
new file mode 100644
index 0000000..32d626e
--- /dev/null
+++ b/src/main/java/com/keuin/kbackupfabric/autobackup/AutoBackupDispatcher.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 AutoBackupDispatcher {
+
+ private Timer timer = null;
+ private final Logger logger = Logger.getLogger(AutoBackupDispatcher.class.getName());
+ private boolean skipIfNoPlayerLoggedIn;
+ private final PlayerActivityTracker playerActivityTracker;
+
+ public AutoBackupDispatcher(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
+ }
+ }
+
+}