98 lines
5.0 KiB
Bash
98 lines
5.0 KiB
Bash
#!/bin/sh
|
||
set -eu
|
||
|
||
REMOTE_DIR="${CAIWU_REMOTE_DIR:-/opt/caiwuxitong}"
|
||
GENERAL_LOCK_FILE="${CAIWU_GENERAL_LOCK:-/run/caiwu-upstream-sync.lock}"
|
||
OA_LOCK_FILE="${CAIWU_OA_LOCK:-/run/caiwu-oa-process.lock}"
|
||
is_import=0
|
||
CAIWU_BATCH_CALLER_DIR="$PWD"
|
||
export CAIWU_BATCH_CALLER_DIR
|
||
case "${1:-}" in
|
||
*:import|*:import-*|*:import:*|payables:unpriced-cycle|product:monthly-weighted-average:base|planting-details:sync-work-orders|jst-order-labels:sync-daily) is_import=1 ;;
|
||
jushuitan:sync-shipping|store-receipt:order-match:sync|store-receipt:planting-daily|store-receipt:product-cost:sync|store-receipt:auto-resync|order-data:warm-shop-candidates|business-mirror:sync-tables|business-mirror:sync-ding-process-details|business-mirror:process-ding-refresh-requests|business-mirror:index-work-order-waybills|business-mirror:refresh-paid-sales|ding-invoices:sync|ding-invoices:process-refresh-requests|account-bills:exports:run|finance-ledger:recalculate|product:monthly-weighted-average:auto|product:monthly-weighted-average:compute|product:monthly-weighted-average:report|finance:monthly-weighted-product-cost:auto) ;;
|
||
*) echo 'Unsupported finance batch command' >&2; exit 64 ;;
|
||
esac
|
||
case "${1:-}" in
|
||
business-mirror:sync-ding-process-details|business-mirror:process-ding-refresh-requests)
|
||
LOCK_FILE="${CAIWU_UPSTREAM_LOCK:-$OA_LOCK_FILE}"
|
||
;;
|
||
*)
|
||
LOCK_FILE="${CAIWU_UPSTREAM_LOCK:-$GENERAL_LOCK_FILE}"
|
||
;;
|
||
esac
|
||
cd "$REMOTE_DIR"
|
||
if [ "$1" = 'account-bills:exports:run' ]; then
|
||
storage_root="$(docker inspect caiwu_app --format '{{range .Mounts}}{{if eq .Destination "/var/www/html/storage"}}{{.Source}}{{end}}{{end}}')"
|
||
[ -n "$storage_root" ] && [ -d "$storage_root" ] || exit 65
|
||
# Check only tiny task metadata; do not start an idle container every minute.
|
||
if ! python3 - "$storage_root" <<'PY'
|
||
import json,pathlib,sys
|
||
for path in pathlib.Path(sys.argv[1]).glob('app/account-bills-exports/*/*.json'):
|
||
try:
|
||
task = json.loads(path.read_text())
|
||
if isinstance(task, dict) and task.get('status') == 'queued':
|
||
sys.exit(0)
|
||
except (OSError,ValueError):
|
||
continue
|
||
sys.exit(1)
|
||
PY
|
||
then
|
||
exit 0
|
||
fi
|
||
fi
|
||
if [ "$1" = 'business-mirror:process-ding-refresh-requests' ]; then
|
||
storage_root="$(docker inspect caiwu_app --format '{{range .Mounts}}{{if eq .Destination "/var/www/html/storage"}}{{.Source}}{{end}}{{end}}')"
|
||
[ -n "$storage_root" ] && [ -d "$storage_root/app/ding-process-refresh" ] || exit 0
|
||
pending_request="$(find "$storage_root/app/ding-process-refresh" -maxdepth 1 -type f -name '*.pending' -print -quit)"
|
||
[ -n "$pending_request" ] || exit 0
|
||
fi
|
||
if [ "$1" = 'ding-invoices:process-refresh-requests' ]; then
|
||
storage_root="$(docker inspect caiwu_app --format '{{range .Mounts}}{{if eq .Destination "/var/www/html/storage"}}{{.Source}}{{end}}{{end}}')"
|
||
[ -n "$storage_root" ] && [ -d "$storage_root/app/ding-invoice-refresh" ] || exit 0
|
||
pending_request="$(find "$storage_root/app/ding-invoice-refresh" -maxdepth 1 -type f -name '*.pending' -print -quit)"
|
||
[ -n "$pending_request" ] || exit 0
|
||
fi
|
||
if [ "$1" != 'account-bills:exports:run' ]; then
|
||
exec 9>"$LOCK_FILE"
|
||
if ! flock -w "${CAIWU_BATCH_LOCK_WAIT:-1800}" 9; then
|
||
echo 'Upstream synchronization is busy; no batch container started' >&2
|
||
exit 75
|
||
fi
|
||
fi
|
||
if [ "$is_import" -eq 1 ]; then
|
||
exec python3 "$REMOTE_DIR/run-finance-import.py" "$@"
|
||
fi
|
||
image="$(docker inspect caiwu_app --format '{{.Config.Image}}')"
|
||
case "$image" in
|
||
caiwu_akaunting:*) IMAGE_TAG="${image#*:}" ;;
|
||
*) echo 'Unsupported active finance image; no batch container started' >&2; exit 65 ;;
|
||
esac
|
||
RUN_MIGRATIONS=0
|
||
export IMAGE_TAG RUN_MIGRATIONS
|
||
# Mutating jobs hold the upstream lock; read-only exports use their own single-worker lock.
|
||
if [ "$1" = 'jushuitan:sync-shipping' ]; then
|
||
# 聚水潭副本库偶发超时会让整次发货数据同步失败(当晚最近几天漏单):
|
||
# 输出里有"同步失败"时自动重试,每次输出留日志;只有警告(未写"同步失败")不重试。
|
||
sync_log="${CAIWU_JST_SYNC_LOG:-$REMOTE_DIR/jst-sync-shipping.log}"
|
||
sync_attempts="${CAIWU_JST_SYNC_ATTEMPTS:-3}"
|
||
if [ -f "$sync_log" ] && [ "$(wc -c <"$sync_log")" -gt 5000000 ]; then
|
||
mv -f "$sync_log" "$sync_log.1"
|
||
fi
|
||
sync_out="$(mktemp)"
|
||
trap 'rm -f "$sync_out"' EXIT
|
||
attempt=1
|
||
while :; do
|
||
echo "== $(date '+%Y-%m-%d %H:%M:%S') attempt $attempt/$sync_attempts: $*" >>"$sync_log"
|
||
rc=0
|
||
docker compose -f compose.jiangling.yml --profile batch run --rm -T --no-deps batch "$@" >"$sync_out" 2>&1 || rc=$?
|
||
tr '\r' '\n' <"$sync_out" | grep -v '已扫描' | tail -n 60 >>"$sync_log" || true
|
||
echo "== rc=$rc" >>"$sync_log"
|
||
if [ "$rc" -eq 0 ] || ! grep -q '同步失败' "$sync_out" || [ "$attempt" -ge "$sync_attempts" ]; then
|
||
exit "$rc"
|
||
fi
|
||
attempt=$((attempt + 1))
|
||
sleep "${CAIWU_JST_SYNC_RETRY_WAIT:-300}"
|
||
done
|
||
fi
|
||
docker compose -f compose.jiangling.yml --profile batch run --rm -T --no-deps batch "$@"
|