Delete files in the background

This commit is contained in:
Jesse Boyd 2018-05-17 19:00:03 +10:00
parent d66c14b06d
commit 3cae6e08c8
No known key found for this signature in database
GPG Key ID: 59F1DE6293AF6E1F
2 changed files with 28 additions and 6 deletions

View File

@ -218,8 +218,13 @@ public class Fawe {
* Implementation dependent stuff * Implementation dependent stuff
*/ */
this.setupConfigs(); this.setupConfigs();
MainUtil.deleteOlder(MainUtil.getFile(IMP.getDirectory(), Settings.IMP.PATHS.HISTORY), TimeUnit.DAYS.toMillis(Settings.IMP.HISTORY.DELETE_AFTER_DAYS)); TaskManager.IMP.async(new Runnable() {
MainUtil.deleteOlder(MainUtil.getFile(IMP.getDirectory(), Settings.IMP.PATHS.CLIPBOARD), TimeUnit.DAYS.toMillis(Settings.IMP.CLIPBOARD.DELETE_AFTER_DAYS)); @Override
public void run() {
MainUtil.deleteOlder(MainUtil.getFile(IMP.getDirectory(), Settings.IMP.PATHS.HISTORY), TimeUnit.DAYS.toMillis(Settings.IMP.HISTORY.DELETE_AFTER_DAYS), false);
MainUtil.deleteOlder(MainUtil.getFile(IMP.getDirectory(), Settings.IMP.PATHS.CLIPBOARD), TimeUnit.DAYS.toMillis(Settings.IMP.CLIPBOARD.DELETE_AFTER_DAYS), false);
}
});
TaskManager.IMP = this.IMP.getTaskManager(); TaskManager.IMP = this.IMP.getTaskManager();
if (Settings.IMP.METRICS) { if (Settings.IMP.METRICS) {

View File

@ -28,6 +28,8 @@ import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.util.*; import java.util.*;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.zip.*; import java.util.zip.*;
@ -1042,30 +1044,45 @@ public class MainUtil {
} }
public static void deleteOlder(File directory, final long timeDiff) { public static void deleteOlder(File directory, final long timeDiff) {
deleteOlder(directory, timeDiff, true);
}
public static void deleteOlder(File directory, final long timeDiff, boolean printDebug) {
final long now = System.currentTimeMillis(); final long now = System.currentTimeMillis();
ForkJoinPool pool = new ForkJoinPool();
iterateFiles(directory, new RunnableVal<File>() { iterateFiles(directory, new RunnableVal<File>() {
@Override @Override
public void run(File file) { public void run(File file) {
long age = now - file.lastModified(); long age = now - file.lastModified();
if (age > timeDiff) { if (age > timeDiff) {
file.delete(); pool.submit(() -> file.delete());
BBC.FILE_DELETED.send(null, file); if (printDebug) BBC.FILE_DELETED.send(null, file);
} }
} }
}); });
pool.shutdown();
try {
pool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
public static boolean deleteDirectory(File directory) { public static boolean deleteDirectory(File directory) {
return deleteDirectory(directory, true);
}
public static boolean deleteDirectory(File directory, boolean printDebug) {
if (directory.exists()) { if (directory.exists()) {
File[] files = directory.listFiles(); File[] files = directory.listFiles();
if (null != files) { if (null != files) {
for (int i = 0; i < files.length; i++) { for (int i = 0; i < files.length; i++) {
File file = files[i]; File file = files[i];
if (file.isDirectory()) { if (file.isDirectory()) {
deleteDirectory(files[i]); deleteDirectory(files[i], printDebug);
} else { } else {
file.delete(); file.delete();
BBC.FILE_DELETED.send(null, file); if (printDebug) BBC.FILE_DELETED.send(null, file);
} }
} }
} }