修改可在config文件自定义运行时间

This commit is contained in:
panjunlan
2026-02-22 14:44:52 +08:00
parent e505148026
commit 375a54187d
4 changed files with 100 additions and 115 deletions

20
main.py
View File

@@ -1,7 +1,7 @@
import time
from apscheduler.schedulers.background import BackgroundScheduler
from utils.logger import logger
from config.settings import YAML_OUTPUT_PATH
from config.settings import YAML_OUTPUT_PATH,SCHEDULE_EXPORT, SCHEDULE_DELETE
from core.get_vm_snapshots import get_all_vms
from core.data_exporter import create_excel_report, export_yaml
from core.remove_snapshots import load_old_snapshots, remove_snapshot
@@ -32,19 +32,14 @@ def delete_old_snapshots():
def main():
"""主执行函数"""
export_conf = SCHEDULE_EXPORT # 从配置读取导出任务时间
delete_conf = SCHEDULE_DELETE # 从配置读取删除任务时间
# 设置定时任务
scheduler = BackgroundScheduler()
scheduler = BackgroundScheduler() # 设置定时任务调度器
scheduler.start() # 开始调度器
# 每周六凌晨4点导出Excel和Yaml文件
scheduler.add_job(export_files, 'cron', day_of_week='sat', hour=4, minute=0)
# 每周六晚上7点执行删除快照任务
scheduler.add_job(delete_old_snapshots, 'cron', day_of_week='sat', hour=19, minute=0)
scheduler.start()
logger.info("定时任务已设置每周六凌晨4点导出文件晚上7点删除快照")
logger.info(f"✓ 导出任务已设置: 每周 {export_conf['day_of_week']} {export_conf['hour']:02d}:{export_conf['minute']:02d}") # :02d 数字格式化选项用于让数字显示为至少2位不足补零。
logger.info(f"✓ 删除任务已设置: 每周 {delete_conf['day_of_week']} {delete_conf['hour']:02d}:{delete_conf['minute']:02d}")
try:
# 保持主程序运行,以便调度器能正常工作
@@ -57,3 +52,4 @@ def main():
if __name__ == "__main__":
main()