CaiWuXiTong/scripts/cmb-mail/run-server.sh

179 lines
5.0 KiB
Bash
Raw 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.

#!/bin/sh
set -eu
BASE_DIR="${CMB_MAIL_BASE_DIR:-/opt/caiwuxitong/runtime/private/cmb-mail}"
CONFIG_FILE="$BASE_DIR/config.json"
FETCH_SCRIPT="$BASE_DIR/fetch_daily.py"
PYTHON_PATH="$BASE_DIR/python"
DATE="${1:-$(TZ=Asia/Shanghai date -d yesterday +%F)}"
STAMP="$(TZ=Asia/Shanghai date +%Y%m%d-%H%M%S)"
RUN_DIR="$BASE_DIR/exports/$DATE/server-$STAMP"
LOG_DIR="$BASE_DIR/logs"
LOG_FILE="$LOG_DIR/$(TZ=Asia/Shanghai date +%F).log"
STATE_FILE="$RUN_DIR/last-fetch.json"
IMPORT_MARKER="$BASE_DIR/state/$DATE.imported.json"
FAILURE_TYPE="unknown"
FAILURE_REASON="招商银行邮件任务执行异常,具体错误已写入服务器日志。"
umask 077
mkdir -p "$RUN_DIR" "$LOG_DIR" "$BASE_DIR/state"
exec >>"$LOG_FILE" 2>&1
log() {
echo "[$(TZ=Asia/Shanghai date '+%F %T')] $*"
}
notify_failure() {
if [ "${CMB_MAIL_NOTIFY_ON_FAILURE:-0}" != "1" ]; then
return 0
fi
recipient="${CMB_MAIL_ALERT_RECIPIENT:-}"
if [ -z "$recipient" ]; then
log "最终失败,但未配置 CMB_MAIL_ALERT_RECIPIENT,无法发送服务器提醒"
return 0
fi
log "发送服务器最终失败提醒"
if docker exec caiwu_app php artisan cmb-mail:notify-failure \
--date="$DATE" \
--type="$FAILURE_TYPE" \
--reason="$FAILURE_REASON" \
--recipient="$recipient"; then
log "服务器最终失败提醒已发送"
else
log "服务器最终失败提醒发送失败,请检查钉钉应用通知权限"
fi
}
on_exit() {
status=$?
trap - EXIT
if [ "$status" -ne 0 ]; then
notify_failure || true
fi
exit "$status"
}
fail() {
FAILURE_TYPE="$1"
FAILURE_REASON="$2"
log "招商银行邮件任务失败 [$FAILURE_TYPE]:$FAILURE_REASON"
exit 1
}
trap on_exit EXIT
if [ ! -f "$CONFIG_FILE" ]; then
fail "config" "缺少招商银行邮件配置。"
fi
if [ ! -f "$FETCH_SCRIPT" ]; then
fail "config" "缺少招商银行邮件抓取脚本。"
fi
log "开始获取招商银行邮件账单 $DATE"
fetch_status=0
PYTHONPATH="$PYTHON_PATH" python3 -X utf8 "$FETCH_SCRIPT" fetch \
--config "$CONFIG_FILE" \
--date "$DATE" \
--out "$BASE_DIR/exports/$DATE" \
--run-dir "$RUN_DIR" || fetch_status=$?
if [ "$fetch_status" -ne 0 ]; then
if [ -f "$STATE_FILE" ]; then
failure_fields="$(
python3 - "$STATE_FILE" <<'PY'
import json
import sys
with open(sys.argv[1], encoding="utf-8") as stream:
state = json.load(stream)
print(str(state.get("error_type") or "unknown").replace("\n", " "))
print(str(state.get("reason") or "服务器日志已记录具体错误。").replace("\n", " "))
PY
)"
FAILURE_TYPE="$(printf '%s\n' "$failure_fields" | sed -n '1p')"
FAILURE_REASON="$(printf '%s\n' "$failure_fields" | sed -n '2p')"
fi
fail "${FAILURE_TYPE:-unknown}" "${FAILURE_REASON:-服务器日志已记录具体错误。}"
fi
if [ ! -f "$STATE_FILE" ]; then
fail "unknown" "抓取任务没有生成状态文件。"
fi
archive_sha="$(
python3 - "$STATE_FILE" <<'PY'
import json
import sys
with open(sys.argv[1], encoding="utf-8") as stream:
state = json.load(stream)
print(state.get("archive_sha256") or "")
PY
)"
if [ -z "$archive_sha" ]; then
fail "unknown" "招商银行邮件状态文件缺少账单指纹。"
fi
if [ -f "$IMPORT_MARKER" ]; then
imported_sha="$(
python3 - "$IMPORT_MARKER" <<'PY'
import json
import sys
with open(sys.argv[1], encoding="utf-8") as stream:
state = json.load(stream)
print(state.get("archive_sha256") or "")
PY
)"
if [ "$imported_sha" = "$archive_sha" ]; then
log "当天相同招商银行账单已导入,跳过重复处理"
exit 0
fi
fi
umask 022
statement_directory="$(
python3 - "$STATE_FILE" <<'PY'
import json
import sys
from pathlib import Path
with open(sys.argv[1], encoding="utf-8") as stream:
state = json.load(stream)
files = [item.get("path") for item in state.get("files") or [] if isinstance(item, dict)]
print(str(Path(files[0]).parent) if files else "")
PY
)"
if [ -z "$statement_directory" ] || [ ! -d "$statement_directory" ]; then
fail "archive" "招商银行 ZIP 中没有可导入文件。"
fi
log "开始一次导入招商银行账单目录:$statement_directory"
if ! /bin/sh /opt/caiwuxitong/run-finance-batch.sh \
finance-ledger:import-bank-bills-directory \
"$statement_directory" \
--bank=招商银行; then
fail "import" "银行账单导入失败。"
fi
python3 - "$STATE_FILE" "$IMPORT_MARKER" <<'PY'
import json
import sys
from datetime import datetime
from zoneinfo import ZoneInfo
with open(sys.argv[1], encoding="utf-8") as stream:
state = json.load(stream)
state["imported_files"] = len(state.get("files") or [])
state["imported_at"] = datetime.now(ZoneInfo("Asia/Shanghai")).isoformat(timespec="seconds")
with open(sys.argv[2], "w", encoding="utf-8") as stream:
json.dump(state, stream, ensure_ascii=False, indent=2)
stream.write("\n")
PY
log "招商银行邮件账单已自动接入银行账单"