fix: duplicate message send bug on attachments

This commit is contained in:
jubb
2021-07-05 15:00:32 +10:00
parent f7bec07503
commit f4e5e5e36a
3 changed files with 20 additions and 3 deletions

View File

@@ -92,7 +92,7 @@ class AttachmentDownloadJob(val attachmentID: Long, val databaseMessageID: Long)
}
private fun handleSuccess() {
Log.w(AttachmentUploadJob.TAG, "Attachment downloaded successfully.")
Log.w("AttachmentDownloadJob", "Attachment downloaded successfully.")
delegate?.handleJobSucceeded(this)
}

View File

@@ -23,6 +23,7 @@ class JobQueue : JobDelegate {
private val attachmentDispatcher = Executors.newFixedThreadPool(2).asCoroutineDispatcher()
private val scope = GlobalScope + SupervisorJob()
private val queue = Channel<Job>(UNLIMITED)
private val pendingJobIds = mutableSetOf<String>()
val timer = Timer()
@@ -86,6 +87,19 @@ class JobQueue : JobDelegate {
MessagingModuleConfiguration.shared.storage.persistJob(job)
}
fun resumePendingSendMessage(job: Job) {
val id = job.id ?: run {
Log.e("Loki", "tried to resume pending send job with no ID")
return
}
if (!pendingJobIds.add(id)) {
Log.e("Loki","tried to re-queue pending/in-progress job")
return
}
queue.offer(job)
Log.d("Loki", "resumed pending send message $id")
}
fun resumePendingJobs() {
if (hasResumedPendingJobs) {
Log.d("Loki", "resumePendingJobs() should only be called once.")
@@ -120,6 +134,7 @@ class JobQueue : JobDelegate {
override fun handleJobSucceeded(job: Job) {
val jobId = job.id ?: return
MessagingModuleConfiguration.shared.storage.markJobAsSucceeded(jobId)
pendingJobIds.remove(jobId)
}
override fun handleJobFailed(job: Job, error: Exception) {
@@ -169,4 +184,7 @@ class JobQueue : JobDelegate {
val maxBackoff = (10 * 60).toDouble() // 10 minutes
return (1000 * 0.25 * min(maxBackoff, (2.0).pow(job.failureCount))).roundToLong()
}
private fun Job.isSend() = this is MessageSendJob || this is AttachmentUploadJob
}