Files
ewoooc/scripts/ppt_cleanup.sh
OoO 3b0b4b3d42
All checks were successful
CD Pipeline / deploy (push) Successful in 2m27s
fix(ppt): address critic findings on commit 38967ce (HIGH-1, HIGH-2, Medium-4)
Critic 審查 38967ce 找出 2 HIGH + 4 medium,本 commit 修最緊急三項:

HIGH-2: cleanup_expired_ppt_cache 預設 dry_run=False 危險
- 函式 default 改 dry_run=True(呼叫方需明確傳 False 才實刪)
- launchd 排程腳本改用 DRY_RUN 環境變數,預設 false→實刪,
  但測試時可 DRY_RUN=true 安全跑
- /cache cleanup Telegram 指令預設乾跑,需加 confirm 才實刪
- /cache cleanup days<1 強制乾跑(防呆)

HIGH-1: matplotlib 三個 helper 缺 try/finally → 渲染失敗洩漏 figure
- _mpl_horiz_bar_png / _mpl_line_chart_png / _mpl_pareto_chart_png
  全部用 try/except/finally 包住,例外時 plt.close() 仍執行
- 同時讓渲染失敗回 None(呼叫端自然 fallback 到原生 chart)

Medium-4: scripts/ppt_cleanup.sh PROJECT_DIR 寫死
- 改用相對路徑解析(cd $SCRIPT_DIR/..),手動執行不再需要設環境變數
- VENV_PY 找不到時 fallback 到系統 python3(容器友善)
- 新增 DRY_RUN 環境變數開關(預設 false)

煙霧測試:
- syntax OK (services/ppt_generator.py + routes/openclaw_bot_routes.py)
- monthly v3.1 重生 10 頁 290KB
- cleanup_expired_ppt_cache dry_run default = True ✓

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 17:23:02 +08:00

51 lines
1.8 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# scripts/ppt_cleanup.sh
# 由 launchd / cron 呼叫 — 清理 N 天前過期的 PPT 檔 + DB row。
# 安全執行:失敗時不會 raise只寫 log。
#
# 環境變數(皆可在 launchd plist / 命令列覆寫):
# PROJECT_DIR source code 根目錄(預設:相對路徑解析至此腳本上層)
# VENV_PY python 直譯器(預設:${PROJECT_DIR}/venv/bin/python3找不到則 fallback python3
# DAYS_OLD 過期門檻天數(預設 7
# DRY_RUN true/false預設 false—launchd 排程實刪;手動測試請傳 DRY_RUN=true
set -euo pipefail
# 動態解析路徑critic Medium-4 修正):手動執行不再需要設環境變數
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="${PROJECT_DIR:-$(cd "$SCRIPT_DIR/.." && pwd)}"
# venv python找不到時 fallback 到系統 python3
DEFAULT_VENV_PY="${PROJECT_DIR}/venv/bin/python3"
if [[ -x "$DEFAULT_VENV_PY" ]]; then
VENV_PY="${VENV_PY:-$DEFAULT_VENV_PY}"
else
VENV_PY="${VENV_PY:-python3}"
fi
LOG_FILE="${LOG_FILE:-${PROJECT_DIR}/logs/ppt_cleanup.log}"
DAYS_OLD="${DAYS_OLD:-7}"
DRY_RUN="${DRY_RUN:-false}"
# 將字串轉為 Python bool literal
if [[ "$DRY_RUN" =~ ^(true|TRUE|1|yes|YES)$ ]]; then
PY_DRY_RUN="True"
else
PY_DRY_RUN="False"
fi
mkdir -p "$(dirname "$LOG_FILE")"
cd "$PROJECT_DIR"
{
echo "[$(date +'%Y-%m-%d %H:%M:%S')] PPT cleanup start (days_old=$DAYS_OLD, dry_run=$PY_DRY_RUN, py=$VENV_PY)"
"$VENV_PY" -c "
from routes.openclaw_bot_routes import cleanup_expired_ppt_cache
import json
stat = cleanup_expired_ppt_cache(days_old=$DAYS_OLD, dry_run=$PY_DRY_RUN)
print('result:', json.dumps(stat, ensure_ascii=False))
" 2>&1 || echo "[ERROR] cleanup failed"
echo "[$(date +'%Y-%m-%d %H:%M:%S')] PPT cleanup end"
echo "---"
} >> "$LOG_FILE"