Make DeleteWorker more resilient

This commit is contained in:
Philipp Heckel 2022-11-19 15:03:37 -05:00
parent 69fdd30a26
commit 4bfc72c1c9
2 changed files with 22 additions and 14 deletions

View file

@ -9,14 +9,12 @@ import io.heckel.ntfy.BuildConfig
import io.heckel.ntfy.db.ATTACHMENT_PROGRESS_DELETED import io.heckel.ntfy.db.ATTACHMENT_PROGRESS_DELETED
import io.heckel.ntfy.db.Repository import io.heckel.ntfy.db.Repository
import io.heckel.ntfy.msg.DownloadIconWorker import io.heckel.ntfy.msg.DownloadIconWorker
import io.heckel.ntfy.ui.DetailAdapter
import io.heckel.ntfy.util.Log import io.heckel.ntfy.util.Log
import io.heckel.ntfy.util.fileStat import io.heckel.ntfy.util.maybeFileStat
import io.heckel.ntfy.util.topicShortUrl import io.heckel.ntfy.util.topicShortUrl
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import java.io.File import java.io.File
import java.util.*
/** /**
* Deletes notifications marked for deletion and attachments for deleted notifications. * Deletes notifications marked for deletion and attachments for deleted notifications.
@ -32,9 +30,23 @@ class DeleteWorker(ctx: Context, params: WorkerParameters) : CoroutineWorker(ctx
override suspend fun doWork(): Result { override suspend fun doWork(): Result {
return withContext(Dispatchers.IO) { return withContext(Dispatchers.IO) {
deleteExpiredIcons() // Before notifications, so we will also catch manually deleted notifications // Run "expired icons" and "expired attachments" before notifications,
deleteExpiredAttachments() // Before notifications, so we will also catch manually deleted notifications // so we will also catch manually deleted notifications
try {
deleteExpiredIcons()
} catch (e: Exception) {
Log.w(TAG, "Failed to delete expired icons", e)
}
try {
deleteExpiredAttachments()
} catch (e: Exception) {
Log.w(TAG, "Failed to delete expired attachments", e)
}
try {
deleteExpiredNotifications() deleteExpiredNotifications()
} catch (e: Exception) {
Log.w(TAG, "Failed to delete expired notifications", e)
}
return@withContext Result.success() return@withContext Result.success()
} }
} }
@ -69,14 +81,9 @@ class DeleteWorker(ctx: Context, params: WorkerParameters) : CoroutineWorker(ctx
Log.d(TAG, "Deleting icons for deleted notifications") Log.d(TAG, "Deleting icons for deleted notifications")
val repository = Repository.getInstance(applicationContext) val repository = Repository.getInstance(applicationContext)
val activeIconUris = repository.getActiveIconUris() val activeIconUris = repository.getActiveIconUris()
val activeIconFilenames = activeIconUris.mapNotNull { val activeIconFilenames = activeIconUris
try { .mapNotNull { maybeFileStat(applicationContext, it)?.filename }
fileStat(applicationContext, Uri.parse(it)).filename .toSet()
} catch (e: Exception) {
Log.w(TAG, "Unable to stat file $it", e)
null
}
}.toSet()
val iconDir = File(applicationContext.cacheDir, DownloadIconWorker.ICON_CACHE_DIR) val iconDir = File(applicationContext.cacheDir, DownloadIconWorker.ICON_CACHE_DIR)
val allIconFilenames = iconDir.listFiles()?.map{ file -> file.name }.orEmpty() val allIconFilenames = iconDir.listFiles()?.map{ file -> file.name }.orEmpty()
val filenamesToDelete = allIconFilenames.minus(activeIconFilenames) val filenamesToDelete = allIconFilenames.minus(activeIconFilenames)

View file

@ -2,6 +2,7 @@ Bug fixes + maintenance:
* Upgrade Android dependencies to SDK 33 (no ticket) * Upgrade Android dependencies to SDK 33 (no ticket)
* Android 13: Ask for permission to post notifications (#508) * Android 13: Ask for permission to post notifications (#508)
* Remove timestamp when copying message text (#471, thanks to @wunter8) * Remove timestamp when copying message text (#471, thanks to @wunter8)
* Fix auto-delete if some icons do not exist anymore (#506)
Additional translations: Additional translations:
* Korean (thanks to @YJSofta0f97461d82447ac) * Korean (thanks to @YJSofta0f97461d82447ac)