check if sync worker is already running

Signed-off-by: Jonas Mayer <jonas.a.mayer@gmx.net>
This commit is contained in:
Jonas Mayer 2024-01-12 15:06:51 +01:00 committed by Jonas Mayer
parent 177ffc9688
commit aef9a41a5f
4 changed files with 38 additions and 0 deletions

View file

@ -163,4 +163,5 @@ interface BackgroundJobManager {
fun cancelAllJobs() fun cancelAllJobs()
fun schedulePeriodicHealthStatus() fun schedulePeriodicHealthStatus()
fun startHealthStatus() fun startHealthStatus()
fun bothFilesSyncJobsRunning(): Boolean
} }

View file

@ -21,6 +21,7 @@ import androidx.work.PeriodicWorkRequest
import androidx.work.WorkInfo import androidx.work.WorkInfo
import androidx.work.WorkManager import androidx.work.WorkManager
import androidx.work.workDataOf import androidx.work.workDataOf
import com.google.common.util.concurrent.ListenableFuture
import com.nextcloud.client.account.User import com.nextcloud.client.account.User
import com.nextcloud.client.core.Clock import com.nextcloud.client.core.Clock
import com.nextcloud.client.di.Injectable 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.download.FileDownloadWorker
import com.nextcloud.client.jobs.upload.FileUploadWorker import com.nextcloud.client.jobs.upload.FileUploadWorker
import com.nextcloud.client.preferences.AppPreferences import com.nextcloud.client.preferences.AppPreferences
import com.nextcloud.utils.extensions.isWorkRunning
import com.nextcloud.utils.extensions.isWorkScheduled import com.nextcloud.utils.extensions.isWorkScheduled
import com.owncloud.android.datamodel.OCFile import com.owncloud.android.datamodel.OCFile
import com.owncloud.android.lib.common.utils.Log_OC
import com.owncloud.android.operations.DownloadType import com.owncloud.android.operations.DownloadType
import java.util.Date import java.util.Date
import java.util.UUID import java.util.UUID
import java.util.concurrent.ExecutionException
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import kotlin.reflect.KClass import kotlin.reflect.KClass
@ -403,6 +407,12 @@ internal class BackgroundJobManagerImpl(
workManager.cancelJob(JOB_PERIODIC_CALENDAR_BACKUP, user) 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() { override fun schedulePeriodicFilesSyncJob() {
val request = periodicRequestBuilder( val request = periodicRequestBuilder(
jobClass = FilesSyncWork::class, jobClass = FilesSyncWork::class,

View file

@ -94,6 +94,10 @@ class FilesSyncWork(
@Suppress("MagicNumber") @Suppress("MagicNumber")
override fun doWork(): Result { 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)) backgroundJobManager.logStartOfWorker(BackgroundJobManagerImpl.formatClassTag(this::class))
val overridePowerSaving = inputData.getBoolean(OVERRIDE_POWER_SAVING, false) val overridePowerSaving = inputData.getBoolean(OVERRIDE_POWER_SAVING, false)

View file

@ -33,3 +33,26 @@ fun WorkManager.isWorkScheduled(tag: String): Boolean {
return running return running
} }
fun WorkManager.isWorkRunning(tag: String): Boolean {
val statuses: ListenableFuture<List<WorkInfo>> = this.getWorkInfosByTag(tag)
var running = false
var workInfoList: List<WorkInfo> = 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
}