From f1b811965b395495b7e199863875527207e3fa18 Mon Sep 17 00:00:00 2001 From: chenshuobo <1119780489@qq.com> Date: Tue, 16 Jun 2026 15:06:07 +0800 Subject: [PATCH] =?UTF-8?q?fix(meituan-etl):=20=E5=B8=B8=E9=A9=BB=E5=8F=AF?= =?UTF-8?q?=E8=A7=82=E6=B5=8B=E6=80=A7=E2=80=94=E2=80=94=E5=8E=8B=E5=88=B6?= =?UTF-8?q?=E7=BE=8E=E5=9B=A2=20logger=20traceback=20+=20stdout=20?= =?UTF-8?q?=E8=A1=8C=E7=BC=93=E5=86=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 本地灰度(20 城 / 每 10min 一轮)暴露两个只在常驻/cron 下出现的问题: - 美团调用偶发 SSL EOF(本机走代理高并发)/ code=5 被 meituan._call 的 logger.exception 打完整 traceback,单轮刷数十 KB 日志;ETL 已用 _STATS 统计放弃页数,故把 shagua.meituan logger 压到 CRITICAL(线上直连少见,统计仍可见放弃数); - stdout 块缓冲导致 cron/后台 log 攒到 ~4KB 才落盘,常驻几乎看不到进度; reconfigure(line_buffering=True) 每行即时 flush。 --- scripts/pull_meituan_coupons.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/scripts/pull_meituan_coupons.py b/scripts/pull_meituan_coupons.py index d136465..3bc2977 100644 --- a/scripts/pull_meituan_coupons.py +++ b/scripts/pull_meituan_coupons.py @@ -34,6 +34,7 @@ from __future__ import annotations import argparse import hashlib +import logging import os import re import sys @@ -43,9 +44,11 @@ import time from concurrent.futures import ThreadPoolExecutor, as_completed from datetime import datetime, timedelta, timezone -# Windows 控制台按 UTF-8 输出中文/¥ +# Windows 控制台按 UTF-8 输出中文/¥;line_buffering=True 让 print 每行即时 flush—— +# 否则 stdout 重定向到 cron/后台日志文件时是块缓冲,要攒到 ~4KB 或进程退出才落盘, +# 常驻(--loop)时几乎看不到每轮进度。 try: - sys.stdout.reconfigure(encoding="utf-8") # type: ignore[attr-defined] + sys.stdout.reconfigure(encoding="utf-8", line_buffering=True) # type: ignore[attr-defined] except Exception: # noqa: BLE001 pass @@ -61,6 +64,11 @@ from app.models.meituan_coupon import MeituanCoupon # noqa: E402 # ETL 不需要 SQL 日志,显式关掉(入库不受影响;线上 prod 本就 echo=False)。 engine.echo = False +# 美团调用偶发错误(本机走代理高并发时的 SSL EOF / 上游 code=5 等)会被 meituan._call 的 +# logger.exception 打完整 traceback,并发抓取下单轮可刷数十 KB 日志。ETL 自己用 _STATS 统计 +# 「放弃页数」,无需逐条 traceback,故把美团 logger 压到 CRITICAL(线上直连少见此类错误)。 +logging.getLogger("shagua.meituan").setLevel(logging.CRITICAL) + QUERY_PATH = "/cps_open/common/api/v1/query_coupon" PAGE_SIZE = 20 MAX_PAGES = 80 # 单路安全上限(搜索 ~52 页、供给 ~70 页)