Class TimeBasedDirectoryCleaner

java.lang.Object
org.kiwiproject.io.TimeBasedDirectoryCleaner
All Implemented Interfaces:
Runnable

public class TimeBasedDirectoryCleaner extends Object implements Runnable
A Runnable that cleans a given directory of files and/or directories that are older than a specified retention threshold.

NOTE: This class relies on classes in commons-io, so you will need to add it as a dependency to your project!

Implementation Note:
This is intended to be run in a single thread, e.g. using a ScheduledExecutorService with one thread. Results are undefined (and probably bad) if multiple threads execute the same instance of this class concurrently. Note also that accessing the delete error count, recent delete errors, etc. is thread-safe.
  • Constructor Details

    • TimeBasedDirectoryCleaner

      public TimeBasedDirectoryCleaner(String directoryPath, Duration retentionThreshold, String deleteErrorLogLevel)
      Create a new TimeBasedDirectoryCleaner instance.
      Parameters:
      directoryPath - the directory to be cleaned
      retentionThreshold - how long the directory contents should be retained before deletion
      deleteErrorLogLevel - the log level; should be a string corresponding to an SLF4J Level
      Implementation Note:
      No validation on whether the directoryPath points to a valid directory at this point; we assume it will exist at some point, e.g. if some other component creates it the first time it is written to, we don't want to throw exceptions from here.
  • Method Details

    • getDirectoryPath

      public String getDirectoryPath()
      Returns the absolute path of the directory being cleaned.
      Returns:
      directory path
    • getRetentionThreshold

      public Duration getRetentionThreshold()
      Returns the retention threshold as a Duration
      Returns:
      retention threshold
    • getDeleteCount

      public long getDeleteCount()
      Returns the total number of deletes this instance has counted.
      Returns:
      the total delete count
      Implementation Note:
      A directory that is deleted is counted as ONE deletion, regardless of how many files were inside.
    • getDeleteErrorCount

      public int getDeleteErrorCount()
      Returns the total number of delete errors this instance has counted.
      Returns:
      the total delete error count
    • getNumberOfRecentDeleteErrors

      public int getNumberOfRecentDeleteErrors()
      Returns the number of recent delete errors currently stored in memory.
      Returns:
      total number of recent delete errors
    • capacityOfRecentDeleteErrors

      public static int capacityOfRecentDeleteErrors()
      Returns the maximum number of delete failures that can be stored in memory.
      Returns:
      number of delete errors that will be stored in memory
    • clearRecentDeleteErrors

      public void clearRecentDeleteErrors()
      Clears all delete errors currently stored in memory.
    • getRecentDeleteErrors

      public List<TimeBasedDirectoryCleaner.DeleteError> getRecentDeleteErrors()
      Returns all the recent delete failures stored in memory.
      Returns:
      a list of recent DeleteError objects
      Implementation Note:
      Per the docs for Queues.synchronizedQueue(Queue), we MUST synchronize when iterating, and creating a new ArrayList has to iterate the constructor argument's contents in some fashion.
    • run

      public void run()
      Specified by:
      run in interface Runnable
    • builder

    • getRetentionThresholdDescription

      public String getRetentionThresholdDescription()