Skip to content

Commit 612eae8

Browse files
committed
⚡ 크롤러 fetch retry 구현
1 parent 3c8417f commit 612eae8

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/main/kotlin/com/wafflestudio/team2server/crawler/BaseCrawler.kt

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,25 @@ abstract class BaseCrawler(
6868
}
6969
}
7070

71-
protected fun fetch(url: String): Document =
72-
Jsoup
73-
.connect(url)
74-
.userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
75-
.timeout(10000)
76-
.get()
71+
protected fun fetch(url: String): Document {
72+
var lastException: Exception? = null
73+
74+
repeat(3) { attempt ->
75+
try {
76+
return Jsoup
77+
.connect(url)
78+
.timeout(10000)
79+
.get()
80+
} catch (e: Exception) {
81+
println("$url ${attempt + 1}th request failed")
82+
lastException = e
83+
Thread.sleep(1000)
84+
}
85+
}
86+
87+
// If we reach this point, all 3 attempts failed
88+
throw lastException ?: Exception("Unknown error fetching URL: $url")
89+
}
7790

7891
protected fun updateExecutionTime() {
7992
try {

src/main/kotlin/com/wafflestudio/team2server/crawler/controller/CrawlerController.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class CrawlerController(
2525
?: return ResponseEntity.notFound().build()
2626

2727
try {
28-
targetCrawler.crawl()
28+
targetCrawler.runScheduled()
2929
return ResponseEntity.ok("크롤러($crawlerCode) 실행 완료!")
3030
} catch (e: Exception) {
3131
return ResponseEntity.internalServerError().body(" 실행 실패: ${e.message}")

0 commit comments

Comments
 (0)