feat(supply-chain): 新增替换款规则每日同步定时器安装脚本

SupplyChainProductArchiveReplacementRule 的上游是 rpt.pm_replacementManage,
此前只有 local-sync-archive.mjs 会调用 sync-product-replacement-rules.ts,
正式机没有任何定时任务,表建好后仍然是空的,SABC 的「同款商品编码」也就一直是空。

新增 install-product-replacement-rule-sync-timer.sh,按仓库既有 install-*-timer.sh
同款写法安装 furniture-product-replacement-rule-sync.{service,timer},
默认每天 07:40(Asia/Shanghai)同步一次,早于 09:00 的 SABC 打标日结。
脚本仅安装定时任务,改动生产数据由 sync-product-replacement-rules.ts 自己完成。
This commit is contained in:
WuYongJie 2026-10-08 16:43:37 +08:00
parent 1bea953315
commit 13b81e5822

View File

@ -0,0 +1,71 @@
#!/usr/bin/env bash
set -euo pipefail
# Installs the daily sync timer for the JST replacement rules table
# (rpt.pm_replacementManage -> SupplyChainProductArchiveReplacementRule).
# The SABC same-product-code merge depends on this table being fresh,
# so the default schedule runs before the 09:00 SABC tag reconciliation.
BASE="${FURNITURE_PROD_BASE:-/opt/furniture-3d-system-prod}"
SERVICE_NAME="${PRODUCT_REPLACEMENT_RULE_SYNC_SERVICE_NAME:-furniture-product-replacement-rule-sync}"
APP_ROOT="$BASE/current/furniture-3d-system"
SHARED="$BASE/shared"
APP_ENV="$SHARED/.env"
SERVICE_FILE="/etc/systemd/system/$SERVICE_NAME.service"
TIMER_FILE="/etc/systemd/system/$SERVICE_NAME.timer"
SCHEDULE="${PRODUCT_REPLACEMENT_RULE_SYNC_SCHEDULE:-*-*-* 07:40:00 Asia/Shanghai}"
RANDOMIZED_DELAY_SEC="${PRODUCT_REPLACEMENT_RULE_SYNC_RANDOMIZED_DELAY_SEC:-5min}"
if [ "$(id -u)" -ne 0 ]; then
echo "This installer must run as root." >&2
exit 1
fi
if [ ! -d "$APP_ROOT" ]; then
echo "Missing app root: $APP_ROOT" >&2
exit 1
fi
if [ ! -f "$APP_ENV" ]; then
echo "Missing app env: $APP_ENV" >&2
exit 1
fi
if command -v systemd-analyze >/dev/null 2>&1; then
systemd-analyze calendar "$SCHEDULE" >/dev/null
fi
cat >"$SERVICE_FILE" <<EOF
[Unit]
Description=Sync JST replacement rules into SupplyChainProductArchiveReplacementRule
After=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
WorkingDirectory=$APP_ROOT
Environment=NODE_ENV=production
EnvironmentFile=$APP_ENV
ExecStart=/bin/bash -lc 'set -euo pipefail; cd "$APP_ROOT"; ./node_modules/.bin/tsx scripts/sync-product-replacement-rules.ts --apply --confirm=sync-product-replacement-rules'
TimeoutStartSec=1h
StandardOutput=journal
StandardError=journal
EOF
cat >"$TIMER_FILE" <<EOF
[Unit]
Description=Sync JST replacement rules once a day
[Timer]
OnCalendar=$SCHEDULE
Persistent=true
RandomizedDelaySec=$RANDOMIZED_DELAY_SEC
Unit=$SERVICE_NAME.service
[Install]
WantedBy=timers.target
EOF
systemctl daemon-reload
systemctl enable --now "$SERVICE_NAME.timer"
systemctl list-timers --all "$SERVICE_NAME.timer" --no-pager