여러 줄 주석이 설명보다 경위(예전·실측·지적)를 적고 있어 읽는 사람이 결론을 찾기 어려웠다. - ts·tsx·js·mjs·css·py 478개: 여러 줄 주석은 첫 문장 한 줄로, 과거형·날짜 문장은 삭제 - 주석 위치는 TypeScript 파서·파이썬 tokenize/ast 로 찾는다 — 문자열 안의 # · /* 는 건드리지 않는다 - eslint·ts·noqa·type: ignore 같은 지시 주석은 그대로 둔다 파이썬 275개 정리 전후 AST 동일, TS 298개 주석 뺀 토큰 동일(빈 JSX 주석 10곳만 차이). site·frontend·admin tsc, site vitest 105 passed Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
101 lines
4.0 KiB
TypeScript
101 lines
4.0 KiB
TypeScript
/** 수집에 딸린 잡 실행과 캐시 정리 — 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<RecollectTask, string> = {
|
|
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);
|
|
}
|