Stop background job when cancelling two-way sync

Signed-off-by: ZetaTom <70907959+ZetaTom@users.noreply.github.com>
This commit is contained in:
ZetaTom 2024-10-30 09:53:32 +01:00
parent dd7b08ff7a
commit a78607a6e9
No known key found for this signature in database
GPG key ID: BC815CEC88F62FBA
4 changed files with 25 additions and 0 deletions

View file

@ -130,6 +130,11 @@ interface BackgroundJobManager {
changedFiles: Array<String> = arrayOf<String>()
)
/**
* Cancel two-way sync. Existing tasks might finish, but no new invocations will occur.
*/
fun cancelTwoWaySyncJob(user: User)
fun scheduleOfflineSync()
fun scheduleMediaFoldersDetectionJob()

View file

@ -503,6 +503,10 @@ internal class BackgroundJobManagerImpl(
)
}
override fun cancelTwoWaySyncJob(user: User) {
workManager.cancelJob(JOB_INTERNAL_TWO_WAY_SYNC, user)
}
override fun scheduleOfflineSync() {
val constrains = Constraints.Builder()
.setRequiredNetworkType(NetworkType.UNMETERED)

View file

@ -29,6 +29,8 @@ class InternalTwoWaySyncWork(
private val powerManagementService: PowerManagementService,
private val connectivityService: ConnectivityService
) : Worker(context, params) {
private var shouldRun = true
override fun doWork(): Result {
Log_OC.d(TAG, "Worker started!")
@ -50,6 +52,11 @@ class InternalTwoWaySyncWork(
val folders = fileDataStorageManager.getInternalTwoWaySyncFolders(user)
for (folder in folders) {
if (!shouldRun) {
Log_OC.d(TAG, "Worker was stopped!")
return Result.failure()
}
checkFreeSpace(folder)?.let { checkFreeSpaceResult ->
return checkFreeSpaceResult
}
@ -90,6 +97,12 @@ class InternalTwoWaySyncWork(
}
}
override fun onStopped() {
Log_OC.d(TAG, "OnStopped of worker called!")
shouldRun = false
super.onStopped()
}
@Suppress("TooGenericExceptionCaught")
private fun checkFreeSpace(folder: OCFile): Result? {
val storagePath = folder.storagePath ?: MainApp.getStoragePath()

View file

@ -96,6 +96,9 @@ class InternalTwoWaySyncActivity : DrawerActivity(), Injectable {
*/
private fun removeAllFolders() {
CoroutineScope(Dispatchers.Main).launch {
// cancel main worker
backgroundJobManager.cancelTwoWaySyncJob(user.get())
val folders = fileDataStorageManager.getInternalTwoWaySyncFolders(user.get())
folders.forEach { folder ->
// update database to ignore folder