106 lines
3.1 KiB
Python
106 lines
3.1 KiB
Python
"""任务三定时调度器"""
|
||
import sys
|
||
import signal
|
||
import time
|
||
from pathlib import Path
|
||
from datetime import datetime
|
||
|
||
# 添加项目根目录到Python路径
|
||
project_root = Path(__file__).parent.parent
|
||
sys.path.insert(0, str(project_root))
|
||
|
||
import schedule
|
||
from src3.main import run_once
|
||
|
||
|
||
class Task3Scheduler:
|
||
"""任务三定时调度器"""
|
||
|
||
def __init__(self):
|
||
self.running = True
|
||
self.total_runs = 0
|
||
self.success_count = 0
|
||
self.fail_count = 0
|
||
self.start_time = datetime.now()
|
||
|
||
# 注册信号处理
|
||
signal.signal(signal.SIGINT, self._signal_handler)
|
||
signal.signal(signal.SIGTERM, self._signal_handler)
|
||
|
||
def _signal_handler(self, signum, frame):
|
||
"""处理退出信号"""
|
||
print("\n收到退出信号,等待当前任务完成...")
|
||
self.running = False
|
||
|
||
def _is_workday(self):
|
||
"""判断是否为工作日(周一到周五)"""
|
||
return datetime.now().weekday() < 5
|
||
|
||
def _job(self):
|
||
"""定时任务"""
|
||
if not self._is_workday():
|
||
print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] 今天是周末,跳过推送")
|
||
return
|
||
|
||
print(f"\n{'='*50}")
|
||
print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] 开始执行过期单推送")
|
||
print(f"{'='*50}")
|
||
|
||
self.total_runs += 1
|
||
|
||
try:
|
||
run_once()
|
||
self.success_count += 1
|
||
print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] ✓ 执行成功")
|
||
except Exception as e:
|
||
self.fail_count += 1
|
||
print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] ✗ 执行失败: {e}")
|
||
import traceback
|
||
traceback.print_exc()
|
||
|
||
def start(self):
|
||
"""启动调度器"""
|
||
from src3.config import Task3ConfigManager
|
||
|
||
try:
|
||
config = Task3ConfigManager()
|
||
push_time = config.get_push_time()
|
||
except Exception as e:
|
||
print(f"✗ 配置加载失败: {e}")
|
||
return
|
||
|
||
print("="*50)
|
||
print("任务三:TAPD过期单推送调度器")
|
||
print("="*50)
|
||
print(f"推送时间: 每个工作日 {push_time}")
|
||
print(f"启动时间: {self.start_time.strftime('%Y-%m-%d %H:%M:%S')}")
|
||
print("="*50)
|
||
|
||
# 设置定时任务
|
||
schedule.every().day.at(push_time).do(self._job)
|
||
|
||
# 循环执行
|
||
while self.running:
|
||
schedule.run_pending()
|
||
time.sleep(1)
|
||
|
||
# 退出统计
|
||
self._print_stats()
|
||
|
||
def _print_stats(self):
|
||
"""打印运行统计"""
|
||
print("\n" + "="*50)
|
||
print("运行统计")
|
||
print("="*50)
|
||
print(f"启动时间: {self.start_time.strftime('%Y-%m-%d %H:%M:%S')}")
|
||
print(f"结束时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
||
print(f"总执行次数: {self.total_runs}")
|
||
print(f"成功次数: {self.success_count}")
|
||
print(f"失败次数: {self.fail_count}")
|
||
print("="*50)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
scheduler = Task3Scheduler()
|
||
scheduler.start()
|