Optimize more during low disk situations.

This commit is contained in:
Cody Henthorne
2025-11-20 14:15:45 -05:00
committed by jeffrey-signal
parent 8eef29cd77
commit 872c7c5ce2
2 changed files with 13 additions and 3 deletions

View File

@@ -1112,7 +1112,7 @@ class AttachmentTable(
* Marking offloaded only clears the strong references to the on disk file and clears other local file data like hashes. * Marking offloaded only clears the strong references to the on disk file and clears other local file data like hashes.
* Another operation must run to actually delete the data from disk. See [deleteAbandonedAttachmentFiles]. * Another operation must run to actually delete the data from disk. See [deleteAbandonedAttachmentFiles].
*/ */
fun markEligibleAttachmentsAsOptimized() { fun markEligibleAttachmentsAsOptimized(minimumAge: Duration = 30.days) {
val now = System.currentTimeMillis() val now = System.currentTimeMillis()
val subSelect = """ val subSelect = """
@@ -1136,7 +1136,7 @@ class AttachmentTable(
) )
AND AND
( (
${MessageTable.TABLE_NAME}.${MessageTable.DATE_RECEIVED} < ${now - 30.days.inWholeMilliseconds} ${MessageTable.TABLE_NAME}.${MessageTable.DATE_RECEIVED} < ${now - minimumAge.inWholeMilliseconds}
) )
""" """

View File

@@ -5,6 +5,7 @@
package org.thoughtcrime.securesms.jobs package org.thoughtcrime.securesms.jobs
import org.signal.core.util.DiskUtil
import org.signal.core.util.logging.Log import org.signal.core.util.logging.Log
import org.thoughtcrime.securesms.database.SignalDatabase import org.thoughtcrime.securesms.database.SignalDatabase
import org.thoughtcrime.securesms.dependencies.AppDependencies import org.thoughtcrime.securesms.dependencies.AppDependencies
@@ -51,7 +52,16 @@ class OptimizeMediaJob private constructor(parameters: Parameters) : Job(paramet
RestoreAttachmentJob.Queues.OFFLOAD_RESTORE.forEach { queue -> AppDependencies.jobManager.add(CheckRestoreMediaLeftJob(queue)) } RestoreAttachmentJob.Queues.OFFLOAD_RESTORE.forEach { queue -> AppDependencies.jobManager.add(CheckRestoreMediaLeftJob(queue)) }
Log.i(TAG, "Optimizing media in the db") Log.i(TAG, "Optimizing media in the db")
SignalDatabase.attachments.markEligibleAttachmentsAsOptimized()
val available = DiskUtil.getAvailableSpace(context).bytes.toFloat()
val total = DiskUtil.getTotalDiskSize(context).bytes.toFloat()
val remaining = (total - available) / total * 100
val minimumAge = if (remaining > 5f) 30.days else 15.days
Log.i(TAG, "${"%.1f".format(remaining)}% storage available, not optimizing the last $minimumAge of attachments")
SignalDatabase.attachments.markEligibleAttachmentsAsOptimized(minimumAge)
Log.i(TAG, "Deleting abandoned attachment files") Log.i(TAG, "Deleting abandoned attachment files")
val count = SignalDatabase.attachments.deleteAbandonedAttachmentFiles() val count = SignalDatabase.attachments.deleteAbandonedAttachmentFiles()