107 lines
3.8 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
import calendar
from config import CONFIG
from logger_config import setup_logger
# 设置日志记录器
logger = setup_logger('scheduler')
def run_sendmsg():
"""执行sendmsg.py脚本周末不执行"""
# 获取当前日期是星期几0是星期一6是星期日
current_weekday = datetime.now().weekday()
# 如果是周末(周六或周日),不执行任务
if current_weekday >= 5: # 5是周六6是周日
print(f"[{datetime.now()}] 今天是周末,不执行任务")
return
script_path = os.path.join(os.path.dirname(__file__), 'sendmsg.py')
logger.info("=" * 50)
logger.info("开始执行每日消息发送任务...")
logger.info(f"脚本路径: {script_path}")
logger.info(f"当前工作目录: {os.getcwd()}")
try:
logger.info("正在启动sendmsg.py子进程...")
result = subprocess.run([sys.executable, script_path],
capture_output=True, text=True, cwd=os.path.dirname(__file__))
logger.info(f"子进程执行完成,返回码: {result.returncode}")
if result.returncode == 0:
logger.info("任务执行成功")
if result.stdout:
logger.info(f"标准输出: {result.stdout}")
else:
logger.error("任务执行失败")
if result.stderr:
logger.error(f"错误输出: {result.stderr}")
if result.stdout:
logger.info(f"标准输出: {result.stdout}")
except Exception as e:
logger.error(f"执行出错: {str(e)}")
logger.error(f"错误类型: {type(e).__name__}")
import traceback
logger.error(f"异常堆栈: {traceback.format_exc()}")
logger.info("=" * 50)
def main():
"""主函数 - 设置定时任务"""
logger.info("=" * 50)
logger.info("定时任务调度器启动")
logger.info("=" * 50)
# 显示配置信息
logger.info(f"检查时间: {CONFIG['checking_time']}")
logger.info(f"发送时间: {CONFIG['sending_time']}")
logger.info(f"文件路径: {CONFIG['file_path']}")
logger.info(f"消息接收者: {CONFIG['messages_reciever']}")
logger.info(f"飞书接收者数量: {len(CONFIG['open_id'])}")
# 每天在配置的时间执行任务
schedule.every().day.at(CONFIG['checking_time']).do(run_sendmsg)
logger.info(f"定时任务已设置,将在每天 {CONFIG['checking_time']} 执行sendmsg.py")
logger.info("按Ctrl+C停止程序")
# 显示下次执行时间
next_run = schedule.next_run()
if next_run:
logger.info(f"下次执行时间: {next_run.strftime('%Y-%m-%d %H:%M:%S')}")
try:
while True:
schedule.run_pending()
time.sleep(60) # 每分钟检查一次
# 每分钟显示一次状态信息
current_time = datetime.now()
logger.debug(f"当前时间: {current_time.strftime('%Y-%m-%d %H:%M:%S')}, 等待下次执行...")
except KeyboardInterrupt:
logger.info(f"\n收到中断信号,正在停止定时任务...")
logger.info(f"定时任务已停止")
except Exception as e:
logger.error(f"定时任务运行过程中出现异常: {str(e)}")
logger.error(f"异常类型: {type(e).__name__}")
import traceback
logger.error(f"异常堆栈: {traceback.format_exc()}")
raise
if __name__ == "__main__":
try:
main()
except Exception as e:
logger.error(f"程序启动失败: {str(e)}")
logger.error(f"异常类型: {type(e).__name__}")
import traceback
logger.error(f"异常堆栈: {traceback.format_exc()}")
sys.exit(1)