Files
kami_apple_exchage/backend/scripts/run_with_graceful_shutdown.py
danial 8ad2a5366a refactor(backend): 将Celery替换为Arq进行协程任务处理
本次提交将后端的任务队列系统从Celery迁移到了Arq,以支持基于协程的任务处理。主要改动包括:
- 更新文档和配置文件,反映架构变化。
- 修改健康检查和服务初始化逻辑,以适应Arq的使用。
- 移除与Celery相关的代码,并添加Arq任务定义和调度器。
- 更新Dockerfile和相关脚本,确保Arq worker能够正确运行。
- 调整API和业务服务中的任务处理逻辑,移除对Celery的依赖。

这些改动旨在提高系统的异步处理能力和整体性能。
2025-09-18 16:02:05 +08:00

83 lines
2.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
带优雅关闭功能的应用启动脚本
支持Web服务和Arq Worker的优雅关闭
"""
import argparse
import asyncio
import os
import sys
from pathlib import Path
# 添加项目根目录到Python路径
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))
from app.core.graceful_shutdown import (
graceful_shutdown_manager,
)
from app.core.log import get_logger
logger = get_logger(__name__)
def run_arq_worker():
"""运行Arq Worker带优雅关闭"""
logger.info("启动Arq Worker...")
# 设置优雅关闭 - 信号处理器由 worker_init 模块统一管理
graceful_shutdown_manager.setup_signal_handlers()
logger.info("Arq worker已经集成到主进程中请使用uv run python -m arq app.core.arq_worker:ArqWorkerSettings启动")
try:
# Windows环境提示
if sys.platform.startswith("win"):
logger.info("在Windows上请使用: uv run python -m arq app.core.arq_worker:ArqWorkerSettings")
else:
logger.info("请使用: uv run python -m arq app.core.arq_worker:ArqWorkerSettings")
# 简单等待,让用户看到消息
import time
time.sleep(3)
except Exception as e:
logger.error(f"Arq Worker启动失败: {e}")
raise
def main():
"""主函数"""
parser = argparse.ArgumentParser(
description="Apple Exchange Backend - 优雅关闭启动器"
)
parser.add_argument(
"mode",
choices=["web", "worker", "dev"],
help="启动模式: web (Web服务), worker (Arq Worker), dev (开发模式)",
)
parser.add_argument("--debug", action="store_true", help="启用调试模式")
args = parser.parse_args()
# 设置环境变量
if args.debug:
os.environ["DEBUG"] = "true"
logger.info(f"启动模式: {args.mode}")
try:
if args.mode == "worker":
run_arq_worker()
except KeyboardInterrupt:
logger.info("收到中断信号,程序已优雅关闭")
except Exception as e:
logger.error(f"程序启动失败: {e}")
sys.exit(1)
if __name__ == "__main__":
main()