final changes

This commit is contained in:
Anthony Stirling 2025-06-25 12:49:42 +01:00
parent 3eda85d00e
commit 9a29c00dfc
3 changed files with 28 additions and 31 deletions

View File

@ -49,9 +49,9 @@ public class TempFileConfiguration {
log.info("Created temporary directory: {}", tempDir);
}
log.info("Temporary file configuration initialized");
log.info("Using temp directory: {}", customTempDirectory);
log.info("Temp file prefix: {}", tempFiles.getPrefix());
log.debug("Temporary file configuration initialized");
log.debug("Using temp directory: {}", customTempDirectory);
log.debug("Temp file prefix: {}", tempFiles.getPrefix());
} catch (Exception e) {
log.error("Failed to initialize temporary file configuration", e);
}

View File

@ -302,53 +302,37 @@ public class TempFileCleanupService {
Consumer<Path> onDeleteCallback)
throws IOException {
// Check recursion depth limit
if (depth > MAX_RECURSION_DEPTH) {
log.warn("Maximum directory recursion depth reached for: {}", directory);
log.debug("Maximum directory recursion depth reached for: {}", directory);
return;
}
// Use try-with-resources to ensure the stream is closed
java.util.List<Path> subdirectories = new java.util.ArrayList<>();
try (Stream<Path> pathStream = Files.list(directory)) {
// Process files in a streaming fashion instead of materializing the whole list
pathStream.forEach(
path -> {
try {
String fileName = path.getFileName().toString();
// Skip if file should be excluded
if (SHOULD_SKIP.test(fileName)) {
return;
}
// Handle directories recursively
if (Files.isDirectory(path)) {
try {
cleanupDirectoryStreaming(
path,
containerMode,
depth + 1,
maxAgeMillis,
isScheduled,
onDeleteCallback);
} catch (IOException e) {
log.warn("Error processing subdirectory: {}", path, e);
}
subdirectories.add(path);
return;
}
// Skip registered files - these are handled by TempFileManager
if (registry.contains(path.toFile())) {
return;
}
// Check if this file should be deleted
if (shouldDeleteFile(path, fileName, containerMode, maxAgeMillis)) {
try {
Files.deleteIfExists(path);
onDeleteCallback.accept(path);
} catch (IOException e) {
// Handle locked files more gracefully
if (e.getMessage() != null
&& e.getMessage()
.contains("being used by another process")) {
@ -363,6 +347,20 @@ public class TempFileCleanupService {
}
});
}
for (Path subdirectory : subdirectories) {
try {
cleanupDirectoryStreaming(
subdirectory,
containerMode,
depth + 1,
maxAgeMillis,
isScheduled,
onDeleteCallback);
} catch (IOException e) {
log.warn("Error processing subdirectory: {}", subdirectory, e);
}
}
}
/** Determine if a file should be deleted based on its name, age, and other criteria. */

View File

@ -4,9 +4,11 @@ import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.stream.Collectors;
@ -22,14 +24,11 @@ import lombok.extern.slf4j.Slf4j;
@Component
public class TempFileRegistry {
// Track temp files with creation timestamps
private final Map<Path, Instant> registeredFiles = new ConcurrentHashMap<>();
// Separately track third-party temp files that need special handling
private final Set<Path> thirdPartyTempFiles = new ConcurrentSkipListSet<>();
// Track temp directories
private final Set<Path> tempDirectories = new ConcurrentSkipListSet<>();
private final ConcurrentMap<Path, Instant> registeredFiles = new ConcurrentHashMap<>();
private final Set<Path> thirdPartyTempFiles =
Collections.newSetFromMap(new ConcurrentHashMap<>());
private final Set<Path> tempDirectories =
Collections.newSetFromMap(new ConcurrentHashMap<>());
/**
* Register a temporary file with the registry.