Search in sources :

Example 1 with CompressionType

use of com.hubspot.singularity.runner.base.shared.CompressionType in project Singularity by HubSpot.

the class SingularityExecutorCleanup method checkForUncompressedLogrotatedFile.

private void checkForUncompressedLogrotatedFile(SingularityExecutorTaskDefinition taskDefinition) {
    final Iterator<Path> iterator = getUncompressedLogrotatedFileIterator(taskDefinition);
    final Set<String> emptyPaths = new HashSet<>();
    final List<Path> uncompressedFiles = new ArrayList<>();
    while (iterator.hasNext()) {
        Path path = iterator.next();
        final String fileName = Objects.toString(path.getFileName());
        Optional<CompressionType> maybeCompressionType = getFileCompressionType(fileName);
        if (maybeCompressionType.isPresent()) {
            try {
                if (Files.size(path) == 0) {
                    Files.deleteIfExists(path);
                    emptyPaths.add(fileName.substring(0, fileName.length() - maybeCompressionType.get().getExtention().length()));
                }
            } catch (IOException ioe) {
                LOG.error("Failed to handle empty {} file {}", maybeCompressionType.get(), path, ioe);
                exceptionNotifier.notify(String.format("Error handling empty file (%s)", ioe.getMessage()), ioe, ImmutableMap.of("file", path.toString()));
            }
        } else {
            uncompressedFiles.add(path);
        }
    }
    for (Path path : uncompressedFiles) {
        if (emptyPaths.contains(Objects.toString(path.getFileName()))) {
            LOG.info("Compressing abandoned file {}", path);
            try {
                new SimpleProcessManager(LOG).runCommand(ImmutableList.<String>of(cleanupConfiguration.getCompressionType().getCommand(), path.toString()));
            } catch (InterruptedException | ProcessFailedException e) {
                LOG.error("Failed to {} {}", cleanupConfiguration.getCompressionType(), path, e);
                exceptionNotifier.notify(String.format("Failed to gzip (%s)", e.getMessage()), e, ImmutableMap.of("file", path.toString()));
            }
        } else {
            LOG.debug("Didn't find matched empty {} file for {}", cleanupConfiguration.getCompressionType(), path);
        }
    }
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SimpleProcessManager(com.hubspot.singularity.runner.base.shared.SimpleProcessManager) ProcessFailedException(com.hubspot.singularity.runner.base.shared.ProcessFailedException) CompressionType(com.hubspot.singularity.runner.base.shared.CompressionType) HashSet(java.util.HashSet)

Aggregations

CompressionType (com.hubspot.singularity.runner.base.shared.CompressionType)1 ProcessFailedException (com.hubspot.singularity.runner.base.shared.ProcessFailedException)1 SimpleProcessManager (com.hubspot.singularity.runner.base.shared.SimpleProcessManager)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1