/** 수집에 딸린 잡 실행과 캐시 정리 — React 를 모르는 부분. */ import type {JobData} from '@/api'; import { getGetPlaceQueryKey, getListFactsQueryKey, getListFaqsQueryKey, getListLinksQueryKey, getListMediaQueryKey, pollJob, startCopy, startVision, } from '@/api'; import {notify, notifyApiError} from '@/lib/notify'; import {queryClient} from '@/lib/query-client'; import {notifyCopy, notifyVision} from './collectNotify'; import type {RecollectTask} from './useCollectFlow'; /** 재수집 작업의 화면 표시명. */ export const RECOLLECT_LABEL: Record = { facts: '정보', photos: '사진', copy: '소개문', }; /** 사진 분석 · 소개문 생성 잡 1회분. */ export async function runSideJob(task: 'photos' | 'copy', placeId: string, signal: AbortSignal) { const label = RECOLLECT_LABEL[task]; let jobId: string; try { const started = task === 'photos' ? // force:true — 이미 alt 가 붙은 사진도 다시 분석한다. // "분석할 사진이 없습니다"로 끝나면 재분석이라는 이름값을 못 한다. await startVision(placeId, {force: true}, undefined, signal) : await startCopy(placeId, {}, undefined, signal); // 거절도 HTTP 200 이다 — 사진 0장(MEDIA_NOT_FOUND)·근거 부족(FAQ_UNGROUNDED)· 키 미설정(GENERATOR_NOT_CONFIGURED)이 전부 이 자리로 온다. if (started.result?.success === false || !started.job_id) { notifyApiError({data: started}, `${label}을 시작하지 못했습니다.`); return; } jobId = started.job_id; } catch (error) { if (signal.aborted) return; notifyApiError(error, `${label}을 시작하지 못했습니다.`); return; } const outcome = await pollJob(jobId, {signal}); switch (outcome.kind) { case 'aborted': return; case 'dead': notify.error(`${label}을 끝내지 못했습니다.`, outcome.job.last_error ?? undefined); return; case 'unreachable': notifyApiError(outcome.error, `${label} 상태를 확인하지 못했습니다.`); return; case 'timeout': notify.warn( `${label}이 예상보다 오래 걸립니다.`, '서버에서는 계속 진행됩니다. 잠시 후 다시 확인해 주세요.', ); return; case 'done': if (task === 'photos') { // 사진은 media 가 진실이다 — 라벨·alt 가 바뀌었으니 다시 읽게 한다. await invalidate(getListMediaQueryKey(placeId)); notifyVision(outcome.job); } else { // 소개문은 fact(intro·meta_description)로, FAQ 는 faq 로 들어간다 — 둘 다 무효화한다. await invalidate(getListFactsQueryKey(placeId), getListFaqsQueryKey(placeId)); notifyCopy(outcome.job); } } } /** 수집이 큐에 넣은 사진 분석 잡을 기다린다. */ export async function waitForVision(placeId: string, job: JobData, signal: AbortSignal) { const visionJobId = (job.result as {vision_job_id?: string} | undefined)?.vision_job_id; if (!visionJobId) return; const outcome = await pollJob(visionJobId, {signal}); if (outcome.kind === 'done') await refreshPlaceCaches(placeId); } /** 수집이 끝난 뒤 서버가 진실인 것들 — place(주소·갱신시각) · 링크 · fact · **사진**. */ export async function refreshPlaceCaches(placeId: string) { await Promise.all([ queryClient.invalidateQueries({queryKey: getGetPlaceQueryKey(placeId)}), queryClient.invalidateQueries({queryKey: getListLinksQueryKey(placeId)}), queryClient.invalidateQueries({queryKey: getListFactsQueryKey(placeId)}), queryClient.invalidateQueries({queryKey: getListMediaQueryKey(placeId)}), ]).catch(() => undefined); } /** 캐시 무효화 묶음. */ export async function invalidate(...queryKeys: readonly (readonly unknown[])[]) { await Promise.all( queryKeys.map((queryKey) => queryClient.invalidateQueries({queryKey})), ).catch(() => undefined); }