Skip to content

Commit 3d874e9

Browse files
committed
wait until the next run to remove the directory if it gets emptied
1 parent 1f8cd55 commit 3d874e9

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

src/main/java/com/lowtuna/jsonblob/core/BlobCleanupConsumer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ public class BlobCleanupConsumer implements Runnable {
2828

2929
@Override
3030
public void run() {
31-
log.info("Polling queue for files to process for {}", QUEUE_TIMEOUT);
31+
log.debug("Polling queue for files to process for {}", QUEUE_TIMEOUT);
3232
try {
3333
File file = filesToProcess.poll(QUEUE_TIMEOUT.getQuantity(), QUEUE_TIMEOUT.getUnit());
3434
if (file == null) {
35-
log.info("Timed out after {} while waiting for something to come onto the queue", QUEUE_TIMEOUT);
35+
log.debug("Timed out after {} while waiting for something to come onto the queue", QUEUE_TIMEOUT);
3636
return;
3737
}
3838
log.debug("Processing {}", file.getAbsolutePath());

src/main/java/com/lowtuna/jsonblob/core/BlobCleanupProducer.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.Collection;
1616
import java.util.concurrent.BlockingQueue;
1717
import java.util.concurrent.TimeUnit;
18+
import java.util.concurrent.atomic.AtomicInteger;
1819

1920
/**
2021
* Created by tburch on 8/18/17.
@@ -41,8 +42,10 @@ protected boolean handleDirectory(File directory, int depth, Collection<Void> re
4142
boolean process = localDate.isBefore(LocalDate.now().minusDays(blobAccessTtl.toDays()));
4243
if (process) {
4344
log.info("Processing {} blobs for un-accessed blobs", directory.getAbsolutePath());
45+
AtomicInteger fileCount = new AtomicInteger(0);
4446
Files.newDirectoryStream(directory.toPath())
4547
.forEach(path -> {
48+
fileCount.incrementAndGet();
4649
File file = path.toFile();
4750
if (file.getName().startsWith(FileSystemJsonBlobManager.BLOB_METADATA_FILE_NAME)) {
4851
return;
@@ -53,27 +56,26 @@ protected boolean handleDirectory(File directory, int depth, Collection<Void> re
5356
log.warn("Interrupted while trying to add file to be processed at {}", file.getAbsolutePath(), e);
5457
}
5558
});
56-
}
5759

58-
File[] files = directory.listFiles();
59-
if (files != null && files.length == 1) {
60-
if (files[0].getName().startsWith(FileSystemJsonBlobManager.BLOB_METADATA_FILE_NAME)) {
61-
if (directory.delete()) {
62-
log.info("{} has only a metadata file, so it's being deleted", directory.getAbsolutePath());
60+
if (fileCount.get() == 0) {
61+
log.info("{} has no files, so it's being deleted", directory.getAbsolutePath());
62+
} else if (fileCount.get() == 1) {
63+
File[] files = directory.listFiles();
64+
if (files != null && files[0].getName().startsWith(FileSystemJsonBlobManager.BLOB_METADATA_FILE_NAME)) {
65+
if (directory.delete()) {
66+
log.info("{} has only a metadata file, so it's being deleted", directory.getAbsolutePath());
67+
}
6368
}
6469
}
65-
} else if (files.length == 0) {
66-
log.info("{} has no files, so it's being deleted", directory.getAbsolutePath());
70+
return false;
6771
}
68-
return false;
6972
} else {
7073
File[] files = directory.listFiles();
7174
if (files != null && files.length == 0) {
7275
if (directory.delete()) log.info("{} has no files, so it's being deleted", directory.getAbsolutePath());
7376
return false;
7477
}
7578
}
76-
7779
return true;
7880
}
7981

0 commit comments

Comments
 (0)