From 9f1db185fdf857abd9b77029a65899f71cf62e86 Mon Sep 17 00:00:00 2001 From: Tyler Roach Date: Tue, 9 Jan 2024 11:35:57 -0500 Subject: [PATCH] fix(storage): Remove work manager restricted API usage and upgrade lib. (#2674) --- .../cloudwatch/worker/CloudwatchRouterWorker.kt | 12 +++--------- .../cloudwatch/worker/CloudwatchWorkerFactory.kt | 4 ++-- .../s3/transfer/worker/BaseTransferWorker.kt | 14 ++++++-------- .../storage/s3/transfer/worker/DownloadWorker.kt | 6 +++--- .../s3/transfer/worker/PartUploadTransferWorker.kt | 4 +++- .../storage/s3/transfer/worker/RouterWorker.kt | 14 +++----------- .../s3/transfer/worker/TransferWorkerFactory.kt | 3 +-- 7 files changed, 21 insertions(+), 36 deletions(-) diff --git a/aws-logging-cloudwatch/src/main/java/com/amplifyframework/logging/cloudwatch/worker/CloudwatchRouterWorker.kt b/aws-logging-cloudwatch/src/main/java/com/amplifyframework/logging/cloudwatch/worker/CloudwatchRouterWorker.kt index 9ff4ef896d..0d78571cb0 100644 --- a/aws-logging-cloudwatch/src/main/java/com/amplifyframework/logging/cloudwatch/worker/CloudwatchRouterWorker.kt +++ b/aws-logging-cloudwatch/src/main/java/com/amplifyframework/logging/cloudwatch/worker/CloudwatchRouterWorker.kt @@ -14,9 +14,9 @@ */ package com.amplifyframework.logging.cloudwatch.worker -import android.annotation.SuppressLint import android.content.Context import androidx.concurrent.futures.CallbackToFutureAdapter +import androidx.work.CoroutineWorker import androidx.work.ListenableWorker import androidx.work.WorkerParameters import com.google.common.util.concurrent.ListenableFuture @@ -28,7 +28,7 @@ internal class CloudwatchRouterWorker(appContext: Context, private val parameter private val workerClassName = parameter.inputData.getString(WORKER_CLASS_NAME) ?: throw IllegalArgumentException("Worker class name is missing") - private var delegateWorker: ListenableWorker? = null + private var delegateWorker: CoroutineWorker? = null companion object { internal const val WORKER_CLASS_NAME = "WORKER_CLASS_NAME" @@ -70,14 +70,8 @@ internal class CloudwatchRouterWorker(appContext: Context, private val parameter } } - @SuppressLint("RestrictedApi") override fun onStopped() { super.onStopped() - delegateWorker?.stop() - } - - @SuppressLint("RestrictedApi") - override fun isRunInForeground(): Boolean { - return delegateWorker?.isRunInForeground ?: false + delegateWorker?.onStopped() } } diff --git a/aws-logging-cloudwatch/src/main/java/com/amplifyframework/logging/cloudwatch/worker/CloudwatchWorkerFactory.kt b/aws-logging-cloudwatch/src/main/java/com/amplifyframework/logging/cloudwatch/worker/CloudwatchWorkerFactory.kt index dc6e7878a1..0936a0e777 100644 --- a/aws-logging-cloudwatch/src/main/java/com/amplifyframework/logging/cloudwatch/worker/CloudwatchWorkerFactory.kt +++ b/aws-logging-cloudwatch/src/main/java/com/amplifyframework/logging/cloudwatch/worker/CloudwatchWorkerFactory.kt @@ -15,7 +15,7 @@ package com.amplifyframework.logging.cloudwatch.worker import android.content.Context -import androidx.work.ListenableWorker +import androidx.work.CoroutineWorker import androidx.work.WorkerFactory import androidx.work.WorkerParameters import com.amplifyframework.logging.cloudwatch.CloudWatchLogManager @@ -30,7 +30,7 @@ internal class CloudwatchWorkerFactory( appContext: Context, workerClassName: String, workerParameters: WorkerParameters - ): ListenableWorker { + ): CoroutineWorker { return when (workerClassName) { CloudwatchLogsSyncWorker::class.java.simpleName -> { CloudwatchLogsSyncWorker( diff --git a/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/transfer/worker/BaseTransferWorker.kt b/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/transfer/worker/BaseTransferWorker.kt index 9b8632fca7..1c3b24f6c4 100644 --- a/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/transfer/worker/BaseTransferWorker.kt +++ b/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/transfer/worker/BaseTransferWorker.kt @@ -51,6 +51,8 @@ import java.io.File import java.lang.Exception import java.net.SocketException import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.currentCoroutineContext +import kotlinx.coroutines.isActive /** * Base worker to perform transfer file task. @@ -74,9 +76,6 @@ internal abstract class BaseTransferWorker( internal const val PART_RECORD_ID = "PART_RECORD_ID" internal const val RUN_AS_FOREGROUND_TASK = "RUN_AS_FOREGROUND_TASK" internal const val WORKER_ID = "WORKER_ID" - private const val OBJECT_TAGS_DELIMITER = "&" - private const val OBJECT_TAG_KEY_VALUE_SEPARATOR = "=" - private const val REQUESTER_PAYS = "requester" private val CANNED_ACL_MAP = ObjectCannedAcl.values().associateBy { it.value } internal const val MULTI_PART_UPLOAD_ID = "multipartUploadId" @@ -112,10 +111,10 @@ internal abstract class BaseTransferWorker( } else -> { val ex = result.exceptionOrNull() - if (!isStopped) { + if (currentCoroutineContext().isActive) { logger.error("${this.javaClass.simpleName} failed with exception: ${Log.getStackTraceString(ex)}") } - if (isRetryableError(ex)) { + if (!currentCoroutineContext().isActive && isRetryableError(ex)) { Result.retry() } else { transferStatusUpdater.updateOnError(transferRecord.id, Exception(ex)) @@ -151,8 +150,7 @@ internal abstract class BaseTransferWorker( } private fun isRetryableError(e: Throwable?): Boolean { - return isStopped || - !isNetworkAvailable(applicationContext) || + return !isNetworkAvailable(applicationContext) || runAttemptCount < maxRetryCount || e is CancellationException || // SocketException is thrown when download is terminated due to network disconnection. @@ -200,7 +198,7 @@ internal abstract class BaseTransferWorker( return false } - internal suspend fun createPutObjectRequest( + internal fun createPutObjectRequest( transferRecord: TransferRecord, progressListener: ProgressListener? ): PutObjectRequest { diff --git a/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/transfer/worker/DownloadWorker.kt b/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/transfer/worker/DownloadWorker.kt index 083ccad2b6..87a9db5612 100644 --- a/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/transfer/worker/DownloadWorker.kt +++ b/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/transfer/worker/DownloadWorker.kt @@ -31,8 +31,9 @@ import com.amplifyframework.storage.s3.transfer.TransferStatusUpdater import java.io.BufferedOutputStream import java.io.File import java.io.FileOutputStream -import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.currentCoroutineContext +import kotlinx.coroutines.isActive import kotlinx.coroutines.withContext /** @@ -48,7 +49,6 @@ internal class DownloadWorker( private lateinit var downloadProgressListener: DownloadProgressListener private val defaultBufferSize = 8192L - val coroutineScope = CoroutineScope(Dispatchers.IO) override suspend fun performWork(): Result { s3.withConfig { enableAccelerate = transferRecord.useAccelerateEndpoint == 1 @@ -104,7 +104,7 @@ internal class DownloadWorker( var totalRead = 0L BufferedOutputStream(fileOutputStream).use { fileOutput -> val copied = 0L - while (!isStopped) { + while (currentCoroutineContext().isActive) { val remaining = limit - copied if (remaining == 0L) break val readBytes = diff --git a/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/transfer/worker/PartUploadTransferWorker.kt b/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/transfer/worker/PartUploadTransferWorker.kt index 41c7c7fd0d..d145786b6d 100644 --- a/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/transfer/worker/PartUploadTransferWorker.kt +++ b/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/transfer/worker/PartUploadTransferWorker.kt @@ -26,6 +26,8 @@ import com.amplifyframework.storage.s3.transfer.TransferDB import com.amplifyframework.storage.s3.transfer.TransferStatusUpdater import com.amplifyframework.storage.s3.transfer.UploadProgressListenerInterceptor import java.io.File +import kotlinx.coroutines.currentCoroutineContext +import kotlinx.coroutines.isActive /** * Worker to upload a part for multipart upload @@ -43,7 +45,7 @@ internal class PartUploadTransferWorker( override var maxRetryCount = 3 override suspend fun performWork(): Result { - if (isStopped) { + if (!currentCoroutineContext().isActive) { return Result.retry() } transferStatusUpdater.updateTransferState(transferRecord.mainUploadId, TransferState.IN_PROGRESS) diff --git a/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/transfer/worker/RouterWorker.kt b/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/transfer/worker/RouterWorker.kt index c8158e51fb..b589e60a92 100644 --- a/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/transfer/worker/RouterWorker.kt +++ b/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/transfer/worker/RouterWorker.kt @@ -15,7 +15,6 @@ package com.amplifyframework.storage.s3.transfer.worker -import android.annotation.SuppressLint import android.content.Context import androidx.concurrent.futures.CallbackToFutureAdapter import androidx.work.ListenableWorker @@ -29,8 +28,7 @@ import java.lang.IllegalStateException /** * Worker to route transfer WorkRequest to appropriate WorkerFactory */ -@SuppressLint("RestrictedApi") -internal class RouterWorker constructor( +internal class RouterWorker( appContext: Context, private val parameter: WorkerParameters ) : ListenableWorker(appContext, parameter) { @@ -45,7 +43,7 @@ internal class RouterWorker constructor( ?: throw IllegalArgumentException("Worker class name is missing") private val workerId = parameter.inputData.getString(BaseTransferWorker.WORKER_ID) - private var delegateWorker: ListenableWorker? = null + private var delegateWorker: BaseTransferWorker? = null companion object { internal const val WORKER_CLASS_NAME = "WORKER_CLASS_NAME" @@ -86,15 +84,9 @@ internal class RouterWorker constructor( } } - @SuppressLint("RestrictedApi") override fun onStopped() { super.onStopped() logger.debug("onStopped for $id") - delegateWorker?.stop() - } - - @SuppressLint("RestrictedApi") - override fun isRunInForeground(): Boolean { - return delegateWorker?.isRunInForeground ?: false + delegateWorker?.onStopped() } } diff --git a/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/transfer/worker/TransferWorkerFactory.kt b/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/transfer/worker/TransferWorkerFactory.kt index ac93da9bc2..88c0dc19f4 100644 --- a/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/transfer/worker/TransferWorkerFactory.kt +++ b/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/transfer/worker/TransferWorkerFactory.kt @@ -15,7 +15,6 @@ package com.amplifyframework.storage.s3.transfer.worker import android.content.Context -import androidx.work.ListenableWorker import androidx.work.WorkerFactory import androidx.work.WorkerParameters import aws.sdk.kotlin.services.s3.S3Client @@ -34,7 +33,7 @@ internal class TransferWorkerFactory( appContext: Context, workerClassName: String, workerParameters: WorkerParameters - ): ListenableWorker { + ): BaseTransferWorker { when (workerClassName) { DownloadWorker::class.java.name -> return DownloadWorker(