fix: 단독 P2V 워커가 모델 미등록으로 큐를 처리하지 못하던 문제 수정
- standalone 이 main 을 import 하지 않아 첫 DB 조회에서 매퍼 설정 오류가 나고, loop() 의 requeue_running 이 try 밖이라 워커 태스크만 조용히 죽던 문제 - standalone 이 모델을 직접 등록하고, 워커 루프가 예기치 않게 끝나면 프로세스도 비정상 종료(restart 로 복구) - 기동 시 requeue_running 실패를 재시도하도록 loop 보강, start() 가 태스크를 반환
This commit is contained in:
1 parent
a92432867c
commit
50899861d0
2 files changed
+31
-7
No files matched your search
@@ -6,10 +6,14 @@ gunicorn 워커가 여럿이면 워커마다 파이프라인 워커가 떠서
|
||||
|
||||
실행: python -m app.p2v.worker.standalone
|
||||
종료: SIGTERM/SIGINT. 진행 중이던 잡은 running 으로 남고, 다음 기동의 requeue_running 이 이어서 돌린다.
|
||||
워커 루프가 예기치 않게 끝나면 프로세스도 비정상 종료(1)한다 — 컨테이너(restart: always)가 다시 띄운다.
|
||||
"""
|
||||
import asyncio
|
||||
import signal
|
||||
|
||||
# 웹 앱은 main 이 모든 모델을 import 해 SQLAlchemy 매퍼를 구성한다. 이 프로세스에는 main 이 없어서
|
||||
# 여기서 직접 불러오지 않으면 첫 DB 조회에서 "name 'User' is not defined" 로 실패한다
|
||||
import main # noqa: F401
|
||||
from app.p2v.utils import blob
|
||||
from app.p2v.worker import worker
|
||||
from app.utils.logger import get_logger
|
||||
@@ -22,18 +26,27 @@ async def run(stop: asyncio.Event) -> None:
|
||||
from app.database.session import dispose_engine
|
||||
|
||||
logger.info("[p2v] 단독 워커 시작")
|
||||
worker.start()
|
||||
task = worker.start()
|
||||
stopper = asyncio.ensure_future(stop.wait())
|
||||
crashed = False
|
||||
try:
|
||||
await stop.wait()
|
||||
await asyncio.wait({task, stopper}, return_when=asyncio.FIRST_COMPLETED)
|
||||
# 종료 신호 없이 루프가 끝났다면 예외로 죽은 것이다. 프로세스만 살아 있으면 큐가 조용히 멈춘다
|
||||
crashed = task.done() and not stop.is_set()
|
||||
finally:
|
||||
logger.info("[p2v] 종료 신호 — 워커 중지")
|
||||
stopper.cancel()
|
||||
logger.info("[p2v] 종료 신호 — 워커 중지" if not crashed else "[p2v] 워커 루프 비정상 종료 — 정리")
|
||||
await worker.stop()
|
||||
await blob.close_client()
|
||||
await dispose_engine()
|
||||
logger.info("[p2v] 단독 워커 종료")
|
||||
if crashed:
|
||||
error = None if task.cancelled() else task.exception()
|
||||
logger.error(f"[p2v] 워커 루프가 예기치 않게 끝났다: {error!r}")
|
||||
raise SystemExit(1)
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
async def main_() -> None:
|
||||
stop = asyncio.Event()
|
||||
loop = asyncio.get_running_loop()
|
||||
for sig in (signal.SIGTERM, signal.SIGINT):
|
||||
@@ -46,4 +59,4 @@ async def main() -> None:
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
asyncio.run(main_())
|
||||
@@ -53,7 +53,17 @@ async def requeue_running() -> None:
|
||||
|
||||
|
||||
async def loop() -> None:
|
||||
await requeue_running()
|
||||
# 기동 시 일시적인 오류(DB 연결 등)로 워커가 조용히 죽지 않게, 성공할 때까지 재시도한다.
|
||||
# 이 줄이 try 밖에 있으면 예외 하나로 태스크만 죽고 프로세스는 살아 있어 큐가 멈춘다
|
||||
while True:
|
||||
try:
|
||||
await requeue_running()
|
||||
break
|
||||
except asyncio.CancelledError:
|
||||
raise
|
||||
except Exception:
|
||||
traceback.print_exc()
|
||||
await asyncio.sleep(POLL_INTERVAL)
|
||||
while True:
|
||||
try:
|
||||
if not await run_once():
|
||||
@@ -66,9 +76,10 @@ async def loop() -> None:
|
||||
await asyncio.sleep(POLL_INTERVAL)
|
||||
|
||||
|
||||
def start() -> None:
|
||||
def start() -> asyncio.Task:
|
||||
global _worker
|
||||
_worker = asyncio.create_task(loop(), name="pipeline-worker")
|
||||
return _worker
|
||||
|
||||
|
||||
async def stop() -> None:
|
||||
|
||||
Reference in new issue
Block a user