|
1 | 1 | package site.techmoa.batch.rss.usecase |
2 | 2 |
|
3 | 3 | import org.slf4j.LoggerFactory |
| 4 | +import org.springframework.beans.factory.annotation.Qualifier |
4 | 5 | import org.springframework.stereotype.Component |
5 | | -import org.springframework.transaction.annotation.Transactional |
| 6 | +import site.techmoa.batch.rss.domain.model.Article |
| 7 | +import site.techmoa.batch.rss.domain.model.Blog |
6 | 8 | import site.techmoa.batch.rss.domain.model.BlogStatus |
7 | 9 | import site.techmoa.batch.rss.port.ArticlePort |
8 | 10 | import site.techmoa.batch.rss.port.BlogPort |
9 | 11 | import site.techmoa.batch.rss.support.RssClient |
10 | 12 | import site.techmoa.batch.rss.trigger.CollectRssUseCase |
| 13 | +import java.util.concurrent.CompletableFuture |
| 14 | +import java.util.concurrent.Executor |
| 15 | +import java.util.concurrent.atomic.AtomicInteger |
11 | 16 |
|
12 | 17 | @Component |
13 | 18 | class CollectByRssReaderUseCase( |
14 | 19 | private val rssClient: RssClient, |
15 | 20 | private val articlePort: ArticlePort, |
16 | | - private val blogPort: BlogPort |
| 21 | + private val blogPort: BlogPort, |
| 22 | + @Qualifier("rssFetchExecutor") private val fetchExecutor: Executor, |
17 | 23 | ) : CollectRssUseCase { |
18 | 24 |
|
19 | 25 | private val log = LoggerFactory.getLogger(this.javaClass) |
20 | 26 |
|
21 | | - @Transactional |
| 27 | + private data class FetchResult( |
| 28 | + val blogId: Long, |
| 29 | + val blogName: String, |
| 30 | + val articles: List<Article>, |
| 31 | + val error: Throwable? |
| 32 | + ) |
| 33 | + |
22 | 34 | override fun execute() { |
23 | 35 | // 1. 구독 블로그 조회 |
24 | | - log.info("[${this.javaClass.simpleName}] Select Blogs List") |
| 36 | + log.info("Select Blogs List") |
25 | 37 | val blogs = blogPort.findAllBy(BlogStatus.ACTIVE) |
| 38 | + if (blogs.isEmpty()) return |
26 | 39 |
|
27 | 40 | // 2. 각 블로그 RSS에서 아티클 수집 |
28 | | - log.info("[${this.javaClass.simpleName}] Start Collecting Articles from RSS") |
29 | | - val articles = blogs.flatMap { rssClient.fetch(it) } |
| 41 | + log.info("Start Collecting Articles from RSS") |
| 42 | + val results = fetchWithLimitedConcurrency(blogs) |
| 43 | + |
| 44 | + val articles = results |
| 45 | + .filter { it.error == null } |
| 46 | + .flatMap { it.articles } |
| 47 | + |
| 48 | + val failedCount = results.count { it.error != null } |
| 49 | + if (failedCount > 0) { |
| 50 | + log.warn( |
| 51 | + "RSS collect partial failure. total=${results.size}, failed=$failedCount", |
| 52 | + ) |
| 53 | + } |
30 | 54 |
|
31 | 55 | // 3. 아티클 저장 |
32 | | - log.info("[${this.javaClass.simpleName}] Save New Articles") |
| 56 | + log.info("Save New Articles") |
33 | 57 | articlePort.saveAllIgnoringDuplicates(articles) |
34 | 58 | } |
| 59 | + |
| 60 | + private fun fetchWithLimitedConcurrency(blogs: List<Blog>): List<FetchResult> { |
| 61 | + val total = blogs.size |
| 62 | + val failures = AtomicInteger(0) |
| 63 | + |
| 64 | + val futures = blogs.map { blog -> |
| 65 | + CompletableFuture.supplyAsync({ fetchSingleBlog(blog, failures) }, fetchExecutor) |
| 66 | + .whenComplete { _, ex -> |
| 67 | + log.info("Future done blogId={}", blog.id) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + CompletableFuture.allOf(*futures.toTypedArray()).join() |
| 72 | + |
| 73 | + val results = futures.map { it.join() } |
| 74 | + log.info( |
| 75 | + "RSS collect finished. total=$total, success=${total - failures.get()}, failed=${failures.get()}", |
| 76 | + ) |
| 77 | + |
| 78 | + return results |
| 79 | + } |
| 80 | + |
| 81 | + private fun fetchSingleBlog( |
| 82 | + blog: Blog, |
| 83 | + failures: AtomicInteger, |
| 84 | + ): FetchResult { |
| 85 | + return try { |
| 86 | + FetchResult( |
| 87 | + blogId = blog.id, |
| 88 | + blogName = blog.name, |
| 89 | + articles = rssClient.fetch(blog), |
| 90 | + error = null, |
| 91 | + ) |
| 92 | + } catch (ex: Throwable) { |
| 93 | + failures.incrementAndGet() |
| 94 | + log.warn( |
| 95 | + "Failed to collect RSS. blogId=${blog.id}, blogName=${blog.name}", |
| 96 | + ex, |
| 97 | + ) |
| 98 | + FetchResult( |
| 99 | + blogId = blog.id, |
| 100 | + blogName = blog.name, |
| 101 | + articles = emptyList(), |
| 102 | + error = ex, |
| 103 | + ) |
| 104 | + } |
| 105 | + } |
35 | 106 | } |
0 commit comments