From bdec0b7ef447a60ae5c1701068d47addfaa6c986 Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Wed, 20 Apr 2016 10:06:35 +1000 Subject: [PATCH] Delete old history --- core/src/main/java/com/boydti/fawe/Fawe.java | 5 ++- .../java/com/boydti/fawe/config/Settings.java | 3 ++ .../java/com/boydti/fawe/util/MainUtil.java | 36 +++++++++++++++++-- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/com/boydti/fawe/Fawe.java b/core/src/main/java/com/boydti/fawe/Fawe.java index 6e1de1b8..c6af93c2 100644 --- a/core/src/main/java/com/boydti/fawe/Fawe.java +++ b/core/src/main/java/com/boydti/fawe/Fawe.java @@ -10,6 +10,7 @@ import com.boydti.fawe.config.Settings; import com.boydti.fawe.object.FawePlayer; import com.boydti.fawe.regions.general.PlotSquaredFeature; import com.boydti.fawe.util.Lag; +import com.boydti.fawe.util.MainUtil; import com.boydti.fawe.util.MemUtil; import com.boydti.fawe.util.TaskManager; import com.boydti.fawe.util.WEManager; @@ -43,6 +44,7 @@ import java.lang.management.MemoryPoolMXBean; import java.lang.management.MemoryUsage; import java.util.List; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; import javax.management.InstanceAlreadyExistsException; import javax.management.Notification; import javax.management.NotificationEmitter; @@ -143,12 +145,13 @@ public class Fawe { private Fawe(final IFawe implementation) { this.IMP = implementation; - this.thread = Thread.currentThread(); /* * Implementation dependent stuff */ this.setupConfigs(); + MainUtil.deleteOlder(new File(IMP.getDirectory(), "history"), TimeUnit.DAYS.toMillis(Settings.DELETE_HISTORY_AFTER_DAYS)); + this.setupCommands(); // TODO command event - queue? diff --git a/core/src/main/java/com/boydti/fawe/config/Settings.java b/core/src/main/java/com/boydti/fawe/config/Settings.java index 5bf0bb75..570d9bf4 100644 --- a/core/src/main/java/com/boydti/fawe/config/Settings.java +++ b/core/src/main/java/com/boydti/fawe/config/Settings.java @@ -21,6 +21,7 @@ public class Settings { public static long MEM_FREE = 95; public static boolean ENABLE_HARD_LIMIT = true; public static boolean STORE_HISTORY_ON_DISK = false; + public static int DELETE_HISTORY_AFTER_DAYS = 7; public static int COMPRESSION_LEVEL = 0; public static int BUFFER_SIZE = 531441; public static boolean METRICS = true; @@ -79,6 +80,7 @@ public class Settings { options.put("history.compress", false); options.put("history.chunk-wait-ms", CHUNK_WAIT); options.put("history.buffer-size", BUFFER_SIZE); + options.put("history.delete-after-days", DELETE_HISTORY_AFTER_DAYS); options.put("region-restrictions", REGION_RESTRICTIONS); options.put("queue.extra-time-ms", ALLOCATE); options.put("queue.target-size", QUEUE_SIZE); @@ -112,6 +114,7 @@ public class Settings { REGION_RESTRICTIONS = config.getBoolean("region-restrictions"); METRICS = config.getBoolean("metrics"); COMPRESSION_LEVEL = config.getInt("history.compression-level", config.getBoolean("history.compress") ? 1 : 0); + DELETE_HISTORY_AFTER_DAYS = config.getInt("history.delete-after-days"); BUFFER_SIZE = config.getInt("history.buffer-size", BUFFER_SIZE); CHUNK_WAIT = config.getInt("history.chunk-wait-ms"); ALLOCATE = config.getInt("queue.extra-time-ms"); diff --git a/core/src/main/java/com/boydti/fawe/util/MainUtil.java b/core/src/main/java/com/boydti/fawe/util/MainUtil.java index d484fd6c..08c2d5d2 100644 --- a/core/src/main/java/com/boydti/fawe/util/MainUtil.java +++ b/core/src/main/java/com/boydti/fawe/util/MainUtil.java @@ -3,6 +3,7 @@ package com.boydti.fawe.util; import com.boydti.fawe.Fawe; import com.boydti.fawe.config.BBC; import com.boydti.fawe.object.FawePlayer; +import com.boydti.fawe.object.RunnableVal; import com.sk89q.jnbt.CompoundTag; import com.sk89q.jnbt.EndTag; import com.sk89q.jnbt.ListTag; @@ -33,16 +34,47 @@ public class MainUtil { } Fawe.debug(s); } + + public static void iterateFiles(File directory, RunnableVal task) { + if (directory.exists()) { + File[] files = directory.listFiles(); + if (null != files) { + for (int i = 0; i < files.length; i++) { + if (files[i].isDirectory()) { + iterateFiles(files[i], task); + } else { + task.run(files[i]); + } + } + } + } + } + + public static void deleteOlder(File directory, final long timeDiff) { + final long now = System.currentTimeMillis(); + iterateFiles(directory, new RunnableVal() { + @Override + public void run(File file) { + long age = now - file.lastModified(); + if (age > timeDiff) { + System.out.println("Deleting file: " + file); + file.delete(); + } + } + }); + } public static boolean deleteDirectory(File directory) { if (directory.exists()) { File[] files = directory.listFiles(); if (null != files) { for (int i = 0; i < files.length; i++) { - if (files[i].isDirectory()) { + File file = files[i]; + if (file.isDirectory()) { deleteDirectory(files[i]); } else { - files[i].delete(); + System.out.println("Deleting file: " + file); + file.delete(); } } }