Skip to content

Commit

Permalink
Merge branch 'release/0.5.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabh9 committed Jun 10, 2020
2 parents bace2b2 + 7567d10 commit 81eff62
Showing 1 changed file with 57 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -38,10 +41,10 @@ public void start(final Promise<Void> 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...");
Expand Down Expand Up @@ -110,7 +113,15 @@ private void nextMirror(final Iterator<String> 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());
Expand All @@ -120,6 +131,49 @@ private void nextMirror(final Iterator<String> iterator, final String fileOrDire
}
}

private Future<Void> deleteOldSnapshots(final String directory) {
log.debug("Deleting old snapshots from {}", directory);
final Promise<Void> 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<Void> delete(final String fileOrDirectory) {
log.debug("Deleting {}", fileOrDirectory);
final Promise<Void> 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,
Expand Down

0 comments on commit 81eff62

Please sign in to comment.