refactor: 重构定时任务和消息发送逻辑
- 删除start_scheduler.bat脚本,改为直接运行scheduler.py - 重构scheduler.py,添加日志记录和主函数封装 - 重构sendmsg.py,将逻辑封装到函数中并添加返回值 - 调整定时任务执行时间为每天17:30
This commit is contained in:
parent
699bac8008
commit
84288fc66b
37
scheduler.py
37
scheduler.py
@ -3,17 +3,36 @@ import time
|
|||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
def run_sendmsg():
|
def run_sendmsg():
|
||||||
|
"""执行sendmsg.py脚本"""
|
||||||
script_path = os.path.join(os.path.dirname(__file__), 'sendmsg.py')
|
script_path = os.path.join(os.path.dirname(__file__), 'sendmsg.py')
|
||||||
subprocess.run([sys.executable, script_path])
|
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)}")
|
||||||
|
|
||||||
# 设置每天12点运行
|
def main():
|
||||||
schedule.every().day.at("14:39").do(run_sendmsg)
|
"""主函数 - 设置定时任务"""
|
||||||
|
# 每天17:30执行任务
|
||||||
|
schedule.every().day.at("17:27").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()}] 定时任务已停止")
|
||||||
|
|
||||||
print("定时任务已启动,将在每天12:00运行sendmsg.py")
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
# 保持程序运行
|
|
||||||
while True:
|
|
||||||
schedule.run_pending()
|
|
||||||
time.sleep(1)
|
|
||||||
53
sendmsg.py
53
sendmsg.py
@ -6,30 +6,33 @@ import subprocess
|
|||||||
import json
|
import json
|
||||||
from config import CONFIG
|
from config import CONFIG
|
||||||
import time
|
import time
|
||||||
from config import CONFIG
|
|
||||||
|
|
||||||
|
def send_daily_message():
|
||||||
|
"""发送每日消息的主函数"""
|
||||||
|
today = date.today()
|
||||||
|
formatted_date = today.strftime('%Y-%m-%d')
|
||||||
|
file_name = formatted_date + ".png"
|
||||||
|
file_path = CONFIG['file_path'] + file_name
|
||||||
|
|
||||||
|
try:
|
||||||
|
wx = WeChat()
|
||||||
|
except:
|
||||||
|
subprocess.run([sys.executable, 'send_openmsg.py'])
|
||||||
|
return False
|
||||||
|
|
||||||
|
msg = 'hello, wxauto!'
|
||||||
|
who = CONFIG['messages_reciever']
|
||||||
|
|
||||||
|
if os.path.isfile(file_path):
|
||||||
|
print("找到了指定文件!")
|
||||||
|
wx.SendFiles(filepath=file_path, who="File Transfer")
|
||||||
|
wx.SendMsg(msg=msg, who=who)
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
print("没找到指定文件")
|
||||||
|
subprocess.run([sys.executable, 'send_filemsg.py'])
|
||||||
|
return False
|
||||||
|
|
||||||
today = date.today()
|
# 如果直接运行此脚本,执行发送消息功能
|
||||||
formatted_date = today.strftime('%Y-%m-%d')
|
if __name__ == "__main__":
|
||||||
file_name = formatted_date+".png"
|
send_daily_message()
|
||||||
file_path = CONFIG['file_path']+file_name
|
|
||||||
try:
|
|
||||||
wx = WeChat()
|
|
||||||
except:
|
|
||||||
subprocess.run([sys.executable, 'send_openmsg.py'])
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
|
|
||||||
msg = 'hello, wxauto!'
|
|
||||||
who = CONFIG['messages_reciever']
|
|
||||||
|
|
||||||
if os.path.isfile(file_path):
|
|
||||||
error_msg = ""
|
|
||||||
else:
|
|
||||||
error_msg = "没找到指定文件"
|
|
||||||
subprocess.run([sys.executable, 'send_filemsg.py'])
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
print("找到了指定文件!")
|
|
||||||
wx.SendFiles(filepath=file_path, who="File Transfer")
|
|
||||||
wx.SendMsg(msg=msg, who=who)
|
|
||||||
|
|||||||
@ -1,9 +0,0 @@
|
|||||||
@echo off
|
|
||||||
:: 查找并停止所有运行 scheduler.py 的 pythonw 进程
|
|
||||||
for /f "tokens=2" %%a in ('tasklist /fi "imagename eq pythonw.exe" /v ^| findstr "scheduler.py"') do taskkill /F /PID %%a
|
|
||||||
|
|
||||||
:: 等待1秒确保进程完全停止
|
|
||||||
timeout /t 1 /nobreak > nul
|
|
||||||
|
|
||||||
:: 启动新的进程
|
|
||||||
start /min pythonw "%~dp0scheduler.py"
|
|
||||||
Loading…
x
Reference in New Issue
Block a user