furniture-3d-system-Wu/furniture-3d-system/scripts/install-modular-processing-daily-snapshot-timer.sh

75 lines
2.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
BASE="${FURNITURE_PROD_BASE:-/opt/furniture-3d-system-prod}"
APP_ROOT="$BASE/current/furniture-3d-system"
SERVICE_NAME="${MODULAR_PROCESSING_SNAPSHOT_SERVICE_NAME:-furniture-modular-processing-daily-snapshot}"
CALENDAR="${MODULAR_PROCESSING_SNAPSHOT_CALENDAR:-*-*-* 09:30:00 Asia/Shanghai}"
RUN_NOW="${MODULAR_PROCESSING_SNAPSHOT_RUN_NOW:-1}"
if [ "$(id -u)" -ne 0 ]; then
echo "This installer requires root." >&2
exit 1
fi
if [ ! -f "$APP_ROOT/scripts/generate-modular-processing-daily-snapshot.ts" ]; then
echo "Missing deployed snapshot script: $APP_ROOT/scripts/generate-modular-processing-daily-snapshot.ts" >&2
exit 1
fi
if [ ! -f "$BASE/shared/.env" ]; then
echo "Missing production env file: $BASE/shared/.env" >&2
exit 1
fi
if ! command -v systemctl >/dev/null 2>&1; then
echo "This installer requires systemd (production Linux)." >&2
exit 1
fi
cat >"/etc/systemd/system/$SERVICE_NAME.service" <<EOF
[Unit]
Description=Generate modular product processing orders at Beijing 09:30
After=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
WorkingDirectory=$APP_ROOT
Environment=NODE_ENV=production
Environment=TZ=Asia/Shanghai
EnvironmentFile=$BASE/shared/.env
UMask=0077
Nice=10
ExecStart=/bin/bash -lc 'exec ./node_modules/.bin/tsx scripts/generate-modular-processing-daily-snapshot.ts'
TimeoutStartSec=2h
Restart=on-failure
RestartSec=15min
EOF
cat >"/etc/systemd/system/$SERVICE_NAME.timer" <<EOF
[Unit]
Description=Generate modular product processing orders daily at Beijing 09:30
[Timer]
OnCalendar=$CALENDAR
Persistent=true
AccuracySec=1min
Unit=$SERVICE_NAME.service
[Install]
WantedBy=timers.target
EOF
systemctl daemon-reload
systemctl enable --now "$SERVICE_NAME.timer"
systemctl reset-failed "$SERVICE_NAME.service" 2>/dev/null || true
if [ "$RUN_NOW" = "1" ]; then
echo "Running the current-day snapshot once..."
systemctl start "$SERVICE_NAME.service"
fi
echo "Timer status:"
systemctl list-timers --all "$SERVICE_NAME.timer" --no-pager
systemctl show "$SERVICE_NAME.service" \
--property=Result \
--property=ExecMainStatus \
--property=ActiveEnterTimestamp \
--property=InactiveEnterTimestamp \
--no-pager