diff --git a/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManager.kt b/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManager.kt index 68a7a027ec..e366bd2054 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManager.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManager.kt @@ -163,4 +163,5 @@ interface BackgroundJobManager { fun cancelAllJobs() fun schedulePeriodicHealthStatus() fun startHealthStatus() + fun bothFilesSyncJobsRunning(): Boolean } diff --git a/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt b/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt index 945e14ded7..59bab2e038 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt @@ -21,6 +21,7 @@ import androidx.work.PeriodicWorkRequest import androidx.work.WorkInfo import androidx.work.WorkManager import androidx.work.workDataOf +import com.google.common.util.concurrent.ListenableFuture import com.nextcloud.client.account.User import com.nextcloud.client.core.Clock import com.nextcloud.client.di.Injectable @@ -28,11 +29,14 @@ import com.nextcloud.client.documentscan.GeneratePdfFromImagesWork import com.nextcloud.client.jobs.download.FileDownloadWorker import com.nextcloud.client.jobs.upload.FileUploadWorker import com.nextcloud.client.preferences.AppPreferences +import com.nextcloud.utils.extensions.isWorkRunning import com.nextcloud.utils.extensions.isWorkScheduled import com.owncloud.android.datamodel.OCFile +import com.owncloud.android.lib.common.utils.Log_OC import com.owncloud.android.operations.DownloadType import java.util.Date import java.util.UUID +import java.util.concurrent.ExecutionException import java.util.concurrent.TimeUnit import kotlin.reflect.KClass @@ -403,6 +407,12 @@ internal class BackgroundJobManagerImpl( workManager.cancelJob(JOB_PERIODIC_CALENDAR_BACKUP, user) } + override fun bothFilesSyncJobsRunning(): Boolean { + + return workManager.isWorkRunning(JOB_PERIODIC_FILES_SYNC) && + workManager.isWorkRunning(JOB_IMMEDIATE_FILES_SYNC) + } + override fun schedulePeriodicFilesSyncJob() { val request = periodicRequestBuilder( jobClass = FilesSyncWork::class, diff --git a/app/src/main/java/com/nextcloud/client/jobs/FilesSyncWork.kt b/app/src/main/java/com/nextcloud/client/jobs/FilesSyncWork.kt index 651c65aa02..d714ae7561 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/FilesSyncWork.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/FilesSyncWork.kt @@ -94,6 +94,10 @@ class FilesSyncWork( @Suppress("MagicNumber") override fun doWork(): Result { + if (backgroundJobManager.bothFilesSyncJobsRunning()){ + Log_OC.d(TAG,"Kill Sync Worker since another instance of the worker seems to be running already!") + return Result.success() + } backgroundJobManager.logStartOfWorker(BackgroundJobManagerImpl.formatClassTag(this::class)) val overridePowerSaving = inputData.getBoolean(OVERRIDE_POWER_SAVING, false) diff --git a/app/src/main/java/com/nextcloud/utils/extensions/WorkManagerExtensions.kt b/app/src/main/java/com/nextcloud/utils/extensions/WorkManagerExtensions.kt index efd20b1296..bc456cb662 100644 --- a/app/src/main/java/com/nextcloud/utils/extensions/WorkManagerExtensions.kt +++ b/app/src/main/java/com/nextcloud/utils/extensions/WorkManagerExtensions.kt @@ -33,3 +33,26 @@ fun WorkManager.isWorkScheduled(tag: String): Boolean { return running } + +fun WorkManager.isWorkRunning(tag: String): Boolean { + val statuses: ListenableFuture> = this.getWorkInfosByTag(tag) + var running = false + var workInfoList: List = emptyList() + + try { + workInfoList = statuses.get() + } catch (e: ExecutionException) { + Log_OC.d("Worker", "ExecutionException in isWorkScheduled: $e") + } catch (e: InterruptedException) { + Log_OC.d("Worker", "InterruptedException in isWorkScheduled: $e") + } + + for (workInfo in workInfoList) { + val state = workInfo.state + running = running || state == WorkInfo.State.RUNNING + } + + return running +} + +