furniture-3d-system-Wu/furniture-3d-system/scripts/install-limit-workflow-sync-timer.sh
居居 e656fa6d5d feat(supply-chain): 限价流程同步改为服务器端可运行 + systemd 每日定时器
- sync-limit-workflows.mjs 路径参数化:新增 --out-dir/--initial-dir/--env-file/--skip-excel,
  环境变量 LIMIT_SYNC_OUT_DIR / LIMIT_SYNC_INITIAL_DIR / LIMIT_SYNC_ENV_FILE / LIMIT_SYNC_SKIP_EXCEL / LIMIT_SYNC_HISTORY_KEEP;
  默认目录改为应用根目录,不再写死 Mac 桌面
- 期初数据:优先显式目录,其次仓库自带 scripts/data/limit-sync/期初数据(首次部署可直接使用),
  服务器上业务更新只需把新的「限价期初数据*.xlsx」放进 shared 目录
- 写库安全:校验 DATABASE_URL、打印目标库(不含密码)、DELETE+INSERT 包在单事务里,
  失败自动回滚保留上一版限价/授权;Excel 导出失败不影响已提交的库数据
- 输出治理:输出目录自动创建;历史版本默认保留最近 20 份,避免服务器磁盘每天累积 30MB
- 新增 run-limit-workflow-sync-production.cjs:预检 shared/.env 变量(DATABASE_URL / JST_REPORT_DB_*)、
  打印库目标、透传参数、30 分钟硬超时、失败写 job 日志
- 新增 install-limit-workflow-sync-timer.sh:照 marketing-sales 样板安装 systemd service+timer,
  EnvironmentFile=$BASE/shared/.env,默认每天北京时间 07:30,输出/期初数据放 $BASE/shared/limit-workflow-sync
- 文档补充「正式站同步」章节;顺带修正 supply-chain-page-permissions 测试的菜单页数(新增限价管理后 27 页)
- 未改动 20261014/20261015 三个迁移目录(正式库已按现名建表)
2026-10-19 09:59:18 +08:00

85 lines
3.3 KiB
Bash
Executable File
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.

#!/usr/bin/env bash
# 限价流程同步 · 正式站 systemd 定时器安装脚本(与 marketing-sales timer 同一套样板)。
#
# 用途:每天把钉钉限价流程 / 产品限价页面已确认限价 / 限价期初数据同步到正式库
# (SupplyChainStyleLimitPrice、SupplyChainLimitFlowAuth),并输出全量明细 Excel。
#
# 前置:
# - 正式站目录:$FURNITURE_PROD_BASE(默认 /opt/furniture-3d-system-prod)
# - 环境变量:$BASE/shared/.env(DATABASE_URL / JST_REPORT_DB_* / PROD_ARCHIVE_DB_*)
# - 期初数据:默认从仓库自带 scripts/data/limit-sync/期初数据 复制到 shared 目录;
# 之后业务更新期初数据,直接把新的「限价期初数据*.xlsx」放进 shared 目录即可。
#
# 时间:默认每天北京时间 07:30(可用 LIMIT_SYNC_ON_CALENDAR 覆盖)。
# 破价订单扫描如果由别处调度,请安排在本任务之后。
set -euo pipefail
BASE="${FURNITURE_PROD_BASE:-/opt/furniture-3d-system-prod}"
APP_ROOT="$BASE/current/furniture-3d-system"
SERVICE="furniture-limit-workflow-sync"
SHARED_DIR="$BASE/shared/limit-workflow-sync"
OUT_DIR="$SHARED_DIR/导出结果"
INITIAL_DIR="$SHARED_DIR/期初数据"
LOG_DIR="$BASE/shared/logs/limit-workflow-sync"
ON_CALENDAR="${LIMIT_SYNC_ON_CALENDAR:-*-*-* 07:30:00 Asia/Shanghai}"
if [ "$(id -u)" -ne 0 ]; then echo "Root required" >&2; exit 1; fi
if [ ! -f "$APP_ROOT/scripts/run-limit-workflow-sync-production.cjs" ] || [ ! -f "$APP_ROOT/scripts/sync-limit-workflows.mjs" ]; then
echo "Missing deployed limit sync runner ($APP_ROOT/scripts)" >&2; exit 1
fi
if [ ! -f "$BASE/shared/.env" ]; then
echo "Missing $BASE/shared/.env(需要 DATABASE_URL / JST_REPORT_DB_* 等既有变量)" >&2; exit 1
fi
chmod 600 "$BASE/shared/.env"
mkdir -p "$OUT_DIR" "$INITIAL_DIR" "$LOG_DIR"
# 首次部署:把仓库自带的期初数据放到 shared 目录(已有文件不覆盖,方便业务更新)
if ! compgen -G "$INITIAL_DIR/限价期初数据*.xlsx" >/dev/null; then
if compgen -G "$APP_ROOT/scripts/data/limit-sync/期初数据/限价期初数据*.xlsx" >/dev/null; then
cp "$APP_ROOT"/scripts/data/limit-sync/期初数据/限价期初数据*.xlsx "$INITIAL_DIR/"
echo "已初始化期初数据:$INITIAL_DIR"
else
echo "警告:没有找到期初数据,本次只同步流程与产品限价页面数据" >&2
fi
fi
cat >"/etc/systemd/system/$SERVICE.service" <<EOF
[Unit]
Description=Synchronize limit workflow prices and price authorizations
After=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
WorkingDirectory=$APP_ROOT
EnvironmentFile=$BASE/shared/.env
Environment=NODE_ENV=production
Environment=LIMIT_SYNC_OUT_DIR=$OUT_DIR
Environment=LIMIT_SYNC_INITIAL_DIR=$INITIAL_DIR
Environment=LIMIT_SYNC_JOB_LOG_DIR=$LOG_DIR
Environment=LIMIT_SYNC_HISTORY_KEEP=20
ExecStart=/usr/bin/node scripts/run-limit-workflow-sync-production.cjs
TimeoutStartSec=35min
KillMode=control-group
EOF
cat >"/etc/systemd/system/$SERVICE.timer" <<EOF
[Unit]
Description=Limit workflow sync once a day at Beijing 07:30
[Timer]
OnCalendar=$ON_CALENDAR
OnBootSec=5min
AccuracySec=1s
Persistent=true
Unit=$SERVICE.service
[Install]
WantedBy=timers.target
EOF
systemctl daemon-reload
systemctl enable "$SERVICE.timer"
systemctl restart "$SERVICE.timer"
systemctl list-timers --all "$SERVICE*" --no-pager