teddy 7233700539 feat: 重构定时任务系统并增强配置管理
- 将硬编码的时间配置改为从CONFIG读取
- 支持多接收人配置,修改open_id为数组类型
- 增加发送时间等待功能,确保消息在指定时间发送
- 改进停止定时任务的进程查找逻辑
- 更新配置项,增加checking_time、sending_time和message字段
2025-06-26 17:24:50 +08:00

39 lines
1.3 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.

import schedule
import time
import subprocess
import sys
import os
from datetime import datetime
from config import CONFIG
def run_sendmsg():
"""执行sendmsg.py脚本"""
script_path = os.path.join(os.path.dirname(__file__), 'sendmsg.py')
try:
print(f"[{datetime.now()}] 开始执行每日消息发送任务...")
result = subprocess.run([sys.executable, script_path],
capture_output=True, text=True, cwd=os.path.dirname(__file__))
if result.returncode == 0:
print(f"[{datetime.now()}] 任务执行成功")
else:
print(f"[{datetime.now()}] 任务执行失败: {result.stderr}")
except Exception as e:
print(f"[{datetime.now()}] 执行出错: {str(e)}")
def main():
"""主函数 - 设置定时任务"""
# 每天17:30执行任务
schedule.every().day.at(CONFIG['checking_time']).do(run_sendmsg)
print(f"[{datetime.now()}] 定时任务已启动将在每天17:30执行sendmsg.py")
print("按Ctrl+C停止程序")
try:
while True:
schedule.run_pending()
time.sleep(60) # 每分钟检查一次
except KeyboardInterrupt:
print(f"\n[{datetime.now()}] 定时任务已停止")
if __name__ == "__main__":
main()