Skip to content

Commit ae592d2

Browse files
committed
run gather in background
run_bg can only be used to run executables in the backgorund not bash functions. This change adds a wait_for_bg_slot funciton and refactors the sos report gathering to run in parallel up to the concurrancy limit.
1 parent d9d3a45 commit ae592d2

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

collection-scripts/bg.sh

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@
22

33
CONCURRENCY=${CONCURRENCY:-5}
44

5+
# Function to wait for a background slot to be available
6+
# This function is used by run_bg to wait for a slot to be available before
7+
# running a new command in the background.
8+
# This function is can also be used to limit background functions
9+
# to a certain number of concurrent processes.
10+
# Example:
11+
# CONCURRENCY=5
12+
# for i in {1..10}; do
13+
# wait_for_bg_slot
14+
# my_funtion &
15+
# done
16+
# wait_bg
17+
18+
function wait_for_bg_slot{
19+
while [[ $(jobs -r | wc -l) -ge $CONCURRENCY ]]; do
20+
wait -n
21+
done
22+
}
23+
524
# Function to run commands in background without exceeding $CONCURRENCY
625
# processes in parallel.
726
# The recommendation is to use this function at the deepest level that can be
@@ -16,11 +35,8 @@ CONCURRENCY=${CONCURRENCY:-5}
1635
#
1736
# For now these methods ignore errors on the calls that are made in the
1837
# background.
19-
2038
function run_bg {
21-
while [[ $(jobs -r | wc -l) -ge $CONCURRENCY ]]; do
22-
wait -n
23-
done
39+
wait_for_bg_slot
2440

2541
# Cannot use the alternative suggested by SC2294 which is just "$@"&
2642
# because that doesn't accomplish what we want, as it executes the first

collection-scripts/gather_edpm_sos

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ data=$(oc get openstackdataplanenodesets --all-namespaces -o json | jq -j '
127127
while read -r node address username secret namespace; do
128128
[[ -z "$node" ]] && continue
129129
if [[ "${SOS_EDPM[0]}" == "all" || "${SOS_EDPM[*]}" == *"${node}"* ]]; then
130-
run_bg gather_edpm_sos $node $address $username $secret $namespace
130+
# run_bg cannot be used here as that only support invoking external scripts
131+
# or executables and not functions
132+
wait_for_bg_slot
133+
gather_edpm_sos $node $address $username $secret $namespace &
131134
fi
132135
done <<< "$data"
133136

collection-scripts/gather_sos

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,10 @@ echo "Will retrieve SOS reports from nodes ${nodes//$'\n'/ }"
174174
for node in $nodes; do
175175
[[ -z "$node" ]] && continue
176176
# Gather SOS report for the node in background
177-
run_bg gather_node_sos "$node"
177+
# run_bg cannot be used here as that only support invoking external scripts
178+
# or executables and not functions
179+
wait_for_bg_slot
180+
gather_node_sos "$node" &
178181
done
179182

180183
[[ $CALLED -eq 1 ]] && wait_bg

0 commit comments

Comments
 (0)