feat(rag): 메인 트레이딩 루프 0초 블로킹을 위한 비동기 백그라운드 AI 분석 워커 큐 아키텍처 구축
This commit is contained in:
@@ -16,7 +16,12 @@ import dev.langchain4j.model.openai.OpenAiEmbeddingModel
|
|||||||
import dev.langchain4j.store.embedding.EmbeddingSearchRequest
|
import dev.langchain4j.store.embedding.EmbeddingSearchRequest
|
||||||
import dev.langchain4j.store.embedding.EmbeddingSearchResult
|
import dev.langchain4j.store.embedding.EmbeddingSearchResult
|
||||||
import dev.langchain4j.store.embedding.filter.MetadataFilterBuilder
|
import dev.langchain4j.store.embedding.filter.MetadataFilterBuilder
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.SupervisorJob
|
||||||
|
import kotlinx.coroutines.channels.Channel
|
||||||
import kotlinx.coroutines.coroutineScope
|
import kotlinx.coroutines.coroutineScope
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
import kotlinx.serialization.json.JsonPrimitive
|
import kotlinx.serialization.json.JsonPrimitive
|
||||||
import kotlinx.serialization.json.addJsonObject
|
import kotlinx.serialization.json.addJsonObject
|
||||||
@@ -53,9 +58,43 @@ import java.util.concurrent.TimeUnit
|
|||||||
object RagService {
|
object RagService {
|
||||||
val isSafetyBeltStockCodes = ConcurrentHashMap.newKeySet<String>()
|
val isSafetyBeltStockCodes = ConcurrentHashMap.newKeySet<String>()
|
||||||
|
|
||||||
|
private data class AiAnalysisTask(
|
||||||
|
val currentPrice: Double,
|
||||||
|
val technicalAnalyzer: TechnicalAnalyzer,
|
||||||
|
val stockName: String,
|
||||||
|
val stockCode: String,
|
||||||
|
val isWatering: Boolean,
|
||||||
|
val scores: InvestmentScores,
|
||||||
|
val financialStmt: FinancialStatement,
|
||||||
|
val tradingDecision: TradingDecision,
|
||||||
|
val finDuration: Long,
|
||||||
|
val techDuration: Long,
|
||||||
|
val totalStartTime: Long,
|
||||||
|
val result: TradingDecisionCallback
|
||||||
|
)
|
||||||
|
|
||||||
|
private val aiTaskChannel = Channel<AiAnalysisTask>(Channel.UNLIMITED)
|
||||||
|
private val aiScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
|
||||||
|
private val analyzingStocks = ConcurrentHashMap.newKeySet<String>()
|
||||||
|
|
||||||
|
init {
|
||||||
|
aiScope.launch {
|
||||||
|
for (task in aiTaskChannel) {
|
||||||
|
try {
|
||||||
|
processAiNewsAnalysis(task)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
println("❌ [RagService AI 워커] ${task.stockName} 분석 실패: ${e.message}")
|
||||||
|
} finally {
|
||||||
|
analyzingStocks.remove(task.stockCode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// (매일 아침 8시 30분 시스템 초기화 시 호출)
|
// (매일 아침 8시 30분 시스템 초기화 시 호출)
|
||||||
fun clearDailyCache() {
|
fun clearDailyCache() {
|
||||||
isSafetyBeltStockCodes.clear()
|
isSafetyBeltStockCodes.clear()
|
||||||
|
analyzingStocks.clear()
|
||||||
checkAndCleanOldIndices(50L) // 50MB 초과 시 오래된 인덱스 자동 정리
|
checkAndCleanOldIndices(50L) // 50MB 초과 시 오래된 인덱스 자동 정리
|
||||||
println("🧹 [System] 일일 재무 미달 캐시 및 루씬 DB 상태 점검 완료")
|
println("🧹 [System] 일일 재무 미달 캐시 및 루씬 DB 상태 점검 완료")
|
||||||
}
|
}
|
||||||
@@ -350,6 +389,56 @@ object RagService {
|
|||||||
return@coroutineScope
|
return@coroutineScope
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 🌟 [검문소 통과!] 1단계(재무), 2단계(기술)를 모두 통과한 진짜 매수 후보 종목
|
||||||
|
// 중복 분석 중인지 체크 후 백그라운드 AI 분석 큐(Worker)에 위임하여 메인 루프를 블로킹하지 않음
|
||||||
|
if (!analyzingStocks.add(stockCode)) {
|
||||||
|
println("⏳ [$stockName] 이미 백그라운드 AI 분석 대기열에서 진행 중입니다. (스킵)")
|
||||||
|
return@coroutineScope
|
||||||
|
}
|
||||||
|
|
||||||
|
println("🚀 [$stockName] 기술/재무 검문소 통과! 백그라운드 AI 분석 큐에 등록합니다. (메인 루프 즉시 복귀)")
|
||||||
|
|
||||||
|
aiTaskChannel.trySend(
|
||||||
|
AiAnalysisTask(
|
||||||
|
currentPrice = currentPrice,
|
||||||
|
technicalAnalyzer = technicalAnalyzer,
|
||||||
|
stockName = stockName,
|
||||||
|
stockCode = stockCode,
|
||||||
|
isWatering = isWatering,
|
||||||
|
scores = scores,
|
||||||
|
financialStmt = financialStmt,
|
||||||
|
tradingDecision = tradingDecision,
|
||||||
|
finDuration = finDuration,
|
||||||
|
techDuration = techDuration,
|
||||||
|
totalStartTime = totalStartTime,
|
||||||
|
result = result
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
} catch (e: Exception) {
|
||||||
|
e.printStackTrace()
|
||||||
|
println("❌ [$stockName] 1/2단계 분석 실패: ${e.message}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 백그라운드 비동기 AI 뉴스 분석 워커 (메인 루프와 완전 분리되어 30~40초 정밀 분석 수행)
|
||||||
|
*/
|
||||||
|
private suspend fun processAiNewsAnalysis(task: AiAnalysisTask) {
|
||||||
|
val stockName = task.stockName
|
||||||
|
val stockCode = task.stockCode
|
||||||
|
val totalStartTime = task.totalStartTime
|
||||||
|
val finDuration = task.finDuration
|
||||||
|
val techDuration = task.techDuration
|
||||||
|
val tradingDecision = task.tradingDecision
|
||||||
|
val scores = task.scores
|
||||||
|
val financialStmt = task.financialStmt
|
||||||
|
val result = task.result
|
||||||
|
|
||||||
|
try {
|
||||||
|
println("🧠 [AI 워커 가동] [$stockName] 뉴스 수집 및 심층 분석 시작...")
|
||||||
|
|
||||||
// [3단계] 뉴스 RAG 및 AI 분석
|
// [3단계] 뉴스 RAG 및 AI 분석
|
||||||
val ragStartTime = System.currentTimeMillis()
|
val ragStartTime = System.currentTimeMillis()
|
||||||
checkAndFetchRecentNews(stockName, stockCode)
|
checkAndFetchRecentNews(stockName, stockCode)
|
||||||
@@ -406,12 +495,13 @@ object RagService {
|
|||||||
val detailLog = "재무(${finDuration}ms), 기술(${techDuration}ms), 뉴스/AI(${ragAiDuration}ms), 전체(${totalDuration}ms)"
|
val detailLog = "재무(${finDuration}ms), 기술(${techDuration}ms), 뉴스/AI(${ragAiDuration}ms), 전체(${totalDuration}ms)"
|
||||||
TradingLogStore.addAnalyzer(stockName, stockCode, detailLog, true)
|
TradingLogStore.addAnalyzer(stockName, stockCode, detailLog, true)
|
||||||
println("$stockName[$stockCode] $detailLog")
|
println("$stockName[$stockCode] $detailLog")
|
||||||
|
|
||||||
|
// 콜백 호출 (globalCallback 실행 -> 조건 부합 시 매수 주문 발주)
|
||||||
result(finalDecision, true)
|
result(finalDecision, true)
|
||||||
|
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
println("❌ [$stockName] 분석 실패: ${e.message}")
|
println("❌ [$stockName] 백그라운드 AI 분석 실패: ${e.message}")
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user