Package org.kiwiproject.io
Class TimeBasedDirectoryCleaner
java.lang.Object
org.kiwiproject.io.TimeBasedDirectoryCleaner
- All Implemented Interfaces:
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
ScheduledExecutorServicewith 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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classValue class representing a file delete error.static class -
Constructor Summary
ConstructorsConstructorDescriptionTimeBasedDirectoryCleaner(String directoryPath, Duration retentionThreshold, String deleteErrorLogLevel) Create a new TimeBasedDirectoryCleaner instance. -
Method Summary
Modifier and TypeMethodDescriptionbuilder()static intReturns the maximum number of delete failures that can be stored in memory.voidClears all delete errors currently stored in memory.longReturns the total number of deletes this instance has counted.intReturns the total number of delete errors this instance has counted.Returns the absolute path of the directory being cleaned.intReturns the number of recent delete errors currently stored in memory.Returns all the recent delete failures stored in memory.Returns the retention threshold as aDurationvoidrun()
-
Constructor Details
-
TimeBasedDirectoryCleaner
public TimeBasedDirectoryCleaner(String directoryPath, Duration retentionThreshold, String deleteErrorLogLevel) Create a new TimeBasedDirectoryCleaner instance.- Parameters:
directoryPath- the directory to be cleanedretentionThreshold- how long the directory contents should be retained before deletiondeleteErrorLogLevel- the log level; should be a string corresponding to an SLF4JLevel- 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
Returns the absolute path of the directory being cleaned.- Returns:
- directory path
-
getRetentionThreshold
Returns the retention threshold as aDuration- 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
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() -
builder
-
getRetentionThresholdDescription
-