| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- using Application.Abstractions.Data;
- using Microsoft.EntityFrameworkCore;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.Logging;
- using Microsoft.Extensions.Options;
- using SharedKernel;
- using Application.Helpers;
- namespace Infrastructure.StockData;
- /// <summary>
- /// SEIBro 외화증권 수집 (Wave 6, 최종) — 기본 08:20 KST(DerivSync 08:00 다음 슬롯), `Seibro:ForeignSync` 게이트.
- /// 전 단계가 외화(Foreign) 예산(Seibro:ForeignBudget, 실한도 20,000 의 80%=15,000)을 공유한다 — 국가×일 수십 콜이라 여유가 크다.
- ///
- /// a) ForeignCustodyNation: getNationFrsecCusInfo 날짜 스윕(STD_DT) × ForeignNations(설정) 3년 → (기준일,국가,종목구분) upsert.
- /// b) ForeignSettlementNation: getNationFrsecSetlInfo 날짜 스윕(SETL_DT) × ForeignNations 3년 → (결제일,국가,매매,종목구분) upsert.
- /// c) ForeignCustodySecurity: getSecnFrsecCusInfo 날짜 스윕(STD_DT) × ForeignIsins(설정, **기본 빈 → 스킵/대기**) → (기준일,ISIN) upsert.
- /// d) ForeignSettlementSecurity: getSecnFrsecSetlInfo 날짜 스윕(PROC_DT) × ForeignIsins(기본 빈) → (처리일,ISIN,매매) upsert.
- ///
- /// 외화 결제는 미국시간 T일 결제가 처리일 T+1 반영이라 결제일/처리일 스윕은 KrxBackfill(영업일·resumable)이 적절하다.
- /// ForeignIsins 가 빈 리스트면 종목별 2단계(c·d)는 콜 없이 정상 통과(대기). 실패/0행=정상 빈결과, HTTP·파싱 오류=경고+false(재시도), quota 소진=정상 종료(true). ApiKey 미설정 시 skip.
- /// </summary>
- internal sealed class SeibroForeignSyncService(
- IServiceScopeFactory scopeFactory,
- IHttpClientFactory httpClientFactory,
- SeibroQuota quota,
- IOptions<AppSettings> settings,
- ILogger<SeibroForeignSyncService> logger
- ) : DailyScheduledService(logger)
- {
- // 마커 job 키 (리뷰 결함 #1) — 결제 없는 영업일 재조회 회피. 모두 date-sweep (SweptDate).
- private const string CusNationJobKey = "foreign-cusnation"; // Discriminator=nation
- private const string SetlNationJobKey = "foreign-setlnation"; // Discriminator=nation
- private const string CusSecJobKey = "foreign-cussec"; // Discriminator=isin
- private const string SetlSecJobKey = "foreign-setlsec"; // Discriminator=isin
- protected override string JobName => "SeibroForeignSync";
- protected override TimeOnly TargetTime => ParseTime(settings.Value.Seibro.ForeignSyncTime, new TimeOnly(8, 20));
- protected override int MaxRetryCount => 2;
- protected override TimeSpan RetryDelay => TimeSpan.FromHours(2);
- protected override async Task<bool> RunOnceAsync(DateOnly todayKst, CancellationToken ct)
- {
- var cfg = settings.Value.Seibro;
- if (string.IsNullOrWhiteSpace(cfg.ApiKey))
- {
- Logger.LogWarning("[{Job}] Seibro:ApiKey 미설정 — 수집 skip", JobName);
- return true;
- }
- using var scope = scopeFactory.CreateScope();
- var db = scope.ServiceProvider.GetRequiredService<IAppDbContext>();
- var client = httpClientFactory.CreateClient(SeibroHttp.ClientName);
- var years = cfg.BackfillYears > 0 ? cfg.BackfillYears : 3;
- var startDate = todayKst.AddYears(-years);
- var nations = cfg.ForeignNations ?? [];
- var isins = cfg.ForeignIsins ?? [];
- var holidays = await db.MarketHoliday.AsNoTracking().Select(c => c.Date).ToListAsync(ct);
- var holidaySet = holidays.ToHashSet();
- try
- {
- // a) 국가별 보관 날짜 스윕 × 국가 (Foreign)
- if (!await SweepCustodyNationAsync(db, client, cfg, nations, holidaySet, startDate, todayKst, ct))
- {
- return true;
- }
- // b) 국가별 결제 날짜 스윕 × 국가 (Foreign)
- if (!await SweepSettlementNationAsync(db, client, cfg, nations, holidaySet, startDate, todayKst, ct))
- {
- return true;
- }
- // c) 종목별 보관 날짜 스윕 × ISIN (Foreign, ForeignIsins 빈 → 콜 0)
- if (!await SweepCustodySecurityAsync(db, client, cfg, isins, holidaySet, startDate, todayKst, ct))
- {
- return true;
- }
- // d) 종목별 결제 날짜 스윕 × ISIN (Foreign, ForeignIsins 빈 → 콜 0)
- await SweepSettlementSecurityAsync(db, client, cfg, isins, holidaySet, startDate, todayKst, ct);
- return true;
- }
- catch (Exception ex) when (ex is HttpRequestException or System.Xml.XmlException or FormatException)
- {
- Logger.LogWarning(ex, "[{Job}] SEIBro 호출/파싱 실패 — run 중단, {Delay} 후 재시도 (최대 {Max}회)", JobName, RetryDelay, MaxRetryCount);
- return false;
- }
- }
- /// <summary>
- /// a) 국가별 보관 날짜 스윕(STD_DT, 최신→과거 영업일) × ForeignNations — 미적재 (기준일,국가)만.
- /// getNationFrsecCusInfo(STD_DT,NATION_CD) 1콜당 종목구분 다수 행 upsert(UQ StdDt+NationCd+SecnTpcd). 예산 소진 시 false.
- /// </summary>
- private async Task<bool> SweepCustodyNationAsync(IAppDbContext db, HttpClient client, AppSettings.SeibroSection cfg, IReadOnlyList<string> nations, IReadOnlySet<DateOnly> holidays, DateOnly startDate, DateOnly today, CancellationToken ct)
- {
- if (nations.Count == 0)
- {
- Logger.LogInformation("[{Job}] getNationFrsecCusInfo 대상 국가 없음 (ForeignNations 비어 있음)", JobName);
- return true;
- }
- var fetched = 0;
- var first = true;
- for (var day = today; day >= startDate; day = day.AddDays(-1))
- {
- if (day.DayOfWeek is DayOfWeek.Saturday or DayOfWeek.Sunday || holidays.Contains(day))
- {
- continue; // 영업일 스윕 (외화 결제 T+1 반영 — KrxBackfill 판단)
- }
- foreach (var nation in nations)
- {
- ct.ThrowIfCancellationRequested();
- // 데이터 존재 OR 조회완료 마커(축=nation) → 스킵 (보관 없는 (일,국가)도 재조회 회피, 리뷰 결함 #1)
- if (await db.ForeignCustodyNation.AsNoTracking().AnyAsync(c => c.StdDt == day && c.NationCd == nation, ct) || await SeibroMarkers.IsSweptAsync(db, CusNationJobKey, day, nation, ct))
- {
- continue;
- }
- if (!quota.TryConsume(SeibroCategory.Foreign, 1, cfg.ForeignBudget))
- {
- Logger.LogWarning("[{Job}] 외화 예산({Budget}) 소진 — getNationFrsecCusInfo {Day}/{Nation} 부터 중단", JobName, cfg.ForeignBudget, day, nation);
- return false;
- }
- if (!first && cfg.DelayMs > 0)
- {
- await Task.Delay(cfg.DelayMs, ct);
- }
- first = false;
- var xml = await SeibroHttp.GetStringWithRetryAsync(client, cfg.BaseUrl, cfg.ApiKey, "getNationFrsecCusInfo", [new("STD_DT", day.ToString("yyyyMMdd")), new("NATION_CD", nation)], Logger, ct);
- var rows = SeibroNationFrsecCusParser.Parse(SeibroXml.Parse(xml));
- var (inserted, updated) = await SeibroForeignImport.UpsertCustodyNationAsync(db, day, nation, rows, ct);
- await SeibroMarkers.MarkSweptAsync(db, CusNationJobKey, day, nation, ct); // 0행 포함 조회완료 기록
- Logger.LogInformation("[{Job}] getNationFrsecCusInfo {Day}/{Nation} rows={Rows} — inserted={Inserted}, updated={Updated}", JobName, day, nation, rows.Count, inserted, updated);
- fetched++;
- }
- }
- Logger.LogInformation("[{Job}] 국가별 보관 스윕 완료 — 창=[{Start}~{End}], 국가={Nations}, fetch={Fetched}콜", JobName, startDate, today, nations.Count, fetched);
- return true;
- }
- /// <summary>
- /// b) 국가별 결제 날짜 스윕(SETL_DT, 최신→과거 영업일) × ForeignNations — 미적재 (결제일,국가)만.
- /// getNationFrsecSetlInfo(SETL_DT,NATION_CD) 1콜당 매매×종목구분 다수 행 upsert(UQ SetlDt+NationCd+IntlBizCacd+SecnTpcd). 예산 소진 시 false.
- /// </summary>
- private async Task<bool> SweepSettlementNationAsync(IAppDbContext db, HttpClient client, AppSettings.SeibroSection cfg, IReadOnlyList<string> nations, IReadOnlySet<DateOnly> holidays, DateOnly startDate, DateOnly today, CancellationToken ct)
- {
- if (nations.Count == 0)
- {
- return true;
- }
- var fetched = 0;
- var first = true;
- for (var day = today; day >= startDate; day = day.AddDays(-1))
- {
- if (day.DayOfWeek is DayOfWeek.Saturday or DayOfWeek.Sunday || holidays.Contains(day))
- {
- continue;
- }
- foreach (var nation in nations)
- {
- ct.ThrowIfCancellationRequested();
- // 데이터 존재 OR 조회완료 마커(축=nation) → 스킵 (결제 없는 (일,국가)도 재조회 회피, 리뷰 결함 #1)
- if (await db.ForeignSettlementNation.AsNoTracking().AnyAsync(c => c.SetlDt == day && c.NationCd == nation, ct) || await SeibroMarkers.IsSweptAsync(db, SetlNationJobKey, day, nation, ct))
- {
- continue;
- }
- if (!quota.TryConsume(SeibroCategory.Foreign, 1, cfg.ForeignBudget))
- {
- Logger.LogWarning("[{Job}] 외화 예산({Budget}) 소진 — getNationFrsecSetlInfo {Day}/{Nation} 부터 중단", JobName, cfg.ForeignBudget, day, nation);
- return false;
- }
- if (!first && cfg.DelayMs > 0)
- {
- await Task.Delay(cfg.DelayMs, ct);
- }
- first = false;
- var xml = await SeibroHttp.GetStringWithRetryAsync(client, cfg.BaseUrl, cfg.ApiKey, "getNationFrsecSetlInfo", [new("SETL_DT", day.ToString("yyyyMMdd")), new("NATION_CD", nation)], Logger, ct);
- var rows = SeibroNationFrsecSetlParser.Parse(SeibroXml.Parse(xml));
- var (inserted, updated) = await SeibroForeignImport.UpsertSettlementNationAsync(db, day, nation, rows, ct);
- await SeibroMarkers.MarkSweptAsync(db, SetlNationJobKey, day, nation, ct); // 0행 포함 조회완료 기록
- Logger.LogInformation("[{Job}] getNationFrsecSetlInfo {Day}/{Nation} rows={Rows} — inserted={Inserted}, updated={Updated}", JobName, day, nation, rows.Count, inserted, updated);
- fetched++;
- }
- }
- Logger.LogInformation("[{Job}] 국가별 결제 스윕 완료 — 창=[{Start}~{End}], 국가={Nations}, fetch={Fetched}콜", JobName, startDate, today, nations.Count, fetched);
- return true;
- }
- /// <summary>
- /// c) 종목별 보관 날짜 스윕(STD_DT, 최신→과거 영업일) × ForeignIsins — 미적재 (기준일,ISIN)만.
- /// getSecnFrsecCusInfo(STD_DT,ISIN) → (기준일,ISIN) upsert(첫 행). **ForeignIsins 기본 빈 → 콜 0(대기)**. 예산 소진 시 false.
- /// </summary>
- private async Task<bool> SweepCustodySecurityAsync(IAppDbContext db, HttpClient client, AppSettings.SeibroSection cfg, IReadOnlyList<string> isins, IReadOnlySet<DateOnly> holidays, DateOnly startDate, DateOnly today, CancellationToken ct)
- {
- if (isins.Count == 0)
- {
- Logger.LogInformation("[{Job}] getSecnFrsecCusInfo 대상 ISIN 없음 (ForeignIsins 비어 있음 — 종목별 보관 대기)", JobName);
- return true;
- }
- var fetched = 0;
- var first = true;
- for (var day = today; day >= startDate; day = day.AddDays(-1))
- {
- if (day.DayOfWeek is DayOfWeek.Saturday or DayOfWeek.Sunday || holidays.Contains(day))
- {
- continue;
- }
- foreach (var isin in isins)
- {
- ct.ThrowIfCancellationRequested();
- // 데이터 존재 OR 조회완료 마커(축=isin) → 스킵 (보관 없는 (일,ISIN)도 재조회 회피, 리뷰 결함 #1)
- if (await db.ForeignCustodySecurity.AsNoTracking().AnyAsync(c => c.StdDt == day && c.Isin == isin, ct) || await SeibroMarkers.IsSweptAsync(db, CusSecJobKey, day, isin, ct))
- {
- continue;
- }
- if (!quota.TryConsume(SeibroCategory.Foreign, 1, cfg.ForeignBudget))
- {
- Logger.LogWarning("[{Job}] 외화 예산({Budget}) 소진 — getSecnFrsecCusInfo {Day}/{Isin} 부터 중단", JobName, cfg.ForeignBudget, day, isin);
- return false;
- }
- if (!first && cfg.DelayMs > 0)
- {
- await Task.Delay(cfg.DelayMs, ct);
- }
- first = false;
- var xml = await SeibroHttp.GetStringWithRetryAsync(client, cfg.BaseUrl, cfg.ApiKey, "getSecnFrsecCusInfo", [new("STD_DT", day.ToString("yyyyMMdd")), new("ISIN", isin)], Logger, ct);
- var rows = SeibroSecnFrsecCusParser.Parse(SeibroXml.Parse(xml));
- var ok = await SeibroForeignImport.UpsertCustodySecurityAsync(db, day, isin, rows, ct);
- await SeibroMarkers.MarkSweptAsync(db, CusSecJobKey, day, isin, ct); // 0행 포함 조회완료 기록
- Logger.LogInformation("[{Job}] getSecnFrsecCusInfo {Day}/{Isin} rows={Rows} — upsert={Ok}", JobName, day, isin, rows.Count, ok);
- fetched++;
- }
- }
- Logger.LogInformation("[{Job}] 종목별 보관 스윕 완료 — 창=[{Start}~{End}], ISIN={Isins}, fetch={Fetched}콜", JobName, startDate, today, isins.Count, fetched);
- return true;
- }
- /// <summary>
- /// d) 종목별 결제 날짜 스윕(PROC_DT, 최신→과거 영업일) × ForeignIsins — 미적재 (처리일,ISIN)만.
- /// getSecnFrsecSetlInfo(PROC_DT,ISIN) 1콜당 매매 다수 행 upsert(UQ ProcDt+Isin+IntlBizCacd). **ForeignIsins 기본 빈 → 콜 0(대기)**. 예산 소진 시 즉시 종료.
- /// </summary>
- private async Task SweepSettlementSecurityAsync(IAppDbContext db, HttpClient client, AppSettings.SeibroSection cfg, IReadOnlyList<string> isins, IReadOnlySet<DateOnly> holidays, DateOnly startDate, DateOnly today, CancellationToken ct)
- {
- if (isins.Count == 0)
- {
- Logger.LogInformation("[{Job}] getSecnFrsecSetlInfo 대상 ISIN 없음 (ForeignIsins 비어 있음 — 종목별 결제 대기)", JobName);
- return;
- }
- var fetched = 0;
- var first = true;
- for (var day = today; day >= startDate; day = day.AddDays(-1))
- {
- if (day.DayOfWeek is DayOfWeek.Saturday or DayOfWeek.Sunday || holidays.Contains(day))
- {
- continue;
- }
- foreach (var isin in isins)
- {
- ct.ThrowIfCancellationRequested();
- // 데이터 존재 OR 조회완료 마커(축=isin) → 스킵 (결제 없는 (일,ISIN)도 재조회 회피, 리뷰 결함 #1)
- if (await db.ForeignSettlementSecurity.AsNoTracking().AnyAsync(c => c.ProcDt == day && c.Isin == isin, ct) || await SeibroMarkers.IsSweptAsync(db, SetlSecJobKey, day, isin, ct))
- {
- continue;
- }
- if (!quota.TryConsume(SeibroCategory.Foreign, 1, cfg.ForeignBudget))
- {
- Logger.LogWarning("[{Job}] 외화 예산({Budget}) 소진 — getSecnFrsecSetlInfo {Day}/{Isin} 부터 중단", JobName, cfg.ForeignBudget, day, isin);
- Logger.LogInformation("[{Job}] 종목별 결제 스윕 부분완료 — 창=[{Start}~{End}], fetch={Fetched}콜", JobName, startDate, today, fetched);
- return;
- }
- if (!first && cfg.DelayMs > 0)
- {
- await Task.Delay(cfg.DelayMs, ct);
- }
- first = false;
- var xml = await SeibroHttp.GetStringWithRetryAsync(client, cfg.BaseUrl, cfg.ApiKey, "getSecnFrsecSetlInfo", [new("PROC_DT", day.ToString("yyyyMMdd")), new("ISIN", isin)], Logger, ct);
- var rows = SeibroSecnFrsecSetlParser.Parse(SeibroXml.Parse(xml));
- var (inserted, updated) = await SeibroForeignImport.UpsertSettlementSecurityAsync(db, day, isin, rows, ct);
- await SeibroMarkers.MarkSweptAsync(db, SetlSecJobKey, day, isin, ct); // 0행 포함 조회완료 기록
- Logger.LogInformation("[{Job}] getSecnFrsecSetlInfo {Day}/{Isin} rows={Rows} — inserted={Inserted}, updated={Updated}", JobName, day, isin, rows.Count, inserted, updated);
- fetched++;
- }
- }
- Logger.LogInformation("[{Job}] 종목별 결제 스윕 완료 — 창=[{Start}~{End}], ISIN={Isins}, fetch={Fetched}콜", JobName, startDate, today, isins.Count, fetched);
- }
- }
|