Search in sources :

Example 1 with DirectoryCleaner

use of com.facebook.buck.util.DirectoryCleaner in project buck by facebook.

the class LogFileHandler method newCleaner.

public static DirectoryCleaner newCleaner(long maxLogsSizeBytes, int maxLogsDirectories, int minAmountOfLogsToKeep) {
    DirectoryCleanerArgs cleanerArgs = DirectoryCleanerArgs.builder().setPathSelector(new DirectoryCleaner.PathSelector() {

        @Override
        public Iterable<Path> getCandidatesToDelete(Path rootPath) throws IOException {
            List<Path> dirPaths = Lists.newArrayList();
            try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(rootPath)) {
                for (Path path : directoryStream) {
                    Matcher matcher = DIR_PATTERN.matcher(path.getFileName().toString());
                    if (matcher.matches()) {
                        dirPaths.add(path);
                    }
                }
            }
            return dirPaths;
        }

        @Override
        public int comparePaths(DirectoryCleaner.PathStats path1, DirectoryCleaner.PathStats path2) {
            return Long.compare(path1.getCreationMillis(), path2.getCreationMillis());
        }
    }).setMaxTotalSizeBytes(maxLogsSizeBytes).setMaxPathCount(maxLogsDirectories).setMinAmountOfEntriesToKeep(minAmountOfLogsToKeep).build();
    DirectoryCleaner cleaner = new DirectoryCleaner(cleanerArgs);
    return cleaner;
}
Also used : Path(java.nio.file.Path) Matcher(java.util.regex.Matcher) DirectoryCleanerArgs(com.facebook.buck.util.DirectoryCleanerArgs) DirectoryStream(java.nio.file.DirectoryStream) DirectoryCleaner(com.facebook.buck.util.DirectoryCleaner) List(java.util.List) IOException(java.io.IOException)

Aggregations

DirectoryCleaner (com.facebook.buck.util.DirectoryCleaner)1 DirectoryCleanerArgs (com.facebook.buck.util.DirectoryCleanerArgs)1 IOException (java.io.IOException)1 DirectoryStream (java.nio.file.DirectoryStream)1 Path (java.nio.file.Path)1 List (java.util.List)1 Matcher (java.util.regex.Matcher)1