furniture-3d-system-Wu/furniture-3d-system/scripts/install-oa-board-sync-timer.sh

68 lines
2.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
BASE="${FURNITURE_PROD_BASE:-/opt/furniture-3d-system-prod}"
SERVICE_NAME="${OA_BOARD_SYNC_SERVICE_NAME:-furniture-oa-board-sync}"
APP_ROOT="$BASE/current/furniture-3d-system"
APP_ENV="$BASE/shared/.env"
HR_SYNC_ENV="$BASE/shared/employee-directory-sync.env"
SERVICE_FILE="/etc/systemd/system/$SERVICE_NAME.service"
TIMER_FILE="/etc/systemd/system/$SERVICE_NAME.timer"
SYNC_ON_BOOT_SEC="${OA_BOARD_SYNC_ON_BOOT_SEC:-2min}"
SYNC_INTERVAL="${OA_BOARD_SYNC_INTERVAL:-15min}"
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
cat >"$SERVICE_FILE" <<EOF
[Unit]
Description=Furniture OA new product board snapshot and overtime todo sync
After=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
WorkingDirectory=$APP_ROOT
Environment=NODE_ENV=production
EnvironmentFile=$APP_ENV
# Reuse the HR read-only credentials from the existing employee-directory sync.
# The leading '-' keeps the OA sync usable before that optional sync is installed.
EnvironmentFile=-$HR_SYNC_ENV
# Keep the todo scan after the snapshot refresh so each decision uses the newest OA state.
# The global switch remains off by default and skips the todo scan until it is explicitly enabled.
ExecStart=/bin/bash -lc 'set -euo pipefail; cd "$APP_ROOT"; ./node_modules/.bin/tsx scripts/sync-oa-board.ts --apply; case "\${DINGTALK_TODO_ENABLED:-false}" in 1|true|TRUE|yes|YES|on|ON) ./node_modules/.bin/tsx scripts/sync-oa-board-todos.ts ;; *) echo "DingTalk OA overtime todo dispatch is disabled; skipping." ;; esac'
StandardOutput=journal
StandardError=journal
EOF
cat >"$TIMER_FILE" <<EOF
[Unit]
Description=Refresh furniture OA board and dispatch enabled overtime todos every 15 minutes
[Timer]
OnBootSec=$SYNC_ON_BOOT_SEC
OnUnitActiveSec=$SYNC_INTERVAL
Persistent=true
RandomizedDelaySec=30
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