Skip to content
This repository has been archived by the owner on Jul 5, 2021. It is now read-only.

Commit

Permalink
Print progress every second
Browse files Browse the repository at this point in the history
The ArchiveTransferManager now uses a ProgressTracker and the
transferred and total bytes are printed every second. This could be
improved with a configurable interval in the configuration file and a
percentage instead of raw bytes. Or maybe both if configurable.
  • Loading branch information
MoriTanosuke committed Oct 28, 2017
1 parent 65e0ad0 commit dd89bc9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
* #L%
*/

import java.io.File;
import java.io.IOException;
import java.util.List;

import com.amazonaws.event.ProgressTracker;
import com.amazonaws.services.glacier.AmazonGlacier;
import com.amazonaws.services.glacier.transfer.ArchiveTransferManager;
import com.amazonaws.services.glacier.transfer.ArchiveTransferManagerBuilder;
Expand All @@ -34,6 +31,11 @@
import de.kopis.glacier.parsers.GlacierUploaderOptionParser;
import joptsimple.OptionSet;

import java.io.File;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class UploadArchiveCommand extends AbstractCommand {

private final ArchiveTransferManager atm;
Expand All @@ -53,12 +55,20 @@ public UploadArchiveCommand(final AmazonGlacier client, final AmazonSQS sqs, fin

public void upload(final String vaultName, final File uploadFile) {
log.info("Starting to upload {} to vault {}...", uploadFile, vaultName);
try {
final String archiveId = atm.upload(vaultName, uploadFile.getName(), uploadFile).getArchiveId();
log.info("Uploaded archive {}", archiveId);
} catch (final IOException e) {
log.error("Something went wrong while uploading " + uploadFile + " to vault " + vaultName + ".", e);
}
final String sameAccountId = "-";
final ProgressTracker progressTracker = new ProgressTracker();
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
log.info("{}/{}) transferred",
progressTracker.getProgress().getRequestBytesTransferred(),
progressTracker.getProgress().getRequestContentLength()
);
}
},1000, 1000, TimeUnit.MILLISECONDS);
final String archiveId = atm.upload(sameAccountId, vaultName, uploadFile.getName(), uploadFile,
progressTracker).getArchiveId();
log.info("Uploaded archive {}", archiveId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,19 @@
* #L%
*/

import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import com.amazonaws.event.ProgressListener;
import com.amazonaws.services.glacier.transfer.ArchiveTransferManager;
import com.amazonaws.services.glacier.transfer.UploadResult;
import joptsimple.OptionSet;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import org.junit.Test;
import com.amazonaws.services.glacier.transfer.ArchiveTransferManager;
import com.amazonaws.services.glacier.transfer.UploadResult;
import joptsimple.OptionSet;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class UploadArchiveCommandTest extends AbstractCommandTest {
@Test
Expand All @@ -46,7 +43,10 @@ public void canUploadFile() throws Exception {
final File file = writeTemporaryFile(content);

final ArchiveTransferManager atm = createMock(ArchiveTransferManager.class);
expect(atm.upload(eq("vaultName"), eq(file.getName()), eq(file))).andReturn(new UploadResult(UUID.randomUUID().toString())).times(1);
expect(atm.upload(eq("-"), eq("vaultName"),
eq(file.getName()), eq(file),
isA(ProgressListener.class)
)).andReturn(new UploadResult(UUID.randomUUID().toString())).times(1);
replay(atm);

final OptionSet options = optionParser.parse("--vault", "vaultName", "--upload", file.getAbsolutePath());
Expand Down

0 comments on commit dd89bc9

Please sign in to comment.