Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sync worker only run when needed #12372

Merged
merged 22 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,5 @@ interface BackgroundJobManager {
fun cancelAllJobs()
fun schedulePeriodicHealthStatus()
fun startHealthStatus()
fun bothFilesSyncJobsRunning(): Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ 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.operations.DownloadType
Expand Down Expand Up @@ -403,6 +404,11 @@ 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,
Expand Down
31 changes: 27 additions & 4 deletions app/src/main/java/com/nextcloud/client/jobs/FilesSyncWork.kt
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,41 @@ class FilesSyncWork(
setForegroundAsync(foregroundInfo)
}

private fun canExitEarly(changedFiles: Array<String>?): Boolean {
var canExitEarly = false
// If we are in power save mode better to postpone scan and upload
val overridePowerSaving = inputData.getBoolean(OVERRIDE_POWER_SAVING, false)
if ((powerManagementService.isPowerSavingEnabled && !overridePowerSaving)) {
canExitEarly = true
}

// or sync worker already running and no changed files to be processed
val alreadyRunning = backgroundJobManager.bothFilesSyncJobsRunning()
if (alreadyRunning && changedFiles.isNullOrEmpty()) {
Log_OC.d(TAG, "FILESYNC Kill Sync Worker since another instance of the worker seems to be running already!")
canExitEarly = true
}

if (!syncedFolderProvider.syncedFolders.any { it.isEnabled }) {
Log_OC.d(TAG, "FILESYNC Kill Sync Worker since no sync folder is enabled!")
canExitEarly = true
}

return canExitEarly
}

@Suppress("MagicNumber")
override fun doWork(): Result {
backgroundJobManager.logStartOfWorker(BackgroundJobManagerImpl.formatClassTag(this::class))

val overridePowerSaving = inputData.getBoolean(OVERRIDE_POWER_SAVING, false)
// If we are in power save mode, better to postpone upload
if (powerManagementService.isPowerSavingEnabled && !overridePowerSaving) {
val changedFiles = inputData.getStringArray(CHANGED_FILES)

if (canExitEarly(changedFiles)) {
val result = Result.success()
backgroundJobManager.logEndOfWorker(BackgroundJobManagerImpl.formatClassTag(this::class), result)
return result
}

val resources = context.resources
val lightVersion = resources.getBoolean(R.bool.syncedFolder_light)
FilesSyncHelper.restartJobsIfNeeded(
Expand All @@ -113,7 +137,6 @@ class FilesSyncWork(
)

// Get changed files from ContentObserverWork (only images and videos) or by scanning filesystem
val changedFiles = inputData.getStringArray(CHANGED_FILES)
collectChangedFiles(changedFiles)

// Create all the providers we'll need
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import java.util.concurrent.ExecutionException

fun WorkManager.isWorkScheduled(tag: String): Boolean {
val statuses: ListenableFuture<List<WorkInfo>> = this.getWorkInfosByTag(tag)
var running = false
var workInfoList: List<WorkInfo> = emptyList()

try {
Expand All @@ -26,10 +25,24 @@ fun WorkManager.isWorkScheduled(tag: String): Boolean {
Log_OC.d("Worker", "InterruptedException in isWorkScheduled: $e")
}

for (workInfo in workInfoList) {
val state = workInfo.state
running = running || (state == WorkInfo.State.RUNNING || state == WorkInfo.State.ENQUEUED)
return workInfoList.any {
it.state == WorkInfo.State.RUNNING || it.state == WorkInfo.State.ENQUEUED
}
}

fun WorkManager.isWorkRunning(tag: String): Boolean {
val statuses: ListenableFuture<List<WorkInfo>> = this.getWorkInfosByTag(tag)
var workInfoList: List<WorkInfo> = emptyList()

return running
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")
}

return workInfoList.any {
it.state == WorkInfo.State.RUNNING
}
}
Loading