diff --git a/src/main/java/com/github/rishabh9/kumoru/snapshots/SnapshotUpdateChecker.java b/src/main/java/com/github/rishabh9/kumoru/snapshots/SnapshotUpdateChecker.java index 4e2cb59..c3efe26 100644 --- a/src/main/java/com/github/rishabh9/kumoru/snapshots/SnapshotUpdateChecker.java +++ b/src/main/java/com/github/rishabh9/kumoru/snapshots/SnapshotUpdateChecker.java @@ -8,6 +8,8 @@ import com.github.rishabh9.kumoru.snapshots.parser.MetadataAsyncXmlParser; import com.github.rishabh9.kumoru.snapshots.parser.SnapshotMetadata; import io.vertx.core.AbstractVerticle; +import io.vertx.core.CompositeFuture; +import io.vertx.core.Future; import io.vertx.core.Promise; import io.vertx.core.buffer.Buffer; import io.vertx.core.eventbus.DeliveryOptions; @@ -22,6 +24,7 @@ import java.util.List; import java.util.Optional; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; import javax.xml.stream.XMLStreamException; import lombok.extern.log4j.Log4j2; @@ -38,10 +41,10 @@ public void start(final Promise startPromise) { log.debug("Starting snapshot updater"); webClient = KumoruCommon.createWebClient(vertx); vertx.eventBus().registerDefaultCodec(UpdateMessage.class, new UpdateMessageCodec()); - final int interval = 12; + final int interval = 60; timerId = vertx.setPeriodic( - TimeUnit.HOURS.toMillis(interval), + TimeUnit.SECONDS.toMillis(interval), id -> { final ZonedDateTime now = ZonedDateTime.now(); log.debug("Snapshot update checker started..."); @@ -110,7 +113,15 @@ private void nextMirror(final Iterator iterator, final String fileOrDire final String metadataXmlFileSystemPath = fileOrDirectory + METADATA_XML; if (isSnapshotUpdated(downloadedFile, metadataXmlFileSystemPath)) { log.debug("New snapshot available from {}", uri); - updateSnapshot(metadataXmlFileSystemPath, snapshotPath, mirror, result.body()); + deleteOldSnapshots(fileOrDirectory) + .onComplete( + asyncResult -> + // Update after attempting to delete is complete + updateSnapshot( + metadataXmlFileSystemPath, + snapshotPath, + mirror, + result.body())); } } else { log.debug("Unable to retrieve metadata from {}", uri, asyncWebResult.cause()); @@ -120,6 +131,49 @@ private void nextMirror(final Iterator iterator, final String fileOrDire } } + private Future deleteOldSnapshots(final String directory) { + log.debug("Deleting old snapshots from {}", directory); + final Promise promise = Promise.promise(); + vertx + .fileSystem() + .readDir( + directory, + readResult -> { + if (readResult.succeeded()) { + CompositeFuture.all( + readResult.result().stream().map(this::delete).collect(Collectors.toList())) + .onSuccess(success -> promise.complete()) + .onFailure( + failure -> { + log.error("Error deleting all files", failure.getCause()); + promise.fail(failure.getCause()); + }); + } else { + log.error("Error listing files", readResult.cause()); + promise.fail(readResult.cause()); + } + }); + return promise.future(); + } + + private Future delete(final String fileOrDirectory) { + log.debug("Deleting {}", fileOrDirectory); + final Promise promise = Promise.promise(); + vertx + .fileSystem() + .deleteRecursive( + fileOrDirectory, + true, + deleteResult -> { + if (deleteResult.failed()) { + log.error("Unable to delete {}", fileOrDirectory, deleteResult.cause()); + promise.fail(deleteResult.cause()); + } + promise.complete(); + }); + return promise.future(); + } + private void updateSnapshot( final String metadataXmlFileSystemPath, final String snapshotPath,