xinxin
This commit is contained in:
parent
fb88fcbbf8
commit
2e9007a16f
Binary file not shown.
@ -48,3 +48,4 @@ typing_extensions==4.1.1
|
||||
urllib3==1.26.20
|
||||
xlrd==1.2.0
|
||||
zipp==3.6.0
|
||||
redis==4.3.6
|
107
scheduler_init_proxy.py
Normal file
107
scheduler_init_proxy.py
Normal file
@ -0,0 +1,107 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
定时执行 init_proxy.py 脚本
|
||||
每整十分钟执行一次(00、10、20、30、40、50分)
|
||||
"""
|
||||
|
||||
import time
|
||||
import datetime
|
||||
import subprocess
|
||||
import sys
|
||||
import os
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
# 设置日志
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s - %(levelname)s - %(message)s',
|
||||
handlers=[
|
||||
logging.FileHandler('scheduler_init_proxy.log', encoding='utf-8'),
|
||||
logging.StreamHandler()
|
||||
]
|
||||
)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
def execute_init_proxy():
|
||||
"""执行 init_proxy.py 脚本"""
|
||||
try:
|
||||
script_path = Path(__file__).parent / "init_proxy.py"
|
||||
if not script_path.exists():
|
||||
logger.error(f"脚本文件不存在: {script_path}")
|
||||
return False
|
||||
|
||||
# 使用当前 Python 解释器执行脚本 - 兼容老版本Python
|
||||
result = subprocess.run(
|
||||
[sys.executable, str(script_path)],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
universal_newlines=True
|
||||
)
|
||||
|
||||
if result.returncode == 0:
|
||||
logger.info("init_proxy.py 执行成功")
|
||||
if result.stdout and result.stdout.strip():
|
||||
logger.info(f"输出: {result.stdout.strip()}")
|
||||
else:
|
||||
logger.error(f"init_proxy.py 执行失败,返回码: {result.returncode}")
|
||||
if result.stderr and result.stderr.strip():
|
||||
logger.error(f"错误信息: {result.stderr.strip()}")
|
||||
|
||||
return result.returncode == 0
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"执行 init_proxy.py 时发生异常: {e}")
|
||||
return False
|
||||
|
||||
def wait_for_next_execution():
|
||||
"""等待到下一个整十分钟"""
|
||||
now = datetime.datetime.now()
|
||||
current_minute = now.minute
|
||||
|
||||
# 计算下一个整十分钟
|
||||
next_minute = ((current_minute // 10) + 1) * 10
|
||||
|
||||
# 处理小时进位
|
||||
if next_minute >= 60:
|
||||
next_minute = 0
|
||||
next_execution = now.replace(minute=0, second=0, microsecond=0) + datetime.timedelta(hours=1)
|
||||
else:
|
||||
next_execution = now.replace(minute=next_minute, second=0, microsecond=0)
|
||||
|
||||
wait_seconds = (next_execution - now).total_seconds()
|
||||
logger.info(f"当前时间: {now.strftime('%Y-%m-%d %H:%M:%S')}")
|
||||
logger.info(f"下次执行时间: {next_execution.strftime('%Y-%m-%d %H:%M:%S')}")
|
||||
logger.info(f"等待 {wait_seconds:.0f} 秒")
|
||||
|
||||
return wait_seconds
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
logger.info("代理重置定时器启动")
|
||||
logger.info("脚本将在每整十分钟执行一次 init_proxy.py")
|
||||
|
||||
# 首次执行
|
||||
logger.info("执行首次代理重置...")
|
||||
execute_init_proxy()
|
||||
|
||||
# 定时循环
|
||||
while True:
|
||||
try:
|
||||
wait_seconds = wait_for_next_execution()
|
||||
time.sleep(wait_seconds)
|
||||
|
||||
logger.info("开始执行定时任务...")
|
||||
execute_init_proxy()
|
||||
|
||||
except KeyboardInterrupt:
|
||||
logger.info("接收到中断信号,程序退出")
|
||||
break
|
||||
except Exception as e:
|
||||
logger.error(f"定时器运行异常: {e}")
|
||||
# 发生异常时等待60秒后继续
|
||||
time.sleep(60)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
x
Reference in New Issue
Block a user