Commit 480d5032 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Remove possible messages stuck with duplicate id for any reason from the resend job queue

parent df21f5a4
...@@ -35,36 +35,38 @@ class MessageService : JobService() { ...@@ -35,36 +35,38 @@ class MessageService : JobService() {
launch(CommonPool) { launch(CommonPool) {
val currentServer = currentServerRepository.get() val currentServer = currentServerRepository.get()
if (currentServer != null) { if (currentServer != null) {
params?.let { retrySendingMessages(params, currentServer)
try { jobFinished(params, false)
retrySendingMessages(currentServer)
jobFinished(params, false)
} catch (ex: RocketChatException) {
Timber.e(ex)
jobFinished(params, true)
}
}
} }
} }
return true return true
} }
private suspend fun retrySendingMessages(currentServer: String) { private suspend fun retrySendingMessages(params: JobParameters?, currentServer: String) {
val temporaryMessages = messageRepository.getAllUnsent() val temporaryMessages = messageRepository.getAllUnsent()
.sortedWith(compareBy(Message::timestamp)) .sortedWith(compareBy(Message::timestamp))
if (temporaryMessages.isNotEmpty()) { if (temporaryMessages.isNotEmpty()) {
val connectionManager = factory.create(currentServer) val connectionManager = factory.create(currentServer)
val client = connectionManager.client val client = connectionManager.client
temporaryMessages.forEach { message -> temporaryMessages.forEach { message ->
client.sendMessage( try {
message = message.message, client.sendMessage(
messageId = message.id, message = message.message,
roomId = message.roomId, messageId = message.id,
avatar = message.avatar, roomId = message.roomId,
attachments = message.attachments, avatar = message.avatar,
alias = message.senderAlias attachments = message.attachments,
) alias = message.senderAlias
messageRepository.save(message.copy(isTemporary = false)) )
messageRepository.save(message.copy(isTemporary = false))
} catch (ex: RocketChatException) {
Timber.e(ex)
if (ex.message?.contains("E11000", true) == true) {
// XXX: Temporary solution. We need proper error codes from the api.
messageRepository.save(message.copy(isTemporary = false))
}
jobFinished(params, true)
}
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment