mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-01-24 16:14:26 +08:00
Delete old dependency cache directories
This commit is contained in:
parent
6643e442c1
commit
6f73af4719
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user