mirror of
https://github.com/headporter81/specialsource-homepage-backend.git
synced 2026-12-06 07:41:09 +09:00
feat : 스크래핑 주기를 10~20분 사이 랜덤으로 변경
SpecialSource Backend CI/CD / build-and-deploy (push) Successful in 1m15s
SpecialSource Backend CI/CD / build-and-deploy (push) Successful in 1m15s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b4ce841613
commit
7813053122
2 files changed
+27
-7
No files matched your search
@@ -2,15 +2,19 @@ package company.specialsource.scraper;
|
|||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.scheduling.annotation.SchedulingConfigurer;
|
||||||
|
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class ScrapingScheduler {
|
public class ScrapingScheduler implements SchedulingConfigurer {
|
||||||
|
|
||||||
// 사이트에 부담을 주지 않기 위해 게시판별 요청 사이 최소 간격을 둔다.
|
// 사이트에 부담을 주지 않기 위해 게시판별 요청 사이 최소 간격을 둔다.
|
||||||
private static final long DELAY_BETWEEN_BOARDS_MS = 1500;
|
private static final long DELAY_BETWEEN_BOARDS_MS = 1500;
|
||||||
@@ -18,10 +22,25 @@ public class ScrapingScheduler {
|
|||||||
private final BoardScraperService boardScraperService;
|
private final BoardScraperService boardScraperService;
|
||||||
private final ScrapedPostSyncService scrapedPostSyncService;
|
private final ScrapedPostSyncService scrapedPostSyncService;
|
||||||
|
|
||||||
@Scheduled(
|
@Value("${scraping.initial-delay-ms:10000}")
|
||||||
initialDelayString = "${scraping.initial-delay-ms:10000}",
|
private long initialDelayMs;
|
||||||
fixedDelayString = "${scraping.interval-ms:600000}"
|
@Value("${scraping.interval-min-ms:600000}")
|
||||||
)
|
private long intervalMinMs;
|
||||||
|
@Value("${scraping.interval-max-ms:1200000}")
|
||||||
|
private long intervalMaxMs;
|
||||||
|
|
||||||
|
// @Scheduled는 고정 간격만 되므로, 실행이 끝날 때마다 다음 실행 시각을 min~max 사이 랜덤으로 정한다.
|
||||||
|
@Override
|
||||||
|
public void configureTasks(ScheduledTaskRegistrar registrar) {
|
||||||
|
registrar.addTriggerTask(this::scrapeAllBoards, ctx -> {
|
||||||
|
Instant last = ctx.lastCompletion();
|
||||||
|
if (last == null) return Instant.now().plusMillis(initialDelayMs);
|
||||||
|
long delay = ThreadLocalRandom.current().nextLong(intervalMinMs, intervalMaxMs + 1);
|
||||||
|
log.info("[스크래핑] 다음 실행까지 {}분 {}초", delay / 60000, delay / 1000 % 60);
|
||||||
|
return last.plusMillis(delay);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public void scrapeAllBoards() {
|
public void scrapeAllBoards() {
|
||||||
for (BoardSource source : BoardSource.values()) {
|
for (BoardSource source : BoardSource.values()) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -40,7 +40,8 @@ jwt:
|
|||||||
|
|
||||||
scraping:
|
scraping:
|
||||||
initial-delay-ms: 10000 # 앱 기동 후 첫 스크래핑까지 대기 시간
|
initial-delay-ms: 10000 # 앱 기동 후 첫 스크래핑까지 대기 시간
|
||||||
interval-ms: 600000 # 게시판 전체 스크래핑 주기 (10분)
|
interval-min-ms: 600000 # 게시판 전체 스크래핑 주기: 이전 실행이 끝난 뒤 min~max 사이 랜덤 (10~20분)
|
||||||
|
interval-max-ms: 1200000
|
||||||
retention-days: 7 # 이 기간(scraped_at 기준)이 지난 게시물은 자동 삭제
|
retention-days: 7 # 이 기간(scraped_at 기준)이 지난 게시물은 자동 삭제
|
||||||
cleanup-initial-delay-ms: 60000 # 앱 기동 후 첫 정리 작업까지 대기 시간
|
cleanup-initial-delay-ms: 60000 # 앱 기동 후 첫 정리 작업까지 대기 시간
|
||||||
cleanup-interval-ms: 86400000 # 정리 작업 주기 (24시간)
|
cleanup-interval-ms: 86400000 # 정리 작업 주기 (24시간)
|
||||||
Reference in new issue
Block a user