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,27 +35,21 @@ class MessageService : JobService() {
launch(CommonPool) {
val currentServer = currentServerRepository.get()
if (currentServer != null) {
params?.let {
try {
retrySendingMessages(currentServer)
retrySendingMessages(params, currentServer)
jobFinished(params, false)
} catch (ex: RocketChatException) {
Timber.e(ex)
jobFinished(params, true)
}
}
}
}
return true
}
private suspend fun retrySendingMessages(currentServer: String) {
private suspend fun retrySendingMessages(params: JobParameters?, currentServer: String) {
val temporaryMessages = messageRepository.getAllUnsent()
.sortedWith(compareBy(Message::timestamp))
if (temporaryMessages.isNotEmpty()) {
val connectionManager = factory.create(currentServer)
val client = connectionManager.client
temporaryMessages.forEach { message ->
try {
client.sendMessage(
message = message.message,
messageId = message.id,
......@@ -65,6 +59,14 @@ class MessageService : JobService() {
alias = message.senderAlias
)
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