작업트리에 커밋되지 않은 채 쌓여 있던 것과, 오늘 찾은 문제 셋을 함께 담는다. ## 1. 콘텐츠 생성 진행 상태 (작업트리에 있던 것) COPY 잡의 실제 단계를 DB에 기록하고 응답으로 내보낸다. 폴링 횟수로 진행률을 흉내 내던 것을 걷어냈다. 새로고침·재접속해도 jobId 로 이어서 본다. - services/copy_steps.py · services/job_progress.py · common/job_errors.py (신규) - postgres-init/migrations/0013_job_progress.sql + init.sql - 프론트: useGenerationJob · generationLabels (신규), Step5Generating·pollJob 배선, orval 모델 갱신(jobProgress · jobStep · jobStepStatus · jobStepReason) - docs/GENERATION_FLOW.md (신규) ## 2. 발행된 사이트만 색인한다 실측(2026-09-15): 디스크의 발행본 33곳 중 **15곳이 draft 인데 `index, follow`** 였고 사이트맵에도 올라가 있었다. 사장님이 발행 버튼을 누른 적 없는 사이트가 짓다 만 상태로 구글에 실려 있었다는 뜻이다. head.ts 가 robots 를 하드코딩하고 payload 의 `site.status` 를 보지 않았다. "색인을 막을 이유가 없다"는 주석은 굽는 것이 곧 발행이던 시절의 말인데, 지금은 빌더 미리보기만 눌러도 draft 로 구워진다. - seo/head.ts: PUBLISHED 일 때만 index, 아니면 `noindex, follow` - 사이트맵·`/s` 목록·llms.txt 에서도 함께 빠진다 — 그쪽은 구운 HTML 의 robots 를 읽어 거른다(seo/directory.ts readBakedNoindex). 규칙을 두 자리에 두지 않으려고 한 곳에 뒀다 ## 3. [새로 크롤링하고 사이트 생성하기] 를 뒤집지 않는다ba90a19의 중복 합치기가 **일부러 다시 만들려는 경우까지** 기존 사업장으로 끌고 갔다 — 새로 만들기를 눌렀는데 기존 에디터가 열린다(사장님 보고 2026-09-15). - Req_VerifyPlaceByUrl.reuse_existing (기본 True — 다른 호출자의 동작은 그대로) - place_service.verify_place_by_url: 끄면 이어붙이지 않는다. 다만 **비어 있는 중복 행은 계속 치운다** — 원래 막으려던 누적이 그것이고 빈 행은 잃을 것이 없다 - ensureServerPlace: 위저드는 새로 만들기 경로에서만 오므로 False 로 보낸다 ## 4. 발행본 파비콘 발행본에 파비콘 링크가 아예 없어 브라우저 탭에 기본 아이콘이 떴다. 파일은 오리진 루트의 공용 자산이라 사이트마다 복사하지 않고 루트 절대경로로 가리킨다. 검증: site vitest 84건 통과 · tsc(site·frontend) · eslint 통과. 백엔드 pytest 는 로컬 DB 비밀번호가 맞지 않아 돌리지 못했다(a5b8701과 같은 자리). 발행본 반영에는 전체 재굽기가 필요하다. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
324 lines
14 KiB
Python
324 lines
14 KiB
Python
"""작업 큐 CRUD — PostgreSQL 을 '제대로' 큐로 쓴다. (LPS `crud/job_crud.py` 이식)
|
|
|
|
- 할당은 **단일 문장 원자 claim**: FOR UPDATE SKIP LOCKED 서브쿼리 + 같은 UPDATE + RETURNING.
|
|
→ 워커 컨테이너가 몇 개든 같은 잡 이중 할당이 원천 불가. fetch 와 claim 을 분리하지 않는다.
|
|
- 모든 전이는 **조건부 CAS**(WHERE 에 status/worker_id 가드) + RETURNING.
|
|
- 복구는 timeout 추측이 아니라 **lease 만료 소유권**(reaper 가 회수).
|
|
- 재시도/백오프/dead-letter 를 큐에 내장.
|
|
|
|
전이가 조회/변경으로 나뉘지 않으므로(RETURNING) execute_lambda_write 로 실행한다.
|
|
큐 전이만 raw SQL 이다 — 다른 crud 는 전부 SQLAlchemy 표현식을 쓴다.
|
|
"""
|
|
|
|
import json
|
|
|
|
from sqlalchemy import text
|
|
|
|
from common.database.db_session_manager import DB_SESSION_MNG
|
|
from common.enums import DBType, DBWRType, JobStatus
|
|
|
|
|
|
# 잡 적재 시 워커를 즉시 깨우는 LISTEN/NOTIFY 채널(폴링 제거).
|
|
JOB_NOTIFY_CHANNEL = "web4ai_job"
|
|
|
|
|
|
def compute_backoff(attempts: int, base: float = 5.0, cap: float = 600.0) -> float:
|
|
"""지수 백오프(초). attempts 회 시도 후 다음 재시도까지 대기 = base * 2^(attempts-1), cap 상한."""
|
|
return min(cap, base * (2 ** max(0, attempts - 1)))
|
|
|
|
|
|
class JobQueue:
|
|
DB = DBType.MAIN.value
|
|
|
|
async def _tx(self, fn):
|
|
"""쓰기 트랜잭션 — 값 반환이 필요한 큐 전이 전용 진입점."""
|
|
return await DB_SESSION_MNG.execute_lambda_write(self.DB, fn)
|
|
|
|
# ---- 적재 ----
|
|
async def enqueue(
|
|
self,
|
|
job_type: int,
|
|
payload: dict,
|
|
priority: int = 100,
|
|
dedupe_key: str | None = None,
|
|
max_attempts: int = 3,
|
|
) -> str | None:
|
|
"""잡 적재. dedupe_key 가 활성(PENDING/RUNNING) 중복이면 삽입 없이 None 반환."""
|
|
sql = text("""
|
|
INSERT INTO jobs (job_type, priority, payload, dedupe_key, max_attempts)
|
|
VALUES (:t, :p, CAST(:payload AS jsonb), :dk, :ma)
|
|
ON CONFLICT (dedupe_key) WHERE status IN (1, 2) AND dedupe_key IS NOT NULL
|
|
DO NOTHING
|
|
RETURNING job_id
|
|
""")
|
|
|
|
async def run(s):
|
|
row = (await s.execute(sql, {
|
|
"t": job_type, "p": priority, "payload": json.dumps(payload, ensure_ascii=False),
|
|
"dk": dedupe_key, "ma": max_attempts,
|
|
})).first()
|
|
if row:
|
|
# 커밋 시 전달됨 → LISTEN 중인 유휴 워커를 즉시 깨운다(중복 스킵 시엔 알림 안 함).
|
|
await s.execute(text("SELECT pg_notify(:ch, '')"), {"ch": JOB_NOTIFY_CHANNEL})
|
|
return str(row[0]) if row else None
|
|
|
|
return await self._tx(run)
|
|
|
|
# ---- 원자적 claim ----
|
|
async def claim(self, worker_id: str, lease_sec: int = 120) -> dict | None:
|
|
"""대기 잡 1건을 원자적으로 점유. 없으면 None.
|
|
FOR UPDATE SKIP LOCKED 로 잠근 행을 같은 UPDATE 에서 RUNNING 으로 전이 → 이중 할당 불가."""
|
|
sql = text("""
|
|
UPDATE jobs SET
|
|
status = 2,
|
|
worker_id = :wid,
|
|
lease_until = now() + make_interval(secs => :lease),
|
|
run_started_at = now(),
|
|
attempts = attempts + 1,
|
|
updated_at = now()
|
|
WHERE job_id = (
|
|
SELECT job_id FROM jobs
|
|
WHERE status = 1 AND run_after <= now()
|
|
ORDER BY priority ASC, created_at ASC
|
|
FOR UPDATE SKIP LOCKED
|
|
LIMIT 1
|
|
)
|
|
RETURNING job_id, job_type, payload, attempts, max_attempts, worker_id
|
|
""")
|
|
|
|
async def run(s):
|
|
row = (await s.execute(sql, {"wid": worker_id, "lease": lease_sec})).mappings().first()
|
|
if not row:
|
|
return None
|
|
d = dict(row)
|
|
d["job_id"] = str(d["job_id"])
|
|
if isinstance(d.get("payload"), str):
|
|
d["payload"] = json.loads(d["payload"])
|
|
return d
|
|
|
|
return await self._tx(run)
|
|
|
|
# ---- 완료/실패 (소유권 가드) ----
|
|
async def complete(self, job_id: str, worker_id: str, result: dict | None = None) -> bool:
|
|
sql = text("""
|
|
UPDATE jobs SET status = 3, result = CAST(:result AS jsonb),
|
|
lease_until = NULL, worker_id = NULL, updated_at = now()
|
|
WHERE job_id = CAST(:id AS uuid) AND status = 2 AND worker_id = :wid
|
|
RETURNING job_id
|
|
""")
|
|
|
|
async def run(s):
|
|
row = (await s.execute(sql, {
|
|
"id": job_id, "wid": worker_id,
|
|
"result": json.dumps(result, ensure_ascii=False) if result is not None else None,
|
|
})).first()
|
|
return row is not None
|
|
|
|
return await self._tx(run)
|
|
|
|
async def fail(self, job_id: str, worker_id: str, error: str, backoff_sec: float = 5.0) -> int | None:
|
|
"""실패 처리. 시도 남으면 PENDING(run_after=백오프)으로 재큐, 소진되면 DEAD(dead-letter).
|
|
전이 후 status(JobStatus 값)를 반환. 소유 불일치면 None."""
|
|
sql = text("""
|
|
UPDATE jobs SET
|
|
status = CASE WHEN attempts >= max_attempts THEN 4 ELSE 1 END,
|
|
run_after = CASE WHEN attempts >= max_attempts THEN run_after
|
|
ELSE now() + make_interval(secs => :backoff) END,
|
|
last_error = :err,
|
|
lease_until = NULL,
|
|
worker_id = NULL,
|
|
updated_at = now()
|
|
WHERE job_id = CAST(:id AS uuid) AND status = 2 AND worker_id = :wid
|
|
RETURNING status
|
|
""")
|
|
|
|
async def run(s):
|
|
row = (await s.execute(sql, {
|
|
"id": job_id, "wid": worker_id, "err": error[:2000], "backoff": backoff_sec,
|
|
})).first()
|
|
return int(row[0]) if row else None
|
|
|
|
return await self._tx(run)
|
|
|
|
async def fail_permanent(self, job_id: str, worker_id: str, error: str) -> bool:
|
|
"""재시도 없이 바로 DEAD. 시도 횟수가 남아 있어도 보내지 않는다.
|
|
|
|
★ 다시 해도 같은 결과인 실패에 쓴다(common/job_errors.PermanentJobError).
|
|
백오프 재큐는 '일시적 장애' 라는 판단인데, 사업장이 지워졌거나 업종이 없는 잡은
|
|
그 판단이 틀렸다 — 큐만 붙들고 DEAD 알림을 세 배로 늘린다."""
|
|
sql = text("""
|
|
UPDATE jobs SET status = 4, last_error = :err,
|
|
lease_until = NULL, worker_id = NULL, updated_at = now()
|
|
WHERE job_id = CAST(:id AS uuid) AND status = 2 AND worker_id = :wid
|
|
RETURNING job_id
|
|
""")
|
|
|
|
async def run(s):
|
|
row = (await s.execute(sql, {"id": job_id, "wid": worker_id, "err": error[:2000]})).first()
|
|
return row is not None
|
|
|
|
return await self._tx(run)
|
|
|
|
# ---- lease 갱신(heartbeat) / 회수(reaper) ----
|
|
async def renew_lease(self, job_id: str, worker_id: str, lease_sec: int = 120) -> bool:
|
|
sql = text("""
|
|
UPDATE jobs SET lease_until = now() + make_interval(secs => :lease), updated_at = now()
|
|
WHERE job_id = CAST(:id AS uuid) AND worker_id = :wid AND status = 2
|
|
RETURNING job_id
|
|
""")
|
|
|
|
async def run(s):
|
|
row = (await s.execute(sql, {"id": job_id, "wid": worker_id, "lease": lease_sec})).first()
|
|
return row is not None
|
|
|
|
return await self._tx(run)
|
|
|
|
async def reap(self) -> list[str]:
|
|
"""만료된 lease(워커 사망 등)의 RUNNING 잡을 회수. 시도 남으면 즉시 재큐, 소진되면 DEAD.
|
|
회수된 job_id 목록 반환."""
|
|
sql = text("""
|
|
UPDATE jobs SET
|
|
status = CASE WHEN attempts >= max_attempts THEN 4 ELSE 1 END,
|
|
run_after = now(),
|
|
last_error = COALESCE(last_error, '') || ' [lease-expired reclaim]',
|
|
lease_until = NULL,
|
|
worker_id = NULL,
|
|
updated_at = now()
|
|
WHERE status = 2 AND lease_until IS NOT NULL AND lease_until < now()
|
|
RETURNING job_id
|
|
""")
|
|
|
|
async def run(s):
|
|
rows = (await s.execute(sql)).all()
|
|
return [str(r[0]) for r in rows]
|
|
|
|
return await self._tx(run)
|
|
|
|
# ---- 단건 조회 (상태 폴링) ----
|
|
async def set_progress(self, job: dict, progress: dict) -> bool:
|
|
# 회수된 옛 워커가 새 시도의 진행 상태를 덮지 못하게 한다.
|
|
sql = text("""
|
|
UPDATE jobs SET progress = CAST(:progress AS jsonb), updated_at = now()
|
|
WHERE job_id = CAST(:id AS uuid) AND status = 2
|
|
AND worker_id = :wid AND attempts = :attempt
|
|
AND lease_until > now()
|
|
RETURNING job_id
|
|
""")
|
|
|
|
async def run(s):
|
|
row = (await s.execute(sql, {
|
|
"id": job["job_id"], "wid": job["worker_id"], "attempt": job["attempts"],
|
|
"progress": json.dumps(progress),
|
|
})).first()
|
|
return row is not None
|
|
|
|
return await self._tx(run)
|
|
|
|
async def find_latest(self, dedupe_key: str) -> dict | None:
|
|
"""복구는 완료·실패 이력도 찾는다. 활성 중복 방지와 다른 조회다."""
|
|
async def run(s):
|
|
row = (await s.execute(text("""
|
|
SELECT job_id, status FROM jobs WHERE dedupe_key = :dk
|
|
ORDER BY created_at DESC, job_id DESC LIMIT 1
|
|
"""), {"dk": dedupe_key})).mappings().first()
|
|
return {**row, "job_id": str(row["job_id"])} if row else None
|
|
|
|
return await DB_SESSION_MNG.execute_lambda(self.DB, DBWRType.DB_READ.value, run)
|
|
|
|
async def get(self, job_id: str) -> dict | None:
|
|
"""잡 단건 조회(읽기). 없으면 None. status 는 정수(JobStatus 값)."""
|
|
sql = text("""
|
|
SELECT job_id, job_type, status, priority, attempts, max_attempts,
|
|
payload, result, progress, last_error, run_after, run_started_at, created_at, updated_at
|
|
FROM jobs WHERE job_id = CAST(:id AS uuid)
|
|
""")
|
|
|
|
async def run(s):
|
|
row = (await s.execute(sql, {"id": job_id})).mappings().first()
|
|
if not row:
|
|
return None
|
|
d = dict(row)
|
|
d["job_id"] = str(d["job_id"])
|
|
for key in ("payload", "result", "progress"):
|
|
if isinstance(d.get(key), str):
|
|
d[key] = json.loads(d[key])
|
|
return d
|
|
|
|
return await DB_SESSION_MNG.execute_lambda(self.DB, DBWRType.DB_READ.value, run)
|
|
|
|
async def find_active(self, dedupe_key: str) -> dict | None:
|
|
"""dedupe_key 로 활성(PENDING/RUNNING) 잡을 찾는다.
|
|
enqueue 가 중복으로 None 을 돌려줬을 때, 이미 돌고 있는 잡의 id 를 알려주기 위함."""
|
|
sql = text("""
|
|
SELECT job_id, job_type, status FROM jobs
|
|
WHERE dedupe_key = :dk AND status IN (1, 2)
|
|
LIMIT 1
|
|
""")
|
|
|
|
async def run(s):
|
|
row = (await s.execute(sql, {"dk": dedupe_key})).mappings().first()
|
|
if not row:
|
|
return None
|
|
d = dict(row)
|
|
d["job_id"] = str(d["job_id"])
|
|
return d
|
|
|
|
return await DB_SESSION_MNG.execute_lambda(self.DB, DBWRType.DB_READ.value, run)
|
|
|
|
# ---- 관측(관리 API/알림용) ----
|
|
async def counts(self) -> dict[str, int]:
|
|
"""상태별 잡 개수."""
|
|
async def run(s):
|
|
rows = (await s.execute(text("SELECT status, count(*) FROM jobs GROUP BY status"))).all()
|
|
by_val = {int(st): int(c) for st, c in rows}
|
|
return {js.name: by_val.get(js.value, 0) for js in JobStatus}
|
|
|
|
return await DB_SESSION_MNG.execute_lambda(self.DB, DBWRType.DB_READ.value, run)
|
|
|
|
async def ops(self) -> dict:
|
|
"""운영 스냅샷(모니터링·알림용): 상태별 카운트 + 큐 지연(가장 오래된 PENDING 나이) +
|
|
최근 1시간 DEAD + stuck(좀비 신호).
|
|
|
|
stuck 은 두 축 — lease 만료(워커 사망인데 reaper 미회수) OR 실행 10분 초과(핸들러 행 —
|
|
heartbeat 가 lease 를 계속 갱신해 lease 축엔 안 잡히므로 run_started_at 으로 따로 본다)."""
|
|
sql = text("""
|
|
SELECT
|
|
count(*) FILTER (WHERE status = 1) AS pending,
|
|
count(*) FILTER (WHERE status = 2) AS running,
|
|
count(*) FILTER (WHERE status = 3) AS done,
|
|
count(*) FILTER (WHERE status = 4) AS dead,
|
|
count(*) FILTER (WHERE status = 4 AND updated_at > now() - interval '1 hour') AS dead_1h,
|
|
count(*) FILTER (WHERE status = 2 AND (
|
|
(lease_until IS NOT NULL AND lease_until < now())
|
|
OR run_started_at < now() - interval '10 minutes'
|
|
)) AS stuck_running,
|
|
COALESCE(EXTRACT(EPOCH FROM (now() - min(created_at) FILTER (WHERE status = 1)))::int, 0) AS oldest_pending_sec,
|
|
count(*) FILTER (WHERE last_error LIKE 'JobDeadlineExceeded%'
|
|
AND updated_at > now() - interval '1 hour') AS deadline_1h
|
|
FROM jobs
|
|
""")
|
|
|
|
async def run(s):
|
|
return {k: int(v) for k, v in dict((await s.execute(sql)).mappings().first()).items()}
|
|
|
|
return await DB_SESSION_MNG.execute_lambda(self.DB, DBWRType.DB_READ.value, run)
|
|
|
|
async def requeue(self, job_id: str) -> str | None:
|
|
"""DEAD 잡 재큐(관리자 액션): attempts 리셋 + PENDING 전이 + 워커 깨움.
|
|
DEAD 가 아니거나 없으면 None. 같은 dedupe_key 의 활성 잡이 있으면 부분 유니크 위반."""
|
|
sql = text("""
|
|
UPDATE jobs SET status = 1, attempts = 0, run_after = now(),
|
|
lease_until = NULL, worker_id = NULL, run_started_at = NULL,
|
|
last_error = NULL, updated_at = now()
|
|
WHERE job_id = CAST(:jid AS uuid) AND status = 4
|
|
RETURNING job_id
|
|
""")
|
|
|
|
async def run(s):
|
|
row = (await s.execute(sql, {"jid": job_id})).first()
|
|
if row:
|
|
await s.execute(text("SELECT pg_notify(:ch, '')"), {"ch": JOB_NOTIFY_CHANNEL})
|
|
return str(row[0]) if row else None
|
|
|
|
return await self._tx(run)
|