Skip to content

Commit

Permalink
add remove log
Browse files Browse the repository at this point in the history
  • Loading branch information
jlcoo committed Nov 12, 2024
1 parent 500cfa1 commit 08c49f4
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
from datetime import datetime


def remove_logs_dirs(log_dir='.'):
def remove_logs_dirs(log_dir='.', max_day=2):
# 获取当前日期
current_date = datetime.now()
max_day = 2
# print("remove_submit_logs start")
# 遍历目录下的所有文件
for filename in os.listdir(log_dir):
Expand Down Expand Up @@ -40,4 +39,4 @@ def remove_logs_dirs(log_dir='.'):


if __name__ == "__main__":
remove_submit_logs()
remove_logs_dirs()
4 changes: 4 additions & 0 deletions easypackages/watch_update_source/build_image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

# /usr/bin/docker build --no-cache -t openeuler-pypi-package-update:latest .
/usr/bin/docker build -t openeuler-pypi-package-update .
42 changes: 42 additions & 0 deletions easypackages/watch_update_source/task/remove_submit_logs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import os
import re
import shutil
from datetime import datetime


def remove_logs_dirs(log_dir='.', max_day=2):
# 获取当前日期
current_date = datetime.now()
# print("remove_submit_logs start")
# 遍历目录下的所有文件
for filename in os.listdir(log_dir):
if filename.startswith('submit-log'): # 确保文件名长度正确
print(filename)
try:
date_match = re.search(r'\d{8}', filename)
if date_match:
date_str = date_match.group(0)
else:
continue
file_datetime = datetime.strptime(date_str, '%Y%m%d')
except ValueError:
print(f'Filename {filename} does not match, skipping.')
continue

# 计算文件日期与当前日期的差值
delta = current_date - file_datetime
print(delta.days)
# 如果文件日期超过max_day天,则删除该文件
if delta.days > max_day:
file_path = os.path.join(log_dir, filename)
# print(f'Deleting {filename} older than {max_day} days.')
try:
shutil.rmtree(file_path)
except Exception as e:
# 捕获其他异常
print(f"删除 {file_path} 时发生错误: {e}")
continue


if __name__ == "__main__":
remove_logs_dirs("/root/easypackages/easypackages/watch_update_source/log", 3)
3 changes: 3 additions & 0 deletions easypackages/watch_update_source/task/schdule.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import time

import schedule
import remove_submit_logs


def check_process_running(script_name):
Expand All @@ -20,6 +21,8 @@ def check_process_running(script_name):
def run_shell_script():
task_job_path = os.path.join(os.path.dirname(__file__), 'task_crontab.sh')
script_name = os.path.basename(task_job_path)
remove_submit_logs.remove_logs_dirs(
"/root/easypackages/easypackages/watch_update_source/log", 30)
if not check_process_running(script_name):
try:
subprocess.Popen(["sh", task_job_path])
Expand Down

0 comments on commit 08c49f4

Please sign in to comment.