Skip to content

Commit

Permalink
Merge pull request #117 from minseok1015/dev
Browse files Browse the repository at this point in the history
feat : 수동 스케줄러를 위한 task추가
  • Loading branch information
minseok1015 authored Aug 17, 2024
2 parents 9a80ba6 + 34fca34 commit 1b4a637
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/main/java/store/itpick/backend/controller/RankController.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,21 @@ public void updateZum() {
keywordService.performDailyTasksZum();
}

@GetMapping("/update/hourly")
public void updateHourly() {
rankService.performHourlyTasks();
}

@GetMapping("/update/daily")
public void updateDaily() {
rankService.performDailyTasks();
}

@GetMapping("/update/weekly")
public void updateWeekly() {
rankService.performWeeklyTasks();
}



@GetMapping("/naver")
Expand Down
105 changes: 104 additions & 1 deletion src/main/java/store/itpick/backend/service/RankService.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,73 @@
package store.itpick.backend.service;

import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.TimeoutException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import store.itpick.backend.common.exception.ReferenceException;
import store.itpick.backend.dto.rank.RankResponseDTO;
import store.itpick.backend.model.CommunityPeriod;
import store.itpick.backend.model.Keyword;
import store.itpick.backend.model.Reference;
import store.itpick.backend.model.rank.PeriodType;
import store.itpick.backend.repository.CommunityPeriodRepository;
import store.itpick.backend.repository.KeywordRepository;
import store.itpick.backend.util.Redis;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

import static store.itpick.backend.common.response.status.BaseExceptionResponseStatus.NO_Search_REFERENCE;


//관련자료 조회하는 Service
@Service
@Slf4j
public class RankService {
@Autowired
private KeywordRepository keywordRepository;

@Autowired
private CommunityPeriodRepository communityPeriodRepository;
@Autowired
private SeleniumService seleniumService;
@Autowired
private KeywordService keywordService;
@Autowired
private Redis redis;


// 최대 재시도 횟수와 재시도 간격 (초)
private static final int MAX_RETRIES = 5;
private static final int RETRY_DELAY_SECONDS = 5;

// 재시도 로직을 포함한 함수
private <T> T executeWithRetries(Callable<T> action, String actionName) {
for (int attempt = 0; attempt < MAX_RETRIES; attempt++) {
try {
return action.call(); // 작업 시도
} catch (TimeoutException e) {
log.warn("{} 시도 중 TimeoutException 발생, 재시도 중... ({}/{})", actionName, attempt + 1, MAX_RETRIES);
if (attempt == MAX_RETRIES - 1) {
log.error("모든 {} 시도 실패. 종료합니다.", actionName);
return null;
}
try {
TimeUnit.SECONDS.sleep(RETRY_DELAY_SECONDS);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
log.error("재시도 지연 중 InterruptedException 발생: {}", ie.getMessage());
return null;
}
} catch (Exception e) {
log.error("{} 작업 중 예기치 않은 오류 발생: {}", actionName, e.getMessage());
break;
}
}
return null;
}

public RankResponseDTO getReferenceByKeyword(String community, String period, String keyword) {
List<String> communitiesToCheck = "total".equals(community)
Expand Down Expand Up @@ -75,6 +119,65 @@ private void isValidReference(String newsTitle){
}
}

@Transactional
public void performHourlyTasks() {
try {
seleniumService.initDriver();
executeWithRetries(() -> seleniumService.useDriverForNaver("https://www.signal.bz/"), "Naver 데이터 수집");
executeWithRetries(() -> seleniumService.useDriverForMnate("https://m.nate.com/"), "Nate 데이터 수집");
executeWithRetries(() -> seleniumService.useDriverForZum("https://news.zum.com/"), "Zum 데이터 수집");
executeWithRetries(() -> seleniumService.useDriverForGoogle("https://trends.google.co.kr/trending/rss?geo=KR"), "Google 데이터 수집");
executeWithRetries(() -> seleniumService.useDriverForNamuwiki("https://blog.anteater-lab.link/namu-soup/"), "Namuwiki 데이터 수집");

/** 일간 통합 랭킹 저장 **/
redis.saveTotalRanking(PeriodType.BY_REAL_TIME);


} catch (Exception e) {
log.error("Error during hourly task", e);
}finally {
seleniumService.quitDriver();
}
}

// 18시에 실행하는 Daily 작업
@Transactional
public void performDailyTasks() {
log.info("Starting scheduled tasks...performing DailyTask");
performHourlyTasks(); // 매일 18시에 hourlyTask를 포함

/**
* Redis에서 일간 랭킹 생성, 계산
* **/

redis.saveDay();
redis.saveTotalRanking(PeriodType.BY_DAY);

/** DB에 있는 18시 검색어들을 Daily검색어로 Reference 참조할 수 있도록 함 **/
keywordService.performDailyTasksNate();
keywordService.performDailyTasksNaver();
keywordService.performDailyTasksZum();
keywordService.performDailyTasksGoogle();
keywordService.performDailyTasksNamuwiki();
log.info("Scheduled tasks completed DailyTask.");
}

@Transactional
public void performWeeklyTasks() {
log.info("Starting weekly task...");
/**
* Redis에서 주간 랭킹 계산, 생성
* **/
redis.saveWeek();
redis.saveTotalRanking(PeriodType.BY_WEEK);

/**
* DB에서 주간 랭킹 생성, 계산
* **/


log.info("Weekly task completed.");
}


}

0 comments on commit 1b4a637

Please sign in to comment.