Package org.kiwiproject.io
Class TimeBasedDirectoryCleaner
- java.lang.Object
-
- org.kiwiproject.io.TimeBasedDirectoryCleaner
-
- All Implemented Interfaces:
Runnable
@NotThreadSafe public class TimeBasedDirectoryCleaner extends Object implements Runnable
ARunnablethat 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 Classes Modifier and Type Class Description static classTimeBasedDirectoryCleaner.DeleteErrorValue class representing a file delete error.
-
Constructor Summary
Constructors Constructor Description TimeBasedDirectoryCleaner(String directoryPath, Duration retentionThreshold, String deleteErrorLogLevel)Create a new TimeBasedDirectoryCleaner instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static intcapacityOfRecentDeleteErrors()Returns the maximum number of delete failures that can be stored in memory.voidclearRecentDeleteErrors()Clears all delete errors currently stored in memory.longgetDeleteCount()Returns the total number of deletes this instance has counted.intgetDeleteErrorCount()Returns the total number of delete errors this instance has counted.StringgetDirectoryPath()Returns the absolute path of the directory being cleaned.intgetNumberOfRecentDeleteErrors()Returns the number of recent delete errors currently stored in memory.List<TimeBasedDirectoryCleaner.DeleteError>getRecentDeleteErrors()Returns all the recent delete failures stored in memory.DurationgetRetentionThreshold()Returns the retention threshold as aDurationvoidrun()
-
-
-
Constructor Detail
-
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 Detail
-
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 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
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.
-
-