-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
326de14
commit 365b127
Showing
30 changed files
with
814 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 15 additions & 2 deletions
17
src/main/kotlin/kr/galaxyhub/sc/api/v1/translation/dto/TranslationRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,20 @@ | ||
package kr.galaxyhub.sc.api.v1.translation.dto | ||
|
||
import java.util.UUID | ||
import kr.galaxyhub.sc.news.domain.Language | ||
import kr.galaxyhub.sc.translation.application.dto.TranslationCommand | ||
import kr.galaxyhub.sc.translation.domain.TranslatorProvider | ||
|
||
data class TranslationRequest( | ||
val destinationLanguage: Language | ||
) | ||
val sourceLanguage: Language, | ||
val targetLanguage: Language, | ||
val translatorProvider: TranslatorProvider, | ||
) { | ||
|
||
fun toCommand(newsId: UUID) = TranslationCommand( | ||
newsId = newsId, | ||
sourceLanguage = sourceLanguage, | ||
targetLanguage = targetLanguage, | ||
translatorProvider = translatorProvider | ||
) | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/kotlin/kr/galaxyhub/sc/news/application/NewsAppendContentEventListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package kr.galaxyhub.sc.news.application | ||
|
||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import kr.galaxyhub.sc.news.application.dto.NewsAppendContentEvent | ||
import kr.galaxyhub.sc.news.domain.NewsRepository | ||
import kr.galaxyhub.sc.news.domain.getOrThrow | ||
import org.springframework.context.event.EventListener | ||
import org.springframework.stereotype.Component | ||
import org.springframework.transaction.annotation.Transactional | ||
|
||
private val log = KotlinLogging.logger {} | ||
|
||
/** | ||
* TranslationCommandService.translate 메시드 호출 시 News가 이미 생성되어 있으므로 예외 발생 가능성은 없음 | ||
* 하지만 혹시 모를 상황에 error 로그 남김 | ||
*/ | ||
@Component | ||
@Transactional | ||
class NewsAppendContentEventListener( | ||
private val newsRepository: NewsRepository | ||
) { | ||
|
||
@EventListener | ||
fun appendContent(event: NewsAppendContentEvent) { | ||
runCatching { | ||
newsRepository.getOrThrow(event.newsId) | ||
}.onSuccess { | ||
it.addContent(event.content) | ||
}.onFailure { | ||
log.error { "뉴스에 컨텐츠를 추가하는 중 예외가 발생했습니다. ${it.message}" } | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/kotlin/kr/galaxyhub/sc/news/application/dto/NewsAppendContentEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package kr.galaxyhub.sc.news.application.dto | ||
|
||
import java.util.UUID | ||
import kr.galaxyhub.sc.news.domain.Content | ||
|
||
data class NewsAppendContentEvent( | ||
val newsId: UUID, | ||
val translateProgressionId: UUID, | ||
val content: Content | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
package kr.galaxyhub.sc.news.domain | ||
|
||
enum class Language { | ||
ENGLISH, | ||
KOREAN, | ||
enum class Language( | ||
val shortName: String, | ||
) { | ||
|
||
ENGLISH("EN"), | ||
KOREAN("KO"), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...n/kotlin/kr/galaxyhub/sc/translation/application/TranslationAppendContentEventListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package kr.galaxyhub.sc.translation.application | ||
|
||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import kr.galaxyhub.sc.news.application.dto.NewsAppendContentEvent | ||
import kr.galaxyhub.sc.translation.application.dto.TranslatorFailureEvent | ||
import kr.galaxyhub.sc.translation.domain.TranslationProgressionRepository | ||
import kr.galaxyhub.sc.translation.domain.getOrThrow | ||
import org.springframework.context.event.EventListener | ||
import org.springframework.stereotype.Component | ||
import org.springframework.transaction.annotation.Transactional | ||
|
||
private val log = KotlinLogging.logger {} | ||
|
||
/** | ||
* TranslationCommandService.translate 메서드의 트랜잭션이 API 호출보다 늦게 끝나면 예외 발생 가능성 있음. | ||
* 따라서 log를 error로 발생시킴 | ||
* 해당 로그 파악하면 Retry 전략 구성할 것 | ||
* 혹은 TranslationCommandService.translate에서 TransactionalEventListener를 받는곳에서 WebClient 실행 고려 | ||
*/ | ||
@Component | ||
@Transactional | ||
class TranslationAppendContentEventListener( | ||
private val translationProgressionRepository: TranslationProgressionRepository | ||
) { | ||
|
||
@EventListener | ||
fun changeTranslationProgressionComplete(event: NewsAppendContentEvent) { | ||
runCatching { | ||
translationProgressionRepository.getOrThrow(event.translateProgressionId) | ||
}.onSuccess { | ||
it.changeComplete() | ||
}.onFailure { | ||
log.error { "번역 진행 상황의 상태를 변경 중 예외가 발생했습니다. message=${it.message}" } | ||
} | ||
} | ||
|
||
@EventListener | ||
fun changeTranslationProgressionFailure(event: TranslatorFailureEvent) { | ||
runCatching { | ||
translationProgressionRepository.getOrThrow(event.translateProgressionId) | ||
}.onSuccess { | ||
it.changeFailure(event.message) | ||
}.onFailure { | ||
log.error { "번역 진행 상황의 상태를 변경 중 예외가 발생했습니다. message=${it.message}" } | ||
} | ||
} | ||
} | ||
|
33 changes: 29 additions & 4 deletions
33
src/main/kotlin/kr/galaxyhub/sc/translation/application/TranslationCommandService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,48 @@ | ||
package kr.galaxyhub.sc.translation.application | ||
|
||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import java.util.UUID | ||
import kr.galaxyhub.sc.common.support.validate | ||
import kr.galaxyhub.sc.news.application.dto.NewsAppendContentEvent | ||
import kr.galaxyhub.sc.news.domain.NewsRepository | ||
import kr.galaxyhub.sc.news.domain.getByDetailByIdAndLanguage | ||
import kr.galaxyhub.sc.translation.application.dto.TranslationCommand | ||
import kr.galaxyhub.sc.translation.application.dto.TranslatorFailureEvent | ||
import kr.galaxyhub.sc.translation.domain.TranslateProgression | ||
import kr.galaxyhub.sc.translation.domain.TranslationProgressionRepository | ||
import kr.galaxyhub.sc.translation.domain.TranslatorClients | ||
import org.springframework.context.ApplicationEventPublisher | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
|
||
private val log = KotlinLogging.logger {} | ||
|
||
@Service | ||
@Transactional | ||
class TranslationCommandService( | ||
private val translationProgressionRepository: TranslationProgressionRepository | ||
private val translationProgressionRepository: TranslationProgressionRepository, | ||
private val translatorClients: TranslatorClients, | ||
private val newsRepository: NewsRepository, | ||
private val eventPublisher: ApplicationEventPublisher, | ||
) { | ||
|
||
fun translate(command: TranslationCommand): UUID { | ||
val newsId = command.newsId | ||
val destinationLanguage = command.destinationLanguage | ||
val translateProgression = TranslateProgression(newsId, destinationLanguage) | ||
val (newsId, sourceLanguage, targetLanguage, translatorProvider) = command | ||
val news = newsRepository.getByDetailByIdAndLanguage(newsId, sourceLanguage) | ||
validate(news.isSupportLanguage(targetLanguage)) { "이미 뉴스에 번역된 컨텐츠가 존재합니다. targetLanguage=$targetLanguage" } | ||
val content = news.getContentByLanguage(sourceLanguage) | ||
val translateProgression = TranslateProgression(newsId, sourceLanguage, targetLanguage, translatorProvider) | ||
translationProgressionRepository.save(translateProgression) | ||
val translatorClient = translatorClients.getClient(translatorProvider) | ||
translatorClient.requestTranslate(content, targetLanguage) | ||
.doOnError { | ||
log.warn { "뉴스 번역 요청이 실패하였습니다. newsId=${newsId}, translateProgressionId=${translateProgression.id}" } | ||
eventPublisher.publishEvent(TranslatorFailureEvent(translateProgression.id, it.message)) | ||
} | ||
.subscribe { | ||
log.info { "뉴스 번역 요청이 완료되었습니다. newsId=${newsId}, translateProgressionId=${translateProgression.id}" } | ||
eventPublisher.publishEvent(NewsAppendContentEvent(newsId, translateProgression.id, it)) | ||
} | ||
return translateProgression.id | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/kr/galaxyhub/sc/translation/application/dto/TranslatorFailureEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package kr.galaxyhub.sc.translation.application.dto | ||
|
||
import java.util.UUID | ||
|
||
data class TranslatorFailureEvent( | ||
val translateProgressionId: UUID, | ||
val message: String? | ||
) |
34 changes: 34 additions & 0 deletions
34
src/main/kotlin/kr/galaxyhub/sc/translation/config/TranslatorClientConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package kr.galaxyhub.sc.translation.config | ||
|
||
import kr.galaxyhub.sc.translation.domain.TranslatorClient | ||
import kr.galaxyhub.sc.translation.domain.TranslatorClients | ||
import kr.galaxyhub.sc.translation.infra.DeepLTranslatorClient | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.context.annotation.Profile | ||
import org.springframework.http.HttpHeaders | ||
import org.springframework.web.reactive.function.client.WebClient | ||
|
||
@Configuration | ||
class TranslatorClientConfig( | ||
@Value("\${galaxyhub.translator.deepl-api-key}") private val deepLApiKey: String, | ||
private val webClient: WebClient, | ||
) { | ||
|
||
@Bean | ||
@Profile("!test") | ||
fun deepLTranslatorClient(): TranslatorClient { | ||
return DeepLTranslatorClient( | ||
webClient.mutate() | ||
.baseUrl("https://api.deepl.com") | ||
.defaultHeader(HttpHeaders.AUTHORIZATION, "DeepL-Auth-Key $deepLApiKey") | ||
.build() | ||
) | ||
} | ||
|
||
@Bean | ||
fun translatorClients(translatorClients: List<TranslatorClient>): TranslatorClients { | ||
return TranslatorClients.from(translatorClients) | ||
} | ||
} |
Oops, something went wrong.