Skip to content

Instantly share code, notes, and snippets.

@pbriggs28
Last active May 31, 2018 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pbriggs28/d70af2d8bb6655fee2742231f7627906 to your computer and use it in GitHub Desktop.
Save pbriggs28/d70af2d8bb6655fee2742231f7627906 to your computer and use it in GitHub Desktop.
package com.team2073.common.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.*;
public class LogFileCleaner {
private final static Logger logger = LoggerFactory.getLogger(LogFileCleaner.class);
// static SortedSet<File> listOfFilesYoungerThanTwoWeeks = new TreeSet<File>();
static List<File> listOfFilesYoungerThanTwoWeeks = new ArrayList<File>();
static long maxSizeAllowedInFolder;
static int howMuchDeleted = 0;
static File baseDir;
static boolean iteratedOverDeleteRecursivelyMethodAlready = false;
public static void deleteFilesRecursivelyOverDirs(String directoryPath, long maxSize) {
if (iteratedOverDeleteRecursivelyMethodAlready == false) {
File baseDirectory = new File(directoryPath);
baseDir = baseDirectory;
iteratedOverDeleteRecursivelyMethodAlready = true;
// TODO: maxSizeAllowedInFolder is always 0 here
deleteFilesRecursivelyOverDirs(directoryPath, maxSizeAllowedInFolder);
} else {
File baseDirectory = new File(directoryPath);
maxSizeAllowedInFolder = maxSize;
//TODO: Sort these by dateLastModified before iterating them. Currently you are grabbing files in a random order
File[] listingSubDirs = baseDirectory.listFiles();
long TWO_WEEKS = 1209600000;
if (baseDirectory.exists()) {
for (File subDir : listingSubDirs) {
// TODO: Don't need this line, subDir is the same before and after
subDir = new File(baseDirectory, subDir.getName());
if (subDir.isFile()) {
delete(subDir);
} else {// If sub part of base is not file, but directory.
// TODO: This is where you need to recursively call deleteFilesRecursivelyOverDirs on this directory
String nameOfSubDir = subDir.getPath();
logger.info("[{}] is not a file, but a directory.", nameOfSubDir);
}
}
// LANA: At the end, print how many files and how much space you cleared, in mb
} else {
logger.info("This directory [{}] does not exist.", baseDirectory);
}
}
}
private static void delete(File file) {
Date dt = new Date(System.currentTimeMillis());
if (beDeleted(file)) {
logger.info("Deleting file [{}], which is [{}] kb. The date is [{}].", file, file.length() / 1000, dt);
file.delete();
// TODO: Use howMuchDeleted++ instead
howMuchDeleted = howMuchDeleted + 1;
logger.info("Deleted [{}] files.", howMuchDeleted);
} else {
logger.info("[{}] does not have to be deleted.", file);
System.out.println(baseDir);
}
}
// LANA: Change this method to only check if a file is past the
// allowed date last modified and return a boolean. Use this return to delete
// the
// file in the method that calls this
private static boolean beDeleted(File fileBeingDeleted) {
long lastModified = fileBeingDeleted.lastModified();
long difference = System.currentTimeMillis() - lastModified;
File parentDir = baseDir.getParentFile();
if (difference > 50 * 24 * 60 * 60 * 1000) {
return true;
// TODO: Read the JavaDocs on parentDir.length(), that's where your main problem is
} else if (parentDir.length() / 1000 > maxSizeAllowedInFolder) {
logger.info("The base dir [{}] is bigger than max specified amount: [{}].", parentDir.getPath(),
maxSizeAllowedInFolder);
listOfFilesYoungerThanTwoWeeks.add(fileBeingDeleted);
sortingAndDeletingFilesLessThanThreshHold();
return false;
} else {
return false;
}
}
public static void sortingAndDeletingFilesLessThanThreshHold() {
int indexIncrement = 0;
int howMuchDeletedToLimitSpace = 0;
// Collections.sort(listOfFilesYoungerThanTwoWeeks, new
// DateLastModifiedComparator());
// Comparator<File> fileComparator = (File a, File b) ->
// Long.compare(a.lastModified(), b.lastModified());
listOfFilesYoungerThanTwoWeeks.stream().sorted(new DateLastModifiedComparator())
.forEach(e -> System.out.println(e));
while(baseDir.getParentFile().length()/1000 > maxSizeAllowedInFolder) {
File getRidOf = listOfFilesYoungerThanTwoWeeks.get(0);
listOfFilesYoungerThanTwoWeeks.remove(0);
System.out.println(baseDir.getParentFile().length()/1000);
getRidOf.delete();
howMuchDeletedToLimitSpace = howMuchDeletedToLimitSpace + 1;
}
logger.info("Deleted [{}] files to maintain specified directory space.",
howMuchDeletedToLimitSpace );
}
}
class DateLastModifiedComparator implements Comparator<File> {
@Override
public int compare(File o1, File o2) {
// TODO: Delete this TODO
// TODO Auto-generated method stub
if (o1.lastModified() > o2.lastModified()) {
return 1;
}
if (o2.lastModified() > o1.lastModified()) {
return -1;
} else {
return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment