further cleanups

This commit is contained in:
Anthony Stirling 2025-07-21 13:06:00 +01:00
parent 82900b9db1
commit fe4a452a54
4 changed files with 61 additions and 12 deletions

View File

@ -26,17 +26,47 @@ public class CleanupAsyncConfig {
// Set custom rejection handler to log when queue is full
exec.setRejectedExecutionHandler(new RejectedExecutionHandler() {
private volatile long lastRejectionTime = 0;
private volatile int rejectionCount = 0;
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
log.warn("Cleanup task rejected - queue full! Active: {}, Queue size: {}, Pool size: {}",
executor.getActiveCount(),
executor.getQueue().size(),
executor.getPoolSize());
long currentTime = System.currentTimeMillis();
rejectionCount++;
// Use caller-runs policy as fallback - this will block the scheduler thread
// but ensures the cleanup still happens
log.warn("Executing cleanup task on scheduler thread as fallback");
r.run();
// Rate-limit logging to avoid spam
if (currentTime - lastRejectionTime > 60000) { // Log at most once per minute
log.warn("Cleanup task rejected #{} - queue full! Active: {}, Queue size: {}, Pool size: {}",
rejectionCount,
executor.getActiveCount(),
executor.getQueue().size(),
executor.getPoolSize());
lastRejectionTime = currentTime;
}
// Try to discard oldest task and add this one
if (executor.getQueue().poll() != null) {
log.debug("Discarded oldest queued cleanup task to make room");
try {
executor.execute(r);
return;
} catch (Exception e) {
// If still rejected, fall back to caller-runs
}
}
// Last resort: caller-runs with timeout protection
log.warn("Executing cleanup task #{} on scheduler thread as last resort", rejectionCount);
long startTime = System.currentTimeMillis();
try {
r.run();
long duration = System.currentTimeMillis() - startTime;
if (duration > 30000) { // Warn if cleanup blocks scheduler for >30s
log.warn("Cleanup task on scheduler thread took {}ms - consider tuning", duration);
}
} catch (Exception e) {
log.error("Cleanup task failed on scheduler thread", e);
}
}
});

View File

@ -328,8 +328,8 @@ public class ApplicationProperties {
private long cleanupIntervalMinutes = 30;
private boolean startupCleanup = true;
private boolean cleanupSystemTemp = false;
private int batchSize = 0;
private long pauseBetweenBatchesMs = 0;
private int batchSize = 1000;
private long pauseBetweenBatchesMs = 50;
public String getBaseTmpDir() {
return baseTmpDir != null && !baseTmpDir.isEmpty()

View File

@ -50,6 +50,9 @@ public class TempFileCleanupService {
// Maximum recursion depth for directory traversal
private static final int MAX_RECURSION_DEPTH = 5;
// Maximum consecutive failures before aborting batch cleanup
private static final int MAX_CONSECUTIVE_FAILURES = 10;
// Cleanup state management
private final AtomicBoolean cleanupRunning = new AtomicBoolean(false);
private final AtomicLong lastCleanupDuration = new AtomicLong(0);
@ -371,6 +374,7 @@ public class TempFileCleanupService {
.getTempFileManagement()
.getPauseBetweenBatchesMs();
int processed = 0;
int consecutiveFailures = 0;
try (java.nio.file.DirectoryStream<Path> stream = Files.newDirectoryStream(directory)) {
for (Path path : stream) {
@ -394,17 +398,32 @@ public class TempFileCleanupService {
try {
Files.deleteIfExists(path);
onDeleteCallback.accept(path);
consecutiveFailures = 0; // Reset failure count on success
} catch (IOException e) {
consecutiveFailures++;
if (e.getMessage() != null
&& e.getMessage().contains("being used by another process")) {
log.debug("File locked, skipping delete: {}", path);
} else {
log.warn("Failed to delete temp file: {}", path, e);
}
if (consecutiveFailures >= MAX_CONSECUTIVE_FAILURES) {
log.error("Aborting directory cleanup after {} consecutive failures in: {}",
consecutiveFailures, directory);
return; // Early exit from cleanup
}
}
}
} catch (Exception e) {
consecutiveFailures++;
log.warn("Error processing path: {}", path, e);
if (consecutiveFailures >= MAX_CONSECUTIVE_FAILURES) {
log.error("Aborting directory cleanup after {} consecutive failures in: {}",
consecutiveFailures, directory);
return; // Early exit from cleanup
}
}
processed++;

View File

@ -134,8 +134,8 @@ system:
cleanupIntervalMinutes: 30 # How often to run cleanup (in minutes)
startupCleanup: true # Clean up old temp files on startup
cleanupSystemTemp: false # Whether to clean broader system temp directory
batchSize: 0 # Number of entries processed before optional pause (0 = unlimited)
pauseBetweenBatchesMs: 0 # Pause duration in milliseconds between batches
batchSize: 1000 # Number of entries processed before optional pause (0 = unlimited)
pauseBetweenBatchesMs: 50 # Pause duration in milliseconds between batches
ui:
appName: '' # application's visible name