CaiWuXiTong/deploy/run-finance-batch.sh

46 lines
1.9 KiB
Bash

#!/bin/sh
set -eu
REMOTE_DIR="${CAIWU_REMOTE_DIR:-/opt/caiwuxitong}"
LOCK_FILE="${CAIWU_UPSTREAM_LOCK:-/run/caiwu-upstream-sync.lock}"
case "${1:-}" in
jushuitan:sync-shipping|store-receipt:order-match:sync|store-receipt:product-cost:sync|store-receipt:auto-resync|order-data:warm-shop-candidates|business-mirror:index-work-order-waybills|business-mirror:refresh-paid-sales|account-bills:exports:run) ;;
*) echo 'Unsupported finance batch command' >&2; exit 64 ;;
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" != '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
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 "$@"