Refactor Uploader.upload(LocalFile,Bucket,Boolean,UploadEventListener,Int) (#129)

* [storate-api] Remove unused tryCount

* [storage-aws] Uploader Reduce the number of parameters
This commit is contained in:
Paul Campbell 2019-08-01 08:59:29 +01:00 committed by GitHub
parent 899e0724c9
commit 4a54038933
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 54 additions and 63 deletions

View file

@ -36,8 +36,7 @@ case class UnversionedMirrorArchive(syncTotals: SyncTotals)
Storage.upload(
localFile,
bucket,
UploadEventListener(localFile, index, syncTotals, totalBytesSoFar),
1)
UploadEventListener(localFile, index, syncTotals, totalBytesSoFar))
}
object UnversionedMirrorArchive {

View file

@ -20,10 +20,11 @@ case class DummyStorageService(s3ObjectData: S3ObjectsData,
): TaskR[Console, S3ObjectsData] =
TaskR(s3ObjectData)
override def upload(localFile: LocalFile,
bucket: Bucket,
uploadEventListener: UploadEventListener,
tryCount: Int): UIO[StorageQueueEvent] = {
override def upload(
localFile: LocalFile,
bucket: Bucket,
uploadEventListener: UploadEventListener,
): UIO[StorageQueueEvent] = {
val (remoteKey, md5Hash) = uploadFiles(localFile.file)
UIO(StorageQueueEvent.UploadQueueEvent(remoteKey, md5Hash))
}

View file

@ -21,7 +21,6 @@ object Storage {
localFile: LocalFile,
bucket: Bucket,
uploadEventListener: UploadEventListener,
tryCount: Int
): ZIO[Storage with Config, Nothing, StorageQueueEvent]
def copy(
@ -57,8 +56,8 @@ object Storage {
override def upload(
localFile: LocalFile,
bucket: Bucket,
uploadEventListener: UploadEventListener,
tryCount: Int): ZIO[Storage, Nothing, StorageQueueEvent] =
uploadEventListener: UploadEventListener
): ZIO[Storage, Nothing, StorageQueueEvent] =
uploadResult
override def copy(
@ -99,11 +98,9 @@ object Storage {
final def upload(
localFile: LocalFile,
bucket: Bucket,
uploadEventListener: UploadEventListener,
tryCount: Int
uploadEventListener: UploadEventListener
): ZIO[Storage with Config, Nothing, StorageQueueEvent] =
ZIO.accessM(
_.storage upload (localFile, bucket, uploadEventListener, tryCount))
ZIO.accessM(_.storage upload (localFile, bucket, uploadEventListener))
final def copy(
bucket: Bucket,

View file

@ -28,11 +28,9 @@ object S3Storage {
localFile: LocalFile,
bucket: Bucket,
uploadEventListener: UploadEventListener,
tryCount: Int): ZIO[Config, Nothing, StorageQueueEvent] =
Uploader.upload(transferManager)(localFile,
bucket,
uploadEventListener,
1)
): ZIO[Config, Nothing, StorageQueueEvent] =
Uploader.upload(transferManager)(
Uploader.Request(localFile, bucket, uploadEventListener))
override def copy(bucket: Bucket,
sourceKey: RemoteKey,

View file

@ -18,24 +18,24 @@ import zio.{UIO, ZIO}
trait Uploader {
def upload(transferManager: => AmazonTransferManager)(
case class Request(
localFile: LocalFile,
bucket: Bucket,
uploadEventListener: UploadEventListener,
tryCount: Int
): ZIO[Config, Nothing, StorageQueueEvent] =
transfer(transferManager)(localFile, bucket, uploadEventListener)
.catchAll(handleError(localFile.remoteKey))
uploadEventListener: UploadEventListener
)
def upload(transferManager: => AmazonTransferManager)(
request: Request): ZIO[Config, Nothing, StorageQueueEvent] =
transfer(transferManager)(request)
.catchAll(handleError(request.localFile.remoteKey))
private def handleError(remoteKey: RemoteKey)(e: Throwable) =
UIO(ErrorQueueEvent(Action.Upload(remoteKey.key), remoteKey, e))
private def transfer(transferManager: => AmazonTransferManager)(
localFile: LocalFile,
bucket: Bucket,
uploadEventListener: UploadEventListener
request: Request
) =
request(localFile, bucket, progressListener(uploadEventListener)) >>=
putObjectRequest(request) >>=
dispatch(transferManager)
private def dispatch(transferManager: AmazonTransferManager)(
@ -49,18 +49,20 @@ trait Uploader {
MD5Hash(uploadResult.getETag)))
}
private def request(
localFile: LocalFile,
bucket: Bucket,
listener: ProgressListener
private def putObjectRequest(
request: Request
) = {
val request =
new PutObjectRequest(bucket.name, localFile.remoteKey.key, localFile.file)
.withMetadata(metadata(localFile))
val putRequest =
new PutObjectRequest(request.bucket.name,
request.localFile.remoteKey.key,
request.localFile.file)
.withMetadata(metadata(request.localFile))
for {
batchMode <- Config.batchMode
r = if (batchMode) request
else request.withGeneralProgressListener(listener)
r = if (batchMode) putRequest
else
putRequest.withGeneralProgressListener(
progressListener(request.uploadEventListener))
} yield r
}

View file

@ -25,28 +25,31 @@ trait AmazonS3ClientTestFixture extends MockFactory {
override def listObjects(
bucket: Bucket,
prefix: RemoteKey): TaskR[Console, S3ObjectsData] =
prefix: RemoteKey
): TaskR[Console, S3ObjectsData] =
Lister.listObjects(client)(bucket, prefix)
override def upload(
localFile: LocalFile,
bucket: Bucket,
uploadEventListener: UploadEventListener,
tryCount: Int): ZIO[Config, Nothing, StorageQueueEvent] =
Uploader.upload(transferManager)(localFile,
bucket,
uploadEventListener,
1)
): ZIO[Config, Nothing, StorageQueueEvent] =
Uploader.upload(transferManager)(
Uploader.Request(localFile, bucket, uploadEventListener))
override def copy(bucket: Bucket,
sourceKey: RemoteKey,
hash: MD5Hash,
targetKey: RemoteKey): UIO[StorageQueueEvent] =
override def copy(
bucket: Bucket,
sourceKey: RemoteKey,
hash: MD5Hash,
targetKey: RemoteKey
): UIO[StorageQueueEvent] =
Copier.copy(client)(
Copier.Request(bucket, sourceKey, hash, targetKey))
override def delete(bucket: Bucket,
remoteKey: RemoteKey): UIO[StorageQueueEvent] =
override def delete(
bucket: Bucket,
remoteKey: RemoteKey
): UIO[StorageQueueEvent] =
Deleter.delete(client)(bucket, remoteKey)
override def shutdown: UIO[StorageQueueEvent] = {

View file

@ -27,7 +27,6 @@ class UploaderTest extends FreeSpec with MockFactory {
val remoteKey = RemoteKey("aRemoteKey")
val localFile = LocalFile(aFile, aSource, hashes, remoteKey)
val bucket = Bucket("aBucket")
val tryCount = 1
val uploadResult = new UploadResult
uploadResult.setKey(remoteKey.key)
uploadResult.setETag(aHash.hash)
@ -47,8 +46,7 @@ class UploaderTest extends FreeSpec with MockFactory {
invoke(fixture.amazonS3TransferManager)(
localFile,
bucket,
uploadEventListener,
tryCount
uploadEventListener
)
assertResult(expected)(result)
}
@ -66,8 +64,7 @@ class UploaderTest extends FreeSpec with MockFactory {
invoke(fixture.amazonS3TransferManager)(
localFile,
bucket,
uploadEventListener,
tryCount
uploadEventListener
)
assertResult(expected)(result)
}
@ -85,8 +82,7 @@ class UploaderTest extends FreeSpec with MockFactory {
invoke(fixture.amazonS3TransferManager)(
localFile,
bucket,
uploadEventListener,
tryCount
uploadEventListener
)
assertResult(expected)(result)
}
@ -94,19 +90,14 @@ class UploaderTest extends FreeSpec with MockFactory {
def invoke(transferManager: AmazonTransferManager)(
localFile: LocalFile,
bucket: Bucket,
uploadEventListener: UploadEventListener,
tryCount: Int
uploadEventListener: UploadEventListener
) = {
type TestEnv = Config
val testEnv: TestEnv = Config.Live
new DefaultRuntime {}.unsafeRunSync {
Uploader
.upload(transferManager)(
localFile,
bucket,
uploadEventListener,
tryCount
)
Uploader.Request(localFile, bucket, uploadEventListener))
.provide(testEnv)
}.toEither
}