49 lines
1.6 KiB
Python
49 lines
1.6 KiB
Python
"""当日限制价的独立采集入口,不启动Celery或QMT连接。"""
|
||
import logging
|
||
import re
|
||
import signal
|
||
from threading import Event
|
||
|
||
|
||
logger = logging.getLogger('pms.market_data_worker')
|
||
|
||
|
||
def _summary(result):
|
||
"""只保留固定摘要字段,源响应及异常正文不得进入进程日志。"""
|
||
if not isinstance(result, dict):
|
||
raise ValueError('采集摘要格式无效')
|
||
status = result.get('status')
|
||
status = status if isinstance(status, str) and re.fullmatch(r'[A-Za-z0-9_-]{1,48}', status) else 'unknown'
|
||
day = result.get('day')
|
||
day = day if isinstance(day, str) and re.fullmatch(r'\d{4}-\d{2}-\d{2}', day) else '-'
|
||
counts = []
|
||
for key in ('target_count', 'valid_count', 'missing_count', 'error_count'):
|
||
value = result.get(key)
|
||
counts.append(value if type(value) is int and value >= 0 else 0)
|
||
return status, day, *counts
|
||
|
||
|
||
def main():
|
||
from app.services import price_limit_collector
|
||
|
||
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(name)s %(message)s')
|
||
stopping = Event()
|
||
|
||
def stop(_signum, _frame):
|
||
stopping.set()
|
||
|
||
signal.signal(signal.SIGTERM, stop)
|
||
signal.signal(signal.SIGINT, stop)
|
||
while not stopping.is_set():
|
||
try:
|
||
result = price_limit_collector.run_once()
|
||
logger.info('限制价采集 status=%s day=%s target_count=%s valid_count=%s missing_count=%s error_count=%s',
|
||
*_summary(result))
|
||
except Exception as exc:
|
||
logger.warning('限制价采集失败:%s', type(exc).__name__)
|
||
stopping.wait(10)
|
||
|
||
|
||
if __name__ == '__main__':
|
||
main()
|