end
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import type { RealEstateArticle } from "../generated/prisma";
|
||||
import type { NaverRealEstate } from "./naver.service";
|
||||
import prisma from "../lib/prisma";
|
||||
import { prisma } from "../../lib/prisma";
|
||||
import pLimit from "p-limit";
|
||||
|
||||
export class RankingService {
|
||||
@@ -11,9 +11,12 @@ export class RankingService {
|
||||
async updateRanking(articles: RealEstateArticle[], checkDate: Date) {
|
||||
const { token, cookie } = await this.naver.getRankingToken();
|
||||
|
||||
const limit = pLimit(20);
|
||||
const tasks = articles.map((article) => {
|
||||
return limit(async () => {
|
||||
for (let i = 0; i < articles.length; i += 20) {
|
||||
const batch = articles.slice(i, i + 20);
|
||||
|
||||
console.log(`🚀 ${i + 1} ~ ${i + batch.length}번 매물 처리 시작`);
|
||||
|
||||
const tasks = batch.map(async (article) => {
|
||||
try {
|
||||
// cortarNo, lgeo 확인
|
||||
if (!article.cortarNo || !article.lgeo) {
|
||||
@@ -24,8 +27,10 @@ export class RankingService {
|
||||
article.lgeo = lgeo;
|
||||
}
|
||||
|
||||
// 랭킹 조회 및 업데이트
|
||||
// 랭킹 조회
|
||||
const ranking = await this.naver.getRanking(article, token, cookie);
|
||||
|
||||
// DB 업데이트
|
||||
await prisma.realEstateArticle.update({
|
||||
where: { id: article.id },
|
||||
data: {
|
||||
@@ -34,17 +39,23 @@ export class RankingService {
|
||||
},
|
||||
});
|
||||
|
||||
console.log(
|
||||
`✅ ${article.articleNumber} - 랭킹 업데이트 완료: ${ranking}위`
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(`❌ updateRanking 오류:`, error);
|
||||
console.log(`✅ ${article.articleNumber} - 랭킹 완료 : ${ranking}`);
|
||||
} catch (err) {
|
||||
console.error(`❌ 오류: ${article.articleNumber}`, err);
|
||||
}
|
||||
});
|
||||
});
|
||||
console.log("🔹 예약된 작업 수:", tasks.length);
|
||||
const results = await Promise.allSettled(tasks);
|
||||
console.log("🟢 모든 limit 작업 완료:", results.length);
|
||||
|
||||
// **이 10개(또는 마지막 batch)가 모두 끝날 때까지 기다림**
|
||||
await Promise.allSettled(tasks);
|
||||
|
||||
// 다음 배치가 남아있다면 딜레이
|
||||
if (i + 20 < articles.length) {
|
||||
console.log(`⏸ 10개 처리 완료 → 10초 휴식`);
|
||||
await Bun.sleep(5000);
|
||||
}
|
||||
}
|
||||
|
||||
console.log("🎉 전체 랭킹 업데이트 완료!");
|
||||
await Bun.sleep(100);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user