修改清理文件逻辑,改到main_start.py中
This commit is contained in:
134
main_start.py
134
main_start.py
@@ -25,6 +25,9 @@ PID_DIR = "pids"
|
||||
# HLS下载器PID文件前缀
|
||||
HLS_DOWNLOADER_PID_PREFIX = "hls_downloader_"
|
||||
|
||||
# HLS清理进程PID文件
|
||||
HLS_CLEANUP_PID_FILE = "hls_cleanup.pid"
|
||||
|
||||
|
||||
def load_debug_mode(config_path: str = "config.yaml") -> bool:
|
||||
"""从配置文件读取调试模式"""
|
||||
@@ -164,6 +167,36 @@ def read_hls_downloader_pid(index_code: str):
|
||||
return None
|
||||
|
||||
|
||||
def get_hls_cleanup_pid_file_path() -> str:
|
||||
"""获取HLS清理进程的 PID 文件路径"""
|
||||
return os.path.join(PID_DIR, HLS_CLEANUP_PID_FILE)
|
||||
|
||||
|
||||
def save_hls_cleanup_pid(pid: int):
|
||||
"""保存HLS清理进程ID到文件"""
|
||||
ensure_pid_dir()
|
||||
pid_file = get_hls_cleanup_pid_file_path()
|
||||
try:
|
||||
with open(pid_file, "w") as f:
|
||||
f.write(str(pid))
|
||||
logger.info(f"[INFO] Saved HLS cleanup PID {pid} to {pid_file}")
|
||||
except Exception as e:
|
||||
logger.error(f"[ERROR] Failed to save HLS cleanup PID file: {e}")
|
||||
|
||||
|
||||
def read_hls_cleanup_pid():
|
||||
"""从PID文件读取HLS清理进程ID"""
|
||||
pid_file = get_hls_cleanup_pid_file_path()
|
||||
try:
|
||||
with open(pid_file, "r") as f:
|
||||
return int(f.read().strip())
|
||||
except FileNotFoundError:
|
||||
return None
|
||||
except Exception as e:
|
||||
logger.error(f"[ERROR] Failed to read HLS cleanup PID file: {e}")
|
||||
return None
|
||||
|
||||
|
||||
def is_process_running(pid: int):
|
||||
"""检查进程是否在运行"""
|
||||
try:
|
||||
@@ -248,7 +281,6 @@ def start_hls_downloader(camera: dict, hls_config: dict) -> Optional[subprocess.
|
||||
"--camera-id", str(camera_id),
|
||||
"--hls-root-path", hls_config.get("hls_root_path", ""),
|
||||
"--rotate-hour", str(hls_config.get("daily_rotate_hour", 3)),
|
||||
"--retention-days", str(hls_config.get("retention_days", 3)),
|
||||
"--retry-interval", str(hls_config.get("retry_interval", 10))
|
||||
]
|
||||
|
||||
@@ -262,6 +294,39 @@ def start_hls_downloader(camera: dict, hls_config: dict) -> Optional[subprocess.
|
||||
return process
|
||||
|
||||
|
||||
def start_hls_cleanup(hls_config: dict) -> Optional[subprocess.Popen]:
|
||||
"""启动HLS清理进程"""
|
||||
hls_root_path = hls_config.get("hls_root_path", "")
|
||||
retention_days = hls_config.get("retention_days", 3)
|
||||
|
||||
if not hls_root_path:
|
||||
logger.warning("[WARN] HLS root path not configured, skipping HLS cleanup")
|
||||
return None
|
||||
|
||||
# 检查是否已经在运行
|
||||
pid = read_hls_cleanup_pid()
|
||||
if pid and is_process_running(pid):
|
||||
logger.warning(f"[WARN] HLS cleanup is already running with PID {pid}")
|
||||
return None
|
||||
|
||||
cmd = [
|
||||
sys.executable,
|
||||
"hls_cleanup.py",
|
||||
"--hls-root-path", hls_root_path,
|
||||
"--retention-days", str(retention_days),
|
||||
"--interval", "1" # 每小时清理一次
|
||||
]
|
||||
|
||||
logger.info(f"[INFO] Starting HLS cleanup service (retention: {retention_days} days)")
|
||||
|
||||
if DOWNLOADER_DEBUG_MODE:
|
||||
process = subprocess.Popen(cmd)
|
||||
else:
|
||||
process = subprocess.Popen(cmd, start_new_session=True)
|
||||
|
||||
return process
|
||||
|
||||
|
||||
def start_service():
|
||||
"""启动所有服务组"""
|
||||
config_path = "config.yaml"
|
||||
@@ -325,6 +390,18 @@ def start_service():
|
||||
logger.info(f"[INFO] Started {started_count}/{len(service_groups)} service groups")
|
||||
logger.info(f"[INFO] Started {downloader_count} HLS downloaders")
|
||||
|
||||
# 启动HLS清理进程
|
||||
cleanup_process = None
|
||||
if hls_config.get("hls_root_path"):
|
||||
try:
|
||||
cleanup_process = start_hls_cleanup(hls_config)
|
||||
if cleanup_process:
|
||||
time.sleep(0.3)
|
||||
save_hls_cleanup_pid(cleanup_process.pid)
|
||||
logger.info(f"[INFO] HLS cleanup service started with PID {cleanup_process.pid}")
|
||||
except Exception as e:
|
||||
logger.error(f"[ERROR] Failed to start HLS cleanup service: {e}")
|
||||
|
||||
# 根据各自的调试模式决定是否等待子进程
|
||||
if DEBUG_MODE and processes:
|
||||
logger.info("[DEBUG] Waiting for service processes...")
|
||||
@@ -336,6 +413,10 @@ def start_service():
|
||||
for process, name in downloader_processes:
|
||||
process.wait()
|
||||
|
||||
if DOWNLOADER_DEBUG_MODE and cleanup_process:
|
||||
logger.info("[DEBUG] Waiting for cleanup process...")
|
||||
cleanup_process.wait()
|
||||
|
||||
return started_count > 0
|
||||
|
||||
|
||||
@@ -398,6 +479,18 @@ def status_service():
|
||||
if downloader_pids:
|
||||
logger.info(f"[INFO] {downloader_running}/{len(downloader_pids)} HLS downloaders running")
|
||||
|
||||
# 检查HLS清理进程状态
|
||||
cleanup_pid = read_hls_cleanup_pid()
|
||||
if cleanup_pid and is_process_running(cleanup_pid):
|
||||
logger.info(f"[INFO] HLS cleanup service is running with PID {cleanup_pid}")
|
||||
else:
|
||||
logger.info(f"[INFO] HLS cleanup service is not running")
|
||||
if cleanup_pid:
|
||||
try:
|
||||
os.remove(get_hls_cleanup_pid_file_path())
|
||||
except:
|
||||
pass
|
||||
|
||||
return running_count > 0
|
||||
|
||||
|
||||
@@ -511,6 +604,45 @@ def stop_service(force=False):
|
||||
if downloader_pids:
|
||||
logger.info(f"[INFO] Stopped {downloader_stopped}/{len(downloader_pids)} HLS downloaders")
|
||||
|
||||
# 停止HLS清理进程
|
||||
cleanup_pid = read_hls_cleanup_pid()
|
||||
if cleanup_pid:
|
||||
if not is_process_running(cleanup_pid):
|
||||
logger.warning(f"[WARN] HLS cleanup PID {cleanup_pid} not running, cleaning up")
|
||||
try:
|
||||
os.remove(get_hls_cleanup_pid_file_path())
|
||||
except:
|
||||
pass
|
||||
else:
|
||||
try:
|
||||
if force:
|
||||
logger.info(f"[INFO] Force killing HLS cleanup (PID {cleanup_pid})")
|
||||
os.kill(cleanup_pid, signal.SIGKILL)
|
||||
else:
|
||||
logger.info(f"[INFO] Stopping HLS cleanup (PID {cleanup_pid})")
|
||||
os.kill(cleanup_pid, signal.SIGTERM)
|
||||
|
||||
# 等待进程结束
|
||||
for i in range(10):
|
||||
if not is_process_running(cleanup_pid):
|
||||
break
|
||||
time.sleep(1)
|
||||
|
||||
if is_process_running(cleanup_pid):
|
||||
logger.warning("[WARN] Force killing HLS cleanup")
|
||||
os.kill(cleanup_pid, signal.SIGKILL)
|
||||
time.sleep(1)
|
||||
|
||||
# 清理PID文件
|
||||
try:
|
||||
os.remove(get_hls_cleanup_pid_file_path())
|
||||
except:
|
||||
pass
|
||||
|
||||
logger.info("[INFO] HLS cleanup stopped")
|
||||
except Exception as e:
|
||||
logger.error(f"[ERROR] Failed to stop HLS cleanup: {e}")
|
||||
|
||||
return True
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user