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

Conversation

JonasMayerDev
Copy link
Collaborator

@JonasMayerDev JonasMayerDev commented Jan 15, 2024

  1. Before running sync worker, check if another sync worker is already running.
    Before in theory 2 sync jobs could run and potentially mess with each other (maybe leads to Excessive file upload conflicts after update to 3.26.0 (not before) #11974)
  2. Check if any sync folder is enabled and early exit if not.
  3. Only scan the file system if "only charging" is disabled or is charging
  • Tests written, or not not needed

@JonasMayerDev JonasMayerDev force-pushed the make_sync_service_run_only_once branch 5 times, most recently from 92c29af to 710bad9 Compare January 19, 2024 10:58
Copy link
Collaborator

@ZetaTom ZetaTom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my brief testing everything worked as expected. Although I didn't check any edge cases (what happens when a worker is stuck, for example). This might be out of scope for this pull request.

The suggestions are just a minor change and, in reality, just eye candy.

@JonasMayerDev JonasMayerDev force-pushed the make_sync_service_run_only_once branch 4 times, most recently from 078de3b to fc33f40 Compare January 29, 2024 19:43
@JonasMayerDev JonasMayerDev reopened this Apr 9, 2024
Signed-off-by: Jonas Mayer <[email protected]>
Signed-off-by: Jonas Mayer <[email protected]>
@JonasMayerDev JonasMayerDev force-pushed the make_sync_service_run_only_once branch from fc33f40 to c420b5e Compare April 9, 2024 14:35
@JonasMayerDev JonasMayerDev force-pushed the make_sync_service_run_only_once branch from b121c21 to cf8199e Compare April 9, 2024 14:56
@JonasMayerDev
Copy link
Collaborator Author

Problem: With current implementation when periodic sync worker is started and immediate filesync is already running because of changed files it will skip periodic sync which is not intended... But it is really unlikely since immediate file sync with changed files should only run for under 1 sec.

Signed-off-by: Jonas Mayer <[email protected]>
… into make_sync_service_run_only_once

# Conflicts:
#	app/src/main/java/com/nextcloud/client/jobs/FilesSyncWork.kt
Signed-off-by: Jonas Mayer <[email protected]>
Copy link
Collaborator

@alperozturk96 alperozturk96 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can get rid of repeated codes in WorkManagerExtensions with this code.

private const val TAG = "WorkManager"

fun WorkManager.isWorkRunning(tag: String): Boolean = checkWork(tag, listOf(WorkInfo.State.RUNNING))

fun WorkManager.isWorkScheduled(tag: String): Boolean =
    checkWork(tag, listOf(WorkInfo.State.RUNNING, WorkInfo.State.ENQUEUED))

private fun WorkManager.checkWork(tag: String, stateConditions: List<WorkInfo.State>): Boolean {
    val statuses: ListenableFuture<List<WorkInfo>> = getWorkInfosByTag(tag)
    var workInfoList: List<WorkInfo> = emptyList()

    try {
        workInfoList = statuses.get()
    } catch (e: ExecutionException) {
        Log_OC.d(TAG, "ExecutionException in checkWork: $e")
    } catch (e: InterruptedException) {
        Log_OC.d(TAG, "InterruptedException in checkWork: $e")
    }

    return workInfoList.any { workInfo -> stateConditions.contains(workInfo.state) }
}

# Conflicts:
#	app/src/main/java/com/nextcloud/client/jobs/FilesSyncWork.kt
Signed-off-by: Jonas Mayer <[email protected]>
@JonasMayerDev JonasMayerDev mentioned this pull request May 2, 2024
4 tasks
Signed-off-by: Jonas Mayer <[email protected]>
Signed-off-by: Jonas Mayer <[email protected]>
@JonasMayerDev JonasMayerDev changed the title Make sure only one sync job is running at a time Make sync worker only run when needed May 2, 2024
Copy link
Collaborator

@alperozturk96 alperozturk96 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested didn't came across with any regression

Signed-off-by: alperozturk <[email protected]>
Copy link

github-actions bot commented May 3, 2024

Codacy

Lint

TypemasterPR
Warnings7272
Errors33

SpotBugs

CategoryBaseNew
Bad practice6666
Correctness7373
Dodgy code349349
Experimental11
Internationalization77
Malicious code vulnerability22
Multithreaded correctness66
Performance5757
Security1919
Total580580

Copy link

github-actions bot commented May 3, 2024

APK file: https://www.kaminsky.me/nc-dev/android-artifacts/12372.apk

qrcode

To test this change/fix you can simply download above APK file and install and test it in parallel to your existing Nextcloud app.

@alperozturk96 alperozturk96 merged commit f027458 into master May 3, 2024
21 checks passed
@delete-merged-branch delete-merged-branch bot deleted the make_sync_service_run_only_once branch May 3, 2024 11:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants