diff --git a/Plan/bukkit/src/main/java/com/djrapitops/plan/modules/bukkit/BukkitTaskModule.java b/Plan/bukkit/src/main/java/com/djrapitops/plan/modules/bukkit/BukkitTaskModule.java index b817aee32..e52ec1048 100644 --- a/Plan/bukkit/src/main/java/com/djrapitops/plan/modules/bukkit/BukkitTaskModule.java +++ b/Plan/bukkit/src/main/java/com/djrapitops/plan/modules/bukkit/BukkitTaskModule.java @@ -27,6 +27,7 @@ import com.djrapitops.plan.gathering.timed.SystemUsageBuffer; import com.djrapitops.plan.settings.upkeep.ConfigStoreTask; import com.djrapitops.plan.storage.upkeep.DBCleanTask; import com.djrapitops.plan.storage.upkeep.LogsFolderCleanTask; +import com.djrapitops.plan.storage.upkeep.OldDependencyCacheDeletionTask; import dagger.Binds; import dagger.Module; import dagger.multibindings.IntoSet; @@ -79,4 +80,7 @@ public interface BukkitTaskModule { @IntoSet TaskSystem.Task bindShutdownDataPreservation(ShutdownDataPreservation dataPreservation); + @Binds + @IntoSet + TaskSystem.Task bindOldDependencyCacheDeletion(OldDependencyCacheDeletionTask deletionTask); } diff --git a/Plan/bungeecord/src/main/java/com/djrapitops/plan/modules/bungee/BungeeTaskModule.java b/Plan/bungeecord/src/main/java/com/djrapitops/plan/modules/bungee/BungeeTaskModule.java index c155abf78..1fb9697cc 100644 --- a/Plan/bungeecord/src/main/java/com/djrapitops/plan/modules/bungee/BungeeTaskModule.java +++ b/Plan/bungeecord/src/main/java/com/djrapitops/plan/modules/bungee/BungeeTaskModule.java @@ -25,6 +25,7 @@ import com.djrapitops.plan.gathering.timed.SystemUsageBuffer; import com.djrapitops.plan.settings.upkeep.NetworkConfigStoreTask; import com.djrapitops.plan.storage.upkeep.DBCleanTask; import com.djrapitops.plan.storage.upkeep.LogsFolderCleanTask; +import com.djrapitops.plan.storage.upkeep.OldDependencyCacheDeletionTask; import dagger.Binds; import dagger.Module; import dagger.multibindings.IntoSet; @@ -67,4 +68,8 @@ public interface BungeeTaskModule { @Binds @IntoSet TaskSystem.Task bindJSONFileStorageCleanTask(JSONFileStorage.CleanTask cleanTask); + + @Binds + @IntoSet + TaskSystem.Task bindOldDependencyCacheDeletion(OldDependencyCacheDeletionTask deletionTask); } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/upkeep/LogsFolderCleanTask.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/upkeep/LogsFolderCleanTask.java index 59c973972..b361744d3 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/storage/upkeep/LogsFolderCleanTask.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/upkeep/LogsFolderCleanTask.java @@ -29,7 +29,6 @@ import javax.inject.Singleton; import java.io.File; import java.io.IOException; import java.nio.file.Files; -import java.util.Objects; import java.util.concurrent.TimeUnit; /** @@ -81,7 +80,9 @@ public class LogsFolderCleanTask extends TaskSystem.Task { private void cleanFolder() { long now = System.currentTimeMillis(); - for (File file : Objects.requireNonNull(folder.listFiles())) { + File[] files = folder.listFiles(); + if (files == null) return; + for (File file : files) { long forDaysMs = TimeUnit.DAYS.toMillis(config.get(PluginSettings.KEEP_LOGS_DAYS)); if (now - file.lastModified() > (forDaysMs > 0 ? forDaysMs : TimeUnit.DAYS.toMillis(1L))) { try { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/storage/upkeep/OldDependencyCacheDeletionTask.java b/Plan/common/src/main/java/com/djrapitops/plan/storage/upkeep/OldDependencyCacheDeletionTask.java new file mode 100644 index 000000000..0db243db2 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/storage/upkeep/OldDependencyCacheDeletionTask.java @@ -0,0 +1,86 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.storage.upkeep; + +import com.djrapitops.plan.TaskSystem; +import com.djrapitops.plan.storage.file.PlanFiles; +import com.djrapitops.plan.utilities.logging.ErrorContext; +import com.djrapitops.plan.utilities.logging.ErrorLogger; +import net.playeranalytics.plugin.scheduling.RunnableFactory; +import net.playeranalytics.plugin.scheduling.TimeAmount; + +import javax.inject.Inject; +import javax.inject.Singleton; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.concurrent.TimeUnit; + +@Singleton +public class OldDependencyCacheDeletionTask extends TaskSystem.Task { + + private final File dependencyCache; + private final File libraries; + + private final ErrorLogger errorLogger; + + @Inject + public OldDependencyCacheDeletionTask( + PlanFiles files, + ErrorLogger errorLogger + ) { + dependencyCache = files.getDataDirectory().resolve("dependency_cache").toFile(); + libraries = files.getDataDirectory().resolve("libraries").toFile(); + this.errorLogger = errorLogger; + } + + @Override + public void register(RunnableFactory runnableFactory) { + long delay = TimeAmount.toTicks(10L, TimeUnit.SECONDS); + runnableFactory.create(this).runTaskLaterAsynchronously(delay); + } + + @Override + public void run() { + tryToDeleteDirectory(dependencyCache); + tryToDeleteDirectory(libraries); + } + + private void tryToDeleteDirectory(File directory) { + try { + if (directory.exists() && directory.isDirectory()) { + deleteDirectory(directory); + } + } catch (IOException e) { + errorLogger.error(e, ErrorContext.builder() + .whatToDo("Failed to delete '" + directory.getAbsolutePath() + "' - Delete it manually.") + .build()); + } + } + + private void deleteDirectory(File directory) throws IOException { + File[] files = directory.listFiles(); + if (files == null) return; + for (File file : files) { + if (file.isDirectory()) { + deleteDirectory(directory); + } else { + Files.delete(file.toPath()); + } + } + } +} diff --git a/Plan/nukkit/src/main/java/com/djrapitops/plan/modules/nukkit/NukkitTaskModule.java b/Plan/nukkit/src/main/java/com/djrapitops/plan/modules/nukkit/NukkitTaskModule.java index dfcdff7b2..eb6c8116a 100644 --- a/Plan/nukkit/src/main/java/com/djrapitops/plan/modules/nukkit/NukkitTaskModule.java +++ b/Plan/nukkit/src/main/java/com/djrapitops/plan/modules/nukkit/NukkitTaskModule.java @@ -28,6 +28,7 @@ import com.djrapitops.plan.gathering.timed.SystemUsageBuffer; import com.djrapitops.plan.settings.upkeep.ConfigStoreTask; import com.djrapitops.plan.storage.upkeep.DBCleanTask; import com.djrapitops.plan.storage.upkeep.LogsFolderCleanTask; +import com.djrapitops.plan.storage.upkeep.OldDependencyCacheDeletionTask; import dagger.Binds; import dagger.Module; import dagger.multibindings.IntoSet; @@ -78,4 +79,8 @@ public interface NukkitTaskModule { @Binds @IntoSet TaskSystem.Task bindShutdownDataPreservation(ShutdownDataPreservation dataPreservation); + + @Binds + @IntoSet + TaskSystem.Task bindOldDependencyCacheDeletion(OldDependencyCacheDeletionTask deletionTask); } diff --git a/Plan/sponge/src/main/java/com/djrapitops/plan/modules/sponge/SpongeTaskModule.java b/Plan/sponge/src/main/java/com/djrapitops/plan/modules/sponge/SpongeTaskModule.java index 4fbe926d6..0cf624f56 100644 --- a/Plan/sponge/src/main/java/com/djrapitops/plan/modules/sponge/SpongeTaskModule.java +++ b/Plan/sponge/src/main/java/com/djrapitops/plan/modules/sponge/SpongeTaskModule.java @@ -27,6 +27,7 @@ import com.djrapitops.plan.gathering.timed.SystemUsageBuffer; import com.djrapitops.plan.settings.upkeep.ConfigStoreTask; import com.djrapitops.plan.storage.upkeep.DBCleanTask; import com.djrapitops.plan.storage.upkeep.LogsFolderCleanTask; +import com.djrapitops.plan.storage.upkeep.OldDependencyCacheDeletionTask; import dagger.Binds; import dagger.Module; import dagger.multibindings.IntoSet; @@ -78,4 +79,8 @@ public interface SpongeTaskModule { @Binds @IntoSet TaskSystem.Task bindShutdownDataPreservation(ShutdownDataPreservation dataPreservation); + + @Binds + @IntoSet + TaskSystem.Task bindOldDependencyCacheDeletion(OldDependencyCacheDeletionTask deletionTask); } diff --git a/Plan/velocity/src/main/java/com/djrapitops/plan/modules/velocity/VelocityTaskModule.java b/Plan/velocity/src/main/java/com/djrapitops/plan/modules/velocity/VelocityTaskModule.java index 1bc5351f9..c0839c59d 100644 --- a/Plan/velocity/src/main/java/com/djrapitops/plan/modules/velocity/VelocityTaskModule.java +++ b/Plan/velocity/src/main/java/com/djrapitops/plan/modules/velocity/VelocityTaskModule.java @@ -25,6 +25,7 @@ import com.djrapitops.plan.gathering.timed.VelocityPingCounter; import com.djrapitops.plan.settings.upkeep.NetworkConfigStoreTask; import com.djrapitops.plan.storage.upkeep.DBCleanTask; import com.djrapitops.plan.storage.upkeep.LogsFolderCleanTask; +import com.djrapitops.plan.storage.upkeep.OldDependencyCacheDeletionTask; import dagger.Binds; import dagger.Module; import dagger.multibindings.IntoSet; @@ -67,4 +68,8 @@ public interface VelocityTaskModule { @Binds @IntoSet TaskSystem.Task bindJSONFileStorageCleanTask(JSONFileStorage.CleanTask cleanTask); + + @Binds + @IntoSet + TaskSystem.Task bindOldDependencyCacheDeletion(OldDependencyCacheDeletionTask deletionTask); }