Skip to content

Commit

Permalink
Add delay and kdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
sunkup committed Jan 20, 2025
1 parent 0abb89e commit 1a41a71
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class CollectionsChangeListener @Inject constructor(
account?.let {
// Start sync after a short delay to avoid multiple syncs in a short time when multiple
// collections change quickly, e.g. at collection refresh
workerManager.enqueueOneTimeAllAuthorities(it)
workerManager.enqueueOneTimeAllAuthorities(it, delay = 500)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import androidx.work.WorkManager
import androidx.work.WorkQuery
import androidx.work.WorkRequest
import at.bitfire.davdroid.push.PushNotificationManager
import at.bitfire.davdroid.repository.DavCollectionRepository
import at.bitfire.davdroid.sync.SyncDataType
import at.bitfire.davdroid.sync.TasksAppManager
import at.bitfire.davdroid.sync.worker.BaseSyncWorker.Companion.INPUT_ACCOUNT_NAME
Expand All @@ -37,13 +36,8 @@ import at.bitfire.davdroid.sync.worker.BaseSyncWorker.Companion.INPUT_UPLOAD
import at.bitfire.davdroid.sync.worker.BaseSyncWorker.Companion.InputResync
import at.bitfire.davdroid.sync.worker.BaseSyncWorker.Companion.NO_RESYNC
import at.bitfire.davdroid.sync.worker.BaseSyncWorker.Companion.commonTag
import dagger.Binds
import dagger.Lazy
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import dagger.multibindings.IntoSet
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import java.util.concurrent.TimeUnit
Expand Down Expand Up @@ -76,7 +70,8 @@ class SyncWorkerManager @Inject constructor(
dataType: SyncDataType,
manual: Boolean = false,
@InputResync resync: Int = NO_RESYNC,
upload: Boolean = false
upload: Boolean = false,
delay: Long = 0
): OneTimeWorkRequest {
// worker arguments
val argumentsBuilder = Data.Builder()
Expand All @@ -93,7 +88,7 @@ class SyncWorkerManager @Inject constructor(
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED) // require a network connection
.build()
return OneTimeWorkRequestBuilder<OneTimeSyncWorker>()
val request = OneTimeWorkRequestBuilder<OneTimeSyncWorker>()
.addTag(OneTimeSyncWorker.workerName(account, dataType))
.addTag(commonTag(account, dataType))
.setInputData(argumentsBuilder.build())
Expand All @@ -104,12 +99,15 @@ class SyncWorkerManager @Inject constructor(
)
.setConstraints(constraints)

/* OneTimeSyncWorker is started by user or sync framework when there are local changes.
In both cases, synchronization should be done as soon as possible, so we set expedited. */
.setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
if (delay > 0)
// Expedited jobs cannot be delayed
request.setInitialDelay(delay, TimeUnit.SECONDS)
else
/* OneTimeSyncWorker is started by user or sync framework when there are local changes.
In both cases, synchronization should be done as soon as possible, so we set expedited. */
request.setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)

// build work request
.build()
return request.build()
}

/**
Expand All @@ -130,7 +128,8 @@ class SyncWorkerManager @Inject constructor(
manual: Boolean = false,
@InputResync resync: Int = NO_RESYNC,
upload: Boolean = false,
fromPush: Boolean = false
fromPush: Boolean = false,
delay: Long = 0
): String {
logger.info("Enqueueing unique worker for account=$account, dataType=$dataType, manual=$manual, resync=$resync, upload=$upload, fromPush=$fromPush")

Expand All @@ -141,15 +140,16 @@ class SyncWorkerManager @Inject constructor(
dataType = dataType,
manual = manual,
resync = resync,
upload = upload
upload = upload,
delay = delay
)
if (fromPush) {
logger.fine("Showing push sync pending notification for $name")
pushNotificationManager.notify(account, dataType)
}
WorkManager.getInstance(context).enqueueUniqueWork(
name,
/* If sync is already running, just continue.
/* If sync is already running, continue that sync; don't enqueue new work.
Existing retried work will not be replaced (for instance when
PeriodicSyncWorker enqueues another scheduled sync). */
ExistingWorkPolicy.KEEP,
Expand All @@ -169,7 +169,8 @@ class SyncWorkerManager @Inject constructor(
manual: Boolean = false,
@InputResync resync: Int = NO_RESYNC,
upload: Boolean = false,
fromPush: Boolean = false
fromPush: Boolean = false,
delay: Long = 0
) {
for (dataType in SyncDataType.entries)
enqueueOneTime(
Expand All @@ -178,7 +179,8 @@ class SyncWorkerManager @Inject constructor(
manual = manual,
resync = resync,
upload = upload,
fromPush = fromPush
fromPush = fromPush,
delay = delay
)
}

Expand Down Expand Up @@ -291,28 +293,4 @@ class SyncWorkerManager @Inject constructor(
}
}

/**
* Listener that enqueues a push registration worker when the collection list changes.
*/
class CollectionsListener @Inject constructor(
private val workerManager: SyncWorkerManager
): DavCollectionRepository.OnChangeListener {

override fun onCollectionsChanged(account: Account?) {
account?.let { workerManager.enqueueOneTimeAllAuthorities(it) }
}

}

/**
* Hilt module that registers [CollectionsListener] in [DavCollectionRepository].
*/
@Module
@InstallIn(SingletonComponent::class)
interface SyncWorkerManagerModule {
@Binds
@IntoSet
fun listener(impl: CollectionsListener): DavCollectionRepository.OnChangeListener
}

}

0 comments on commit 1a41a71

Please sign in to comment.