提交 666bc596 authored 作者: PGY's avatar PGY

feat(scripts): 添加清理已删除文件的脚本及定时任务配置

上级 313f1a32
# CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
## Project Overview
This is an automated testing infrastructure project for meeting reservation systems (预定系统), operation control systems (运维集控), and unified platforms (统一平台). The project uses a custom `hytest` framework for test automation and includes PowerShell scripts for server maintenance and remote updates.
### Core Components
1. **hytest Framework** - A custom Python test framework (located in `hytest/`) that provides test execution, reporting, and case management
2. **预定系统 (Meeting Reservation System)** - Main test suite for reservation system with Selenium-based UI automation
3. **运维集控 (Operation Control)** - MQTT-based testing for IoT device control
4. **统一平台 (Unified Platform)** - Cross-platform meeting system tests
5. **AuxiliaryTool/ScriptTool** - PowerShell utilities for server health checks, remote updates, and automated deployments
6. **Mqtt_Universal_Tool** - Universal MQTT testing tools for device simulation and message handling
## Build and Test Commands
### Python Environment Setup
```bash
# Required Python version: 3.10.5
# Install required packages:
pip install hytest selenium webdriver-manager openpyxl psutil requests chardet pyyaml colorama python-docx tqdm paramiko pyinstaller appium
```
### Running Tests with hytest Framework
```bash
# From the 预定系统 directory:
# Run a single test module
hytest --report_title "测试报告名称" --test 会议预约_0**
# Run all modules with a specific tag
hytest --report_title "测试报告名称" --tag 后台管理功能
# Run all tests in cases directory
hytest --report_title "测试报告名称"
```
### PowerShell Script Execution
```powershell
# Service health inspection (服务自检)
cd AuxiliaryTool/ScriptTool/ServiceSelfInspection
.\check_server_health.ps1
# Remote container update
cd AuxiliaryTool/ScriptTool/RemoteUpdate
.\remote_update.ps1
```
### Appium Mobile Automation
```bash
# Required for mobile device testing in 展厅巡检
# Appium server must be running
# ADB connection to mobile devices is required
adb connect <device_ip>:5555
```
## Architecture and Code Organization
### Test Framework (hytest)
- `hytest/run.py` - Main entry point for test execution
- `hytest/utils/runner.py` - Test collector and runner
- `hytest/utils/log.py` - Logging utilities
- Test cases are organized as Python classes with `teststeps()` method
- Tags are used to group and filter tests
### PO (Page Object) Pattern
Common automation utilities are centralized in `Base/` directories:
- `base.py` - Selenium WebDriver utilities, browser initialization, element location
- `Mqtt_Send.py` - MQTT message sending and receiving utilities
- `app_base.py` - Appium mobile automation utilities
### Configuration Management
- `config.json` files store environment-specific configurations (URLs, credentials, IPs)
- Multi-environment support via JSON configuration files
- Dynamic environment loading for different deployment targets
### MQTT Testing Architecture
- MQTT client simulation for device testing (Android info, heartbeats, mmWave radar)
- Message body construction based on topic configuration
- Support for authentication (username/password)
### PowerShell Modules (AuxiliaryTool/ScriptTool)
Modular architecture with shared libraries in `lib/`:
- `log.ps1` - Logging utilities
- `ssh.ps1` - SSH connection management using plink.exe
- `file.ps1` - File operations
- `backup.ps1` - Backup and rollback
- `database.ps1` - Database operations
- `modules/*.psm1` - Individual check modules (ServiceCheck, MiddlewareCheck, ContainerCheck, etc.)
### Report Generation
- HTML reports automatically generated in `log/` directories
- Screenshot capture for test steps
- Integration with DingTalk (钉钉) for test notifications
- ngrok integration for public report access
## Custom Skills
Two custom skills are available in `.claude/skills/`:
### `/prd-plan`
Parses PRD (Product Requirement Document) and generates execution plan documents.
```
/prd-plan "Docs/PRD/服务自检/_PRD_服务自检需求文档.md"
```
### `/prd-code`
Parses execution plan documents and generates or updates code.
```
/prd-code "Docs/PRD/服务自检/_PRD_服务自检需求文档_计划执行.md"
```
## Code Standards
Based on `Docs/PRD/01规范文档/_PRD_规范文档_代码规范.md`:
1. **Comments**: All code must have Chinese comments explaining functionality
2. **Compatibility**: New code must not break existing functionality
3. **Logging**: Detailed logging with proper levels (INFO, ERROR, WARN) for audit trails
## Document Standards
Based on `Docs/PRD/01规范文档/_PRD_规范文档_文档规范.md`:
1. **Structure**: Clear markdown heading hierarchy
2. **Status Tracking**: Use checkboxes `[x]` for implemented, `[ ]` for pending
3. **Code Blocks**: Use markdown with language specification for syntax highlighting
4. **Optimization Tracking**: "优化功能回填" section at document end for tracking improvements
## Key Testing Patterns
### Selenium WebDriver Pattern
```python
from hytest import *
from selenium import webdriver
class TestCase:
def teststeps(self):
# Browser initialization
wd = webdriver.Chrome()
wd.implicitly_wait(60)
# Test steps with screenshots
# ...
SELENIUM_LOG_SCREEN(wd) # Capture screenshot
```
### MQTT Testing Pattern
```python
from Base.Mqtt_Send import Mqtt_Send
# Initialize MQTT client
mqtt_client = Mqtt_Send(broker, port, username, password)
mqtt_client.build_message(topic, payload)
mqtt_client.publish(topic)
```
### Appium Mobile Pattern
```python
# For mobile automation in 展厅巡检
from appium import webdriver
# ADB connection setup
# Appium driver initialization
# Mobile element interaction
```
## Important Notes
- **ChromeDriver**: Manual path specification required (see `预定系统/Base/base.py:88`)
- **Element Locators**: CSS selectors preferred over XPath due to internationalization
- **Window Handles**: Proper window switching required for multi-window scenarios
- **ADB Connections**: Automatically managed, with disconnection after mobile operations
- **Encoding**: UTF-8 encoding critical for PowerShell scripts (set via `$OutputEncoding`)
- **Test Data**: CSV and JSON files for data-driven testing in `测试数据/` directories
## Common Issues
1. **Browser Driver Mismatch**: Chrome version updates require matching ChromeDriver version
2. **Element Locator Changes**: UI updates may require locator updates (CSS preferred)
3. **ADB Connection Drops**: Network issues can cause ADB disconnection during mobile tests
4. **ngrok Mapping**: IP changes require ngrok restart for report access
5. **Encoding Issues**: Chinese characters may display incorrectly if UTF-8 not properly set
#!/bin/bash
# ================= 配置区域 =================
TARGET_KEY="ubains-INFO-AND-ERROR"
MIN_SIZE=$((1024*1024*1024)) # 1GB (单位:字节)
LOG_FILE="$(dirname "$0")/auto_clean_deleted_ubains.log"
MAX_LOG_SIZE=$((5*1024*1024)) # 5MB 日志大小限制
LOG_RETENTION_DAYS=7 # 日志保留天数
CONTAINER_NAME="ujava2" # 容器名称
# ===========================================
# 日志函数
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" | tee -a "$LOG_FILE"
}
# 日志轮转
rotate_logs() {
if [ -f "$LOG_FILE" ]; then
FILE_SIZE=$(stat -c%s "$LOG_FILE")
if [ "$FILE_SIZE" -ge "$MAX_LOG_SIZE" ]; then
mv "$LOG_FILE" "$LOG_FILE.$(date '+%Y%m%d%H%M%S')"
touch "$LOG_FILE"
log "日志文件超过 5MB,已自动轮转。"
fi
fi
find "$(dirname "$0")" -name "auto_clean_deleted_ubains.log.*" -mtime +$LOG_RETENTION_DAYS -exec rm -f {} \;
}
rotate_logs
log "==============================================="
log "开始扫描:检测 deleted 大文件并自动清理 (内核直读版)"
log "匹配关键字: $TARGET_KEY"
log "大于尺寸: 1GB"
log "==============================================="
FOUND=0
NEED_RESTART=0
# 遍历所有进程的 fd 目录,寻找匹配关键字且标记为 deleted 的文件
for fd_path in /proc/[0-9]*/fd/*; do
# 检查是否为符号链接且指向包含关键字的已删除文件
if [ -L "$fd_path" ]; then
target_file=$(readlink "$fd_path" 2>/dev/null)
if [[ "$target_file" == *"(deleted)"* ]] && [[ "$target_file" == *"$TARGET_KEY"* ]]; then
FOUND=1
# 提取 PID 和 FD
pid=$(echo "$fd_path" | cut -d'/' -f3)
fd=$(echo "$fd_path" | cut -d'/' -f5)
# 获取进程名
proc_name="unknown"
[ -f "/proc/$pid/comm" ] && proc_name=$(cat "/proc/$pid/comm")
# 获取文件大小 (字节) - 使用 stat -L 获取链接指向的实际文件状态
size_bytes=$(stat -L -c %s "$fd_path" 2>/dev/null || echo 0)
size_mb=$((size_bytes / 1024 / 1024))
log "-----------------------------------------------"
log "发现匹配文件:"
log "进程: $proc_name"
log "PID: $pid"
log "FD: $fd"
log "文件: $target_file"
log "大小: $size_mb MB"
if [ "$size_bytes" -ge "$MIN_SIZE" ]; then
log "⚠ 文件超过1GB,执行自动处理。"
log "➡ 杀死进程 PID: $pid"
kill -9 "$pid" 2>/dev/null
sleep 1
NEED_RESTART=1
else
log "⏩ 文件不足 1GB,跳过。"
fi
fi
fi
done
if [ "$FOUND" -eq 0 ]; then
log "未发现匹配的 deleted 文件。"
fi
if [ "$NEED_RESTART" -eq 1 ]; then
log "➡ 检测到大文件进程已处理,统一重启 docker 容器:$CONTAINER_NAME"
# 检查 docker 命令是否存在
if command -v docker >/dev/null 2>&1; then
docker restart "$CONTAINER_NAME" >> "$LOG_FILE" 2>&1
log "✔ Docker 容器重启完成。"
else
log "❌ 错误: 未找到 docker 命令,请手动重启容器。"
fi
else
log "无需重启 docker 容器。"
fi
log "🎉 执行结束。"
log "==============================================="
......@@ -46,7 +46,9 @@ crontab -e
# 每日凌晨4点检查并清理被占用的已删除文件
# 功能:查找link count为0但仍被进程占用的文件,发送SIGHUP信号释放句柄
# 说明:提供两种清理脚本,分别针对普通文件和预定ubains相关文件,避免误杀进程
0 4 * * * /opt/scripts/cleanup_deleted_files.sh
0 4 * * * /opt/scripts/clean_deleted_ubains_v2.sh
# 每5分钟监测EMQX服务状态
# 功能:检查uemqx容器运行状态、EMQX进程存在性和服务可用性,异常时自动重启
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论