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("Created temporary directory: {}", tempDir);
} }
log.info("Temporary file configuration initialized"); log.debug("Temporary file configuration initialized");
log.info("Using temp directory: {}", customTempDirectory); log.debug("Using temp directory: {}", customTempDirectory);
log.info("Temp file prefix: {}", tempFiles.getPrefix()); log.debug("Temp file prefix: {}", tempFiles.getPrefix());
} catch (Exception e) { } catch (Exception e) {
log.error("Failed to initialize temporary file configuration", e); log.error("Failed to initialize temporary file configuration", e);
} }

View File

@ -302,53 +302,37 @@ public class TempFileCleanupService {
Consumer<Path> onDeleteCallback) Consumer<Path> onDeleteCallback)
throws IOException { throws IOException {
// Check recursion depth limit
if (depth > MAX_RECURSION_DEPTH) { if (depth > MAX_RECURSION_DEPTH) {
log.warn("Maximum directory recursion depth reached for: {}", directory); log.debug("Maximum directory recursion depth reached for: {}", directory);
return; 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)) { try (Stream<Path> pathStream = Files.list(directory)) {
// Process files in a streaming fashion instead of materializing the whole list
pathStream.forEach( pathStream.forEach(
path -> { path -> {
try { try {
String fileName = path.getFileName().toString(); String fileName = path.getFileName().toString();
// Skip if file should be excluded
if (SHOULD_SKIP.test(fileName)) { if (SHOULD_SKIP.test(fileName)) {
return; return;
} }
// Handle directories recursively
if (Files.isDirectory(path)) { if (Files.isDirectory(path)) {
try { subdirectories.add(path);
cleanupDirectoryStreaming(
path,
containerMode,
depth + 1,
maxAgeMillis,
isScheduled,
onDeleteCallback);
} catch (IOException e) {
log.warn("Error processing subdirectory: {}", path, e);
}
return; return;
} }
// Skip registered files - these are handled by TempFileManager
if (registry.contains(path.toFile())) { if (registry.contains(path.toFile())) {
return; return;
} }
// Check if this file should be deleted
if (shouldDeleteFile(path, fileName, containerMode, maxAgeMillis)) { if (shouldDeleteFile(path, fileName, containerMode, maxAgeMillis)) {
try { try {
Files.deleteIfExists(path); Files.deleteIfExists(path);
onDeleteCallback.accept(path); onDeleteCallback.accept(path);
} catch (IOException e) { } catch (IOException e) {
// Handle locked files more gracefully
if (e.getMessage() != null if (e.getMessage() != null
&& e.getMessage() && e.getMessage()
.contains("being used by another process")) { .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. */ /** 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.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.time.Instant; import java.time.Instant;
import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentSkipListSet; import java.util.concurrent.ConcurrentSkipListSet;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -22,14 +24,11 @@ import lombok.extern.slf4j.Slf4j;
@Component @Component
public class TempFileRegistry { public class TempFileRegistry {
// Track temp files with creation timestamps private final ConcurrentMap<Path, Instant> registeredFiles = new ConcurrentHashMap<>();
private final Map<Path, Instant> registeredFiles = new ConcurrentHashMap<>(); private final Set<Path> thirdPartyTempFiles =
Collections.newSetFromMap(new ConcurrentHashMap<>());
// Separately track third-party temp files that need special handling private final Set<Path> tempDirectories =
private final Set<Path> thirdPartyTempFiles = new ConcurrentSkipListSet<>(); Collections.newSetFromMap(new ConcurrentHashMap<>());
// Track temp directories
private final Set<Path> tempDirectories = new ConcurrentSkipListSet<>();
/** /**
* Register a temporary file with the registry. * Register a temporary file with the registry.