Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
Make the GitHub worker more reliable
Browse files Browse the repository at this point in the history
  • Loading branch information
ILIYANGERMANOV committed Sep 2, 2023
1 parent 2c64d99 commit c0b84e5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ import androidx.work.Constraints
import androidx.work.CoroutineWorker
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.NetworkType
import androidx.work.PeriodicWorkRequest
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.WorkerParameters
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import dagger.hilt.android.qualifiers.ApplicationContext
import java.util.Calendar
import java.util.concurrent.TimeUnit
import javax.inject.Inject

Expand All @@ -25,20 +23,18 @@ class GitHubAutoBackupManager @Inject constructor(
private val uniqueWorkName = "GITHUB_AUTO_BACKUP_WORK"

fun scheduleAutoBackups() {
val initialDelay = calculateInitialDelayMillis()

val dailyWorkRequest = PeriodicWorkRequestBuilder<GitHubBackupWorker>(
12, TimeUnit.HOURS
).setInitialDelay(initialDelay, TimeUnit.MILLISECONDS)
6, TimeUnit.HOURS
).setInitialDelay(30, TimeUnit.MINUTES)
.setConstraints(
Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
)
.setBackoffCriteria(
BackoffPolicy.EXPONENTIAL,
PeriodicWorkRequest.MIN_BACKOFF_MILLIS,
TimeUnit.MILLISECONDS
60,
TimeUnit.SECONDS
)
.build()

Expand All @@ -50,25 +46,6 @@ class GitHubAutoBackupManager @Inject constructor(
)
}

private fun calculateInitialDelayMillis(): Long {
val lunchTime = Calendar.getInstance().apply {
set(Calendar.HOUR_OF_DAY, 12)
set(Calendar.MINUTE, 0)
set(Calendar.SECOND, 0)
}

val currentTime = Calendar.getInstance()

return if (currentTime.after(lunchTime)) {
// If current time is after 12 pm, schedule for next day
lunchTime.add(Calendar.DAY_OF_YEAR, 1)
lunchTime.timeInMillis - currentTime.timeInMillis
} else {
// If it's before 12 pm, set delay to reach 12 pm
lunchTime.timeInMillis - currentTime.timeInMillis
}
}

fun cancelAutoBackups() {
WorkManager.getInstance(context).cancelUniqueWork(uniqueWorkName)
}
Expand All @@ -81,7 +58,7 @@ class GitHubBackupWorker @AssistedInject constructor(
private val gitHubBackup: GitHubBackup,
) : CoroutineWorker(appContext, params) {
companion object {
const val MAX_RETRIES = 7
const val MAX_RETRIES = 15
}

override suspend fun doWork(): Result {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class GitHubWorkerMigration @Inject constructor(
private val gitHubAutoBackupManager: GitHubAutoBackupManager,
) : Migration {
override val key: String
get() = "github_auto_backup_worker_v1"
get() = "github_auto_backup_worker_v2"

override suspend fun migrate() {
if (gitHubBackup.enabled.firstOrNull() == true) {
Expand Down

0 comments on commit c0b84e5

Please sign in to comment.