Skip to content

Commit

Permalink
Improve run_parallel.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
unkhz committed Aug 5, 2024
1 parent 88d26a6 commit 560f224
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 49 deletions.
8 changes: 1 addition & 7 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,7 @@ tasks:
vars:
TASKS: '{{.TASKS | default ""}}'
cmds:
- |
tasks=({{.TASKS}})
args=()
for task in "${tasks[@]}"; do
args+=("$task" "task $task")
done
./run_parallel.sh "${args[@]}"
- ./run_parallel_task.sh {{.TASKS}}

start-gateway:
desc: Start gateway services in parallel
Expand Down
62 changes: 20 additions & 42 deletions run_parallel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,36 @@

# Function to execute a command and print its output with colorized label prefix
run_command() {
local command="$1"
local label="$2"
local color_code=$((31 + $(($RANDOM % 7))))
# Use a subshell to prefix each line of output with the colorized label
(
eval "$command" 2>&1 | while IFS= read -r line; do
# Use printf instead of echo for reliable escape sequence interpretation
printf "\e[${color_code}m[%s]\e[0m %s\n" "$label" "$line"
done
) &
local label="$1"
local command="$2"
local color_code="$3"
eval "$command" 2>&1 | sed -u "s/^/\x1b[${color_code}m[${label}]\x1b[0m /"
}
export -f run_command

# Trap CTRL+C to kill all background processes
trap 'kill $(jobs -p); echo "Interrupted by CTRL+C"' INT
trap 'parallel --halt now,fail=1 kill {}' INT

# Process command-line arguments
if [[ $# -lt 2 ]]; then
echo "Usage: $0 <label1> <command1> [<label2> <command2> ...]"
exit 1
fi

# Use a regular array to store label-color pairs (works on both macOS and Linux)
label_colors=()

# Loop through arguments and run commands in parallel
for i in $(seq 1 2 $#); do
if [[ $((i % 2)) -eq 1 ]]; then
label="$1"
shift
command="$1"
shift

# Assign a color to the label if not already assigned
color_assigned=false
for j in "${!label_colors[@]}"; do
if [[ "${label_colors[$j]}" == "$label" ]]; then
color_assigned=true
break
fi
done

if ! $color_assigned; then
color_code=$((31 + $(($RANDOM % 7))))
label_colors+=("$label")
label_colors+=("$color_code")
fi

run_command "$command" "$label"
fi
# Create arrays for labels, commands, and colors
labels=()
commands=()
colors=()

# Loop through arguments and populate arrays
while [[ $# -gt 0 ]]; do
labels+=("$1")
commands+=("$2")
colors+=($((31 + RANDOM % 7)))
shift 2
done

# Wait for all background processes to finish
wait
# Use parallel to run commands
parallel --halt now,fail=1 --line-buffer --link run_command {1} {2} {3} ::: "${labels[@]}" ::: "${commands[@]}" ::: "${colors[@]}"

echo "All commands finished!"
echo "All commands finished!"
8 changes: 8 additions & 0 deletions run_parallel_task.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

tasks=($@)
args=()
for task in "${tasks[@]}"; do
args+=("$task" "task $task")
done
./run_parallel.sh "${args[@]}"

0 comments on commit 560f224

Please sign in to comment.