File tree Expand file tree Collapse file tree 2 files changed +20
-7
lines changed
src/main/kotlin/com/wafflestudio/team2server/crawler Expand file tree Collapse file tree 2 files changed +20
-7
lines changed Original file line number Diff line number Diff 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 {
Original file line number Diff line number Diff 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} " )
You can’t perform that action at this time.
0 commit comments