#!/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:*|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|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" != '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. docker compose -f compose.jiangling.yml --profile batch run --rm -T --no-deps batch "$@"