Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix auto-translation errors, drop some handled exceptions (sentry) #2040

Merged
merged 3 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ When no languages provided, it translates only untranslated languages."""
throw BadRequestException(Message.CANNOT_TRANSLATE_BASE_LANGUAGE)
}

autoTranslationService.autoTranslateSync(
autoTranslationService.autoTranslateSyncWithRetry(
key = key,
forcedLanguageTags = languages?.toList(),
useTranslationMemory = useTranslationMemory ?: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ class BatchJobStateProvider(
}

fun getCachedJobIds(): MutableSet<Long> {
return getStatesMap().keys
val keys = getStatesMap().keys
// redisson defers the access to the key set, so it was throwing NoSuchElementException when iterating over keys
// so let's rather copy the set
return keys.toMutableSet()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.tolgee.component

import io.sentry.Hint
import io.sentry.SentryEvent
import io.sentry.SentryOptions
import org.springframework.stereotype.Component

@Component
class SentryBeforeSendCallback : SentryOptions.BeforeSendCallback {
override fun execute(event: SentryEvent, hint: Hint): SentryEvent? {
if (event.containsMessage("Failed to send message to MessageChannel")) {
return null
}

if (event.containsExceptionOfType("FailedDontRequeueException")) {
return null
}

if (event.containsExceptionOfType("ClientAbortException")) {
return null
}

if (event.containsMessage("Cannot render error page for request [/websocket")) return null

return event
}

private fun SentryEvent.containsMessage(string: String): Boolean {
return message?.formatted?.contains(string) == true
}

private fun SentryEvent.containsExceptionOfType(type: String) =
exceptions?.any { it.type?.contains(type) == true } == true
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ abstract class ExceptionWithMessage(
this.tolgeeMessage = message
}

constructor(message: Message) : this(null, null) {
constructor(message: Message) : this(message.code, null) {
this.tolgeeMessage = message
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ import io.tolgee.repository.AutoTranslationConfigRepository
import io.tolgee.security.authentication.AuthenticationFacade
import io.tolgee.service.LanguageService
import io.tolgee.service.machineTranslation.MtService
import io.tolgee.util.executeInNewTransaction
import io.tolgee.util.tryUntilItDoesntBreakConstraint
import jakarta.persistence.EntityManager
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import org.springframework.transaction.PlatformTransactionManager

@Service
class AutoTranslationService(
Expand All @@ -30,7 +33,8 @@ class AutoTranslationService(
private val languageService: LanguageService,
private val batchJobService: BatchJobService,
private val authenticationFacade: AuthenticationFacade,
private val entityManager: EntityManager
private val entityManager: EntityManager,
private val transactionManager: PlatformTransactionManager
) {
val logger: Logger = LoggerFactory.getLogger(this::class.java)

Expand Down Expand Up @@ -127,6 +131,20 @@ class AutoTranslationService(
return null
}

fun autoTranslateSyncWithRetry(
key: Key,
forcedLanguageTags: List<String>? = null,
useTranslationMemory: Boolean? = null,
useMachineTranslation: Boolean? = null,
isBatch: Boolean,
) {
tryUntilItDoesntBreakConstraint(maxRepeats = 10) {
executeInNewTransaction(transactionManager) {
autoTranslateSync(key, forcedLanguageTags, useTranslationMemory, useMachineTranslation, isBatch)
}
}
}

fun autoTranslateSync(
key: Key,
forcedLanguageTags: List<String>? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import jakarta.persistence.PersistenceException
import org.springframework.dao.CannotAcquireLockException
import org.springframework.dao.DataIntegrityViolationException

inline fun <T> tryUntilItDoesntBreakConstraint(fn: () -> T): T {
inline fun <T> tryUntilItDoesntBreakConstraint(maxRepeats: Int = 100, fn: () -> T): T {
var exception: Exception? = null
var repeats = 0
for (it in 1..100) {
for (it in 1..maxRepeats) {
try {
return fn()
} catch (e: Exception) {
Expand All @@ -16,6 +16,7 @@ inline fun <T> tryUntilItDoesntBreakConstraint(fn: () -> T): T {
repeats++
exception = e
}

else -> throw e
}
}
Expand Down
Loading