#!/usr/bin/env bash # Shared bounded-batch concurrent job runner. Sourced by # scripts/sync-plugin-content.sh, tests/run-tests.sh, and tests/run-bats.sh so # their concurrency-cap and per-item log/status handling can't silently # diverge -- previously the same batching logic (core-count cap, per-item # log/status files, batched `wait`) was hand-implemented independently in all # three places. # # Batches (not a rolling pool) because a bounded rolling pool needs `wait -n`, # which is bash 4.3+ -- all three callers are explicitly bash-3.2-safe. # `getconf` over `nproc` for the same reason: `nproc` doesn't exist on macOS. # # Not meant to be executed directly -- source it. # batch_jobs_limit # Prints the concurrency cap to use for batching. batch_jobs_limit() { getconf _NPROCESSORS_ONLN 2>/dev/null || echo 4 } # batch_run [ ...] # # For each key/cmd pair, backgrounds `eval "$cmd"` with its combined # stdout+stderr redirected to "/.log", bounded to at most # batch_jobs_limit concurrent jobs (waiting out the current batch before # starting the next). # # Each owns writing its own result to "/.status" -- # this helper only owns dispatch/throttling and log capture, not status # semantics. Callers differ on how they do that (capturing $? of an external # command with `|| rc=$?`, or a sync function writing its own status flag # directly) -- both patterns are preserved as-is by callers, not standardized # here, so existing error-handling behavior (including how each pattern # interacts with `set -e` in the caller) is unchanged by this extraction. batch_run() { local scratch_dir="$1" shift local jobs_limit running key cmd jobs_limit="$(batch_jobs_limit)" running=0 while [[ $# -gt 0 ]]; do key="$1" cmd="$2" shift 2 (eval "$cmd") >"$scratch_dir/$key.log" 2>&1 & running=$((running + 1)) if [[ $running -ge $jobs_limit ]]; then wait running=0 fi done wait }