forked from themattrix/bash-concurrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcurrent.lib.sh
822 lines (690 loc) · 25.7 KB
/
concurrent.lib.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
#!/usr/bin/env bash
concurrent() (
#
# General Utilities
#
__crt__error() {
echo "ERROR (concurrent): ${1}" 1>&2
exit 1
}
__crt__is_dry_run() {
[[ -n "${CONCURRENT_DRY_RUN}" ]]
}
__crt__hide_failure() {
"${@}" &> /dev/null || :
}
#
# Compatibility Check
#
if [[ -z "${BASH_VERSINFO[@]}" || "${BASH_VERSINFO[0]}" -lt 4 || "${BASH_VERSINFO[1]}" -lt 2 ]]; then
__crt__error "Requires Bash version 4.2 for 'declare -g' (you have ${BASH_VERSION:-a different shell})"
fi
if sed --version &> /dev/null; then # BSD sed has no --version
__crt__sed='sed'
elif gsed --version &> /dev/null; then
__crt__sed='gsed'
else
__crt__error "Requires GNU sed"
fi
#
# Help and Usage
#
__crt__help__version='concurrent 2.0.1'
__crt__help__usage="concurrent - Run tasks in parallel and display pretty output as they complete.
Usage:
concurrent \\
(- TASK COMMAND [ARGS...])... \\
[--sequential | \\
[((--require TASK)...|--require-all) \\
((--before TASK)...]...|--before-all)]
concurrent -h | --help
concurrent --version
Options:
-h --help Show this help.
--version Show version.
- TASK COMMAND [ARGS...] Define a task named TASK for running COMMAND with ARGS.
--sequential Each task requires the previous task.
--require TASK Require a TASK to complete successfully...
--before TASK ...before another TASK.
--require-all Shortcut for requiring all tasks.
--before-all Given tasks are prerequisites to all others.
Failed Tasks:
If a task fails, all dependent tasks (and their dependent tasks, and so on) are
immediately marked 'SKIP'. The status and output of all failed and skipped tasks
are displayed at the end. The exit status will be 1.
Examples:
# Run three tasks concurrently.
concurrent \\
- 'My long task' sleep 10 \\
- 'My medium task' sleep 5 \\
- 'My short task' sleep 1
# Run three tasks sequentially.
concurrent \\
- 'My long task' sleep 10 \\
- 'My medium task' sleep 5 \\
- 'My short task' sleep 1 \\
--sequential
# Start the medium task *after* the short task succeeds.
concurrent \\
- 'My long task' sleep 10 \\
- 'My medium task' sleep 5 \\
- 'My short task' sleep 1 \\
--require 'My short task' \\
--before 'My medium task'
# Start the short task after *both* other tasks succeed.
...
--require 'My long task' \\
--require 'My medium task' \\
--before 'My short task'
# Start the medium task *and* the long task after the short task succeeds.
...
--require 'My short task' \\
--before 'My medium task' \\
--before 'My long task'
# If your command has a '-' argument, you can use a different task delimiter.
concurrent \\
+ 'My long task' wget -O - ... \\
+ 'My medium task' sleep 5 \\
+ 'My short task' sleep 1
Requirements:
bash >= 4.2, cat, cp, date, mkdir, mkfifo, mktemp, mv, sed (gsed for OS X), tail, tput
Author:
Matthew Tardiff <[email protected]>
License:
MIT
Version:
${__crt__help__version}
URL:
https://github.com/themattrix/bash-concurrent"
__crt__help__display_usage_and_exit() {
"${__crt__sed}" 's/^ //' <<< "${__crt__help__usage}"
exit 0
}
__crt__help__display_version_and_exit() {
echo "${__crt__help__version}"
exit 0
}
if [[ -z "${1}" ]] || [[ "${1}" == '-h' ]] || [[ "${1}" == '--help' ]]; then
__crt__help__display_usage_and_exit
elif [[ "${1}" == '--version' ]]; then
__crt__help__display_version_and_exit
fi
__crt__unset_env() {
# Unset all concurrent-related environment variables.
unset CONCURRENT_DRY_RUN
}
__crt__unset() {
local sub_namespace=${1}
if [[ -n "${sub_namespace}" ]]; then
local namespace="__crt__${sub_namespace}__"
else
local namespace="__crt__"
fi
# shellcheck disable=SC2046
unset -f $(compgen -A function "${namespace}")
# shellcheck disable=SC2046
unset $(compgen -v "${namespace}")
}
__crt__unset 'help'
#
# Settings
#
__crt__ORIG_PWD=${PWD}
__crt__ORIG_OLDPWD=${OLDPWD}
__crt__ORIG_BASHOPTS=${BASHOPTS}
__crt__ORIG_SHELLOPTS=${SHELLOPTS}
__crt__set_original_pwd() {
cd "${__crt__ORIG_PWD}"
export OLDPWD=${__crt__ORIG_OLDPWD}
}
__crt__set_our_shell_options() {
set -o errexit # Exit on a failed command...
set -o pipefail # ...even if that command is in a pipeline.
shopt -s nullglob # Empty glob evaluates to nothing instead of itself
}
__crt__set_original_shell_options() {
__crt__set_our_shell_options
[[ "${__crt__ORIG_SHELLOPTS}" == *errexit* ]] || set +o errexit
[[ "${__crt__ORIG_SHELLOPTS}" == *pipefail* ]] || set +o pipefail
[[ "${__crt__ORIG_BASHOPTS}" == *nullglob* ]] || shopt -u nullglob
}
__crt__set_our_shell_options
#
# Status Updates
#
__crt__txtred='\e[0;31m' # Red
__crt__txtgrn='\e[0;32m' # Green
__crt__txtylw='\e[0;33m' # Yellow
__crt__txtblu='\e[0;34m' # Blue
__crt__bldwht='\e[1;37m' # White
__crt__txtrst='\e[0m' # Text Reset
__crt__seconds_between_frames=1.0
__crt__running_status_current_frame=0
__crt__running_status_frames=(
" ${__crt__txtblu} =>${__crt__txtrst} "
" ${__crt__txtblu} >${__crt__txtrst} "
)
__crt__indent() {
"${__crt__sed}" 's/^/ /' "${@}"
}
__crt__move_cursor_to_first_task() {
tput cuu "${__crt__task_count}"
tput sc
}
__crt__move_cursor_below_tasks() {
tput cud "${__crt__task_count}"
tput sc
}
__crt__draw_running_status() {
# shellcheck disable=SC2059
printf "${__crt__running_status_frames[${__crt__running_status_current_frame}]}"
}
__crt__draw_initial_tasks() {
local i
for (( i = 0; i < __crt__task_count; i++ )); do
echo " ${__crt__names[${i}]}"
done
}
__crt__draw_status() {
local index=${1}
local code=${2}
tput rc
[[ "${index}" -eq 0 ]] || tput cud "${index}"
if [[ "${code}" == "running" ]]; then __crt__draw_running_status
elif [[ "${code}" == "int" ]]; then printf " ${__crt__txtred}%s${__crt__txtrst} " 'SIGINT'
elif [[ "${code}" == "skip" ]]; then printf " ${__crt__txtylw}%s${__crt__txtrst} " ' SKIP '
elif [[ "${code}" == "0" ]]; then printf " ${__crt__txtgrn}%s${__crt__txtrst} " ' OK '
else printf " ${__crt__txtred}%s${__crt__txtrst} " 'FAILED'
fi
tput rc
}
__crt__draw_meta() {
local index=${1}
tput rc
[[ "${index}" -eq 0 ]] || tput cud "${index}"
tput cuf 8 # move past status
printf "%s ${__crt__bldwht}%s${__crt__txtrst}" "${__crt__names[${index}]}" "${__crt__meta[${index}]}"
tput rc
}
__crt__update_running_status_frames() {
local i
for (( i = 0; i < __crt__task_count; i++ )); do
if __crt__is_task_running "${i}"; then
__crt__draw_status "${i}" running
fi
done
__crt__running_status_current_frame=$((
(__crt__running_status_current_frame + 1) % ${#__crt__running_status_frames[@]}
))
}
__crt__print_failures() {
cd "${__crt__status_dir}"
local i
for (( i = 0; i < __crt__task_count; i++ )); do
if [[ "${__crt__codes[${i}]}" != '0' ]]; then
echo
echo "['${__crt__names[${i}]}' failed with exit status ${__crt__codes[${i}]}]"
__crt__indent "${i}"
fi
done
if [[ "${__crt__final_status}" != "0" ]]; then
printf '\nLogs for all tasks can be found in:\n %s\n' "${__crt__log_dir}/"
fi
}
__crt__disable_echo() {
# Disable local echo so the user can't mess up the pretty display.
stty -echo
}
__crt__enable_echo() {
# Enable local echo so user can type again. (Simply exiting the subshell
# is not sufficient to reset this, which is surprising.)
stty echo
}
__crt__status_cleanup() {
trap INT # no longer need special sigint handling
__crt__move_cursor_below_tasks
__crt__print_failures
}
#
# Task Management
#
__crt__is_task_started() {
[[ "${__crt__started[${1}]}" == "true" ]]
}
__crt__is_task_done() {
[[ -n "${__crt__codes[${1}]}" ]]
}
__crt__are_all_tasks_done() {
local i
for (( i = 0; i < __crt__task_count; i++ )); do
__crt__is_task_done "${i}" || return 1
done
}
__crt__is_task_running() {
__crt__is_task_started "${1}" && ! __crt__is_task_done "${1}"
}
__crt__name_index() {
local name=${1}
local i
for i in "${!__crt__names[@]}"; do
if [[ "${__crt__names[${i}]}" == "${name}" ]]; then
printf '%s' "${i}"
return
fi
done
__crt__error "Failed to find task named '${name}'"
}
__crt__is_task_allowed_to_start() {
# A task is allowed to start if:
# 1. it has not already started, and if
# 2. all prereq tasks have succeeded.
# If any prereqs have failed or have been skipped, then this task will
# be skipped.
local task=${1}
if __crt__is_task_started "${task}"; then
return 1 # cannot start again
fi
local requires
local prereqs="prereqs_${task}[@]"
for requires in "${!prereqs}"; do
if [[ -z "${__crt__codes[${requires}]}" ]]; then
return 1
elif [[ "${__crt__codes[${requires}]}" != "0" ]]; then
__crt__skip_task "${task}" "${__crt__names[${requires}]}"
return 1
fi
done
# All prereqs succeeded! This task can be started.
}
__crt__mark_task_with_code() {
local task=${1}
local code=${2}
echo "task:${task}:${code}" >> "${__crt__event_pipe}"
}
__crt__task_runner() (
# Do not create real variables for these so that they do not override
# names from the parent script.
# $1: task
# $2: command args array
# $3: status dir
set -- "${1}" "command_${1}[@]" "${__crt__status_dir}"
__crt__handle_sigint() {
__crt__mark_task_with_code "${1}" int
__crt__exit_by_signal INT
}
# shellcheck disable=SC2064
trap "__crt__handle_sigint ${1}" INT
set +o errexit # a failure of the command should not exit the task
(
__crt__set_original_pwd
__crt__set_original_shell_options
__crt__unset_env
__crt__unset
"${!2}" &> "${3}/${1}" 3>> "${3}/meta/${1}"
)
code=$?
set -o errexit # but other failures should
trap INT # reset the signal handler
__crt__mark_task_with_code "${1}" "${code}"
)
__crt__start_task() {
__crt__task_runner "${1}" &
__crt__started["${1}"]=true
__crt__draw_status "${1}" running
}
__crt__start_all_tasks() {
__crt__draw_initial_tasks
__crt__move_cursor_to_first_task
__crt__start_allowed_tasks
}
__crt__mark_task_as_interrupted() {
__crt__started["${1}"]=true
echo "[INTERRUPTED]" >> "${__crt__status_dir}/${1}"
}
__crt__mark_all_running_tasks_as_interrupted() {
local i
for (( i = 0; i < __crt__task_count; i++ )); do
if __crt__is_task_running "${i}"; then
__crt__mark_task_as_interrupted "${i}"
fi
done
}
__crt__skip_task() {
__crt__started["${1}"]=true
echo "[SKIPPED] Prereq '${2}' failed or was skipped" > "${__crt__status_dir}/${1}"
__crt__mark_task_with_code "${1}" skip
}
__crt__start_allowed_tasks() {
local __crt__i
for (( __crt__i = 0; __crt__i < __crt__task_count; __crt__i++ )); do
if __crt__is_task_allowed_to_start "${__crt__i}"; then
__crt__start_task "${__crt__i}"
fi
done
}
__crt__save_stdin_stream() {
exec 4<&0 # duplicate stdin stream to fd 4
}
__crt__restore_stdin_stream() {
exec 0<&4 # restore original stdin stream from fd 4
}
__crt__wait_for_all_tasks() {
__crt__start_animation_frame
__crt__save_stdin_stream
__crt__run_event_loop < "${__crt__event_pipe}"
__crt__status_cleanup
__crt__stop_animation
__crt__stop_meta_collector
wait # wait for all (completed) tasks
__crt__restore_stdin_stream
}
__crt__run_event_loop() {
# Main event loop! Each line read from the event pipe is an event to
# handle. We can exit the loop once all tasks have completed.
local __crt__status
while read -r __crt__status; do
if [[ "${__crt__status}" == task:* ]]; then
__crt__handle_done_task "${__crt__status#task:}"
if __crt__are_all_tasks_done; then
break
fi
elif [[ "${__crt__status}" == anim:* ]]; then
__crt__start_animation_frame
elif [[ "${__crt__status}" == meta:* ]]; then
__crt__manage_meta "${__crt__status#meta:}"
fi
done
}
__crt__handle_done_task() {
local index=${1%%:*}
local code=${1#*:}
__crt__codes["${index}"]=${code}
__crt__draw_status "${index}" "${code}"
cp -- "${__crt__status_dir}/${index}" "${__crt__log_dir}/${index}. ${__crt__names[${index}]//\//-} (${code}).log"
if [[ "${code}" != "0" ]]; then
__crt__final_status=1
fi
__crt__start_allowed_tasks
}
__crt__start_animation_frame() {
__crt__update_running_status_frames
{
sleep "${__crt__seconds_between_frames}"
echo "anim:" >> "${__crt__event_pipe}"
} &
__crt__animation_pid=$!
}
__crt__stop_animation() {
__crt__hide_failure kill "${__crt__animation_pid}"
__crt__hide_failure wait "${__crt__animation_pid}"
}
__crt__start_meta_monitor() {
# Initialize meta files. This gives tail something to monitor.
mkdir "${__crt__status_dir}/meta"
local i
for (( i = 0; i < __crt__task_count; i++ )); do
> "${__crt__status_dir}/meta/${i}"
done
local pipe="${__crt__status_dir}/pipe0"
mkfifo "${pipe}"
cd "${__crt__status_dir}/meta"
# shellcheck disable=SC2035
tail -f * >> "${pipe}" &
__crt__meta_collector_pids=($!)
"${__crt__sed}" -n -u -e '
/^$/{b}
/^==> .* <==$/{s/^==> //;s/ <==$//;h;b}
G;s/\(.*\)\n\(.*\)/meta:\2:\1/p' "${pipe}" >> "${__crt__event_pipe}" &
__crt__meta_collector_pids+=($!)
}
__crt__stop_meta_collector() {
__crt__hide_failure kill "${__crt__meta_collector_pids[@]}"
__crt__hide_failure wait "${__crt__meta_collector_pids[@]}"
}
__crt__manage_meta() {
local index=${1%%:*}
local meta=${1#*:}
__crt__meta["${index}"]+=${meta}
__crt__draw_meta "${index}"
}
#
# Argument Parsing
#
__crt__names=() # task names by index
__crt__meta=() # metadata strings by index
__crt__codes=() # task exit codes (unset, 0-255, 'skip', or 'int') by index
__crt__started=() # task started status (unset or 'true') by index
__crt__task_count=0 # total number of tasks
__crt__final_status=0 # 0 if all tasks succeeded, 1 otherwise
# Arrays of command arguments by task index <T>:
# command_<T>=(...)
#
# Arrays of prerequisite task indices by task index <T>:
# prereqs_<T>=(...)
#
# These are dynamically created during argument parsing since bash doesn't
# have a concept of nested lists.
__crt__args__task_delimiter=${1}
__crt__args__is_task_flag() { [[ "${1}" == "${__crt__args__task_delimiter}" ]]; }
__crt__args__is_require_flag() { [[ "${1}" == "--require" ]]; }
__crt__args__is_require_all_flag() { [[ "${1}" == "--require-all" ]]; }
__crt__args__is_before_flag() { [[ "${1}" == "--before" ]]; }
__crt__args__is_before_all_flag() { [[ "${1}" == "--before-all" ]]; }
__crt__args__is_sequential_flag() { [[ "${1}" == "--sequential" ]]; }
__crt__args__is_flag_starting_section() {
__crt__args__is_task_flag "${1}" ||
__crt__args__is_require_flag "${1}" ||
__crt__args__is_require_all_flag "${1}" ||
__crt__args__is_sequential_flag "${1}"
}
__crt__args__is_item_in_array() {
local item_to_find=${1}
local array_name="${2}[@]"
local i
for i in "${!array_name}"; do
if [[ "${i}" == "${item_to_find}" ]]; then return 0; fi
done
return 1
}
__crt__args__get_tasks_not_in() {
local these_tasks=${1}
local other_tasks=()
local i
for (( i = 0; i < __crt__task_count; i++ )); do
__crt__args__is_item_in_array "${i}" "${these_tasks}" || other_tasks=(${other_tasks[@]} ${i})
done
__crt__args__fn_result=("${other_tasks[@]}")
}
__crt__args__assign_sequential_prereqs() {
local i
for (( i = 1; i < __crt__task_count; i++ )); do
declare -g -a "prereqs_${i}=($(( i - 1 )))"
done
}
__crt__args__handle_task_flag() {
set -- "${remaining_args[@]}"
shift; (( $# )) || __crt__error "expected task name after '-'"
__crt__names+=("${1}")
shift; (( $# )) || __crt__error "expected command after task name"
local args=()
while (( $# )) && ! __crt__args__is_flag_starting_section "${1}"; do
args+=("${1}")
shift
done
if __crt__is_dry_run; then
args=(sleep 3) # DRY RUN: Sleep for 3 seconds instead of running a real command.
fi
declare -g -a "command_${__crt__task_count}=(\"\${args[@]}\")"
(( __crt__task_count++ )) || :
remaining_args=("${@}")
}
__crt__args__handle_sequential_flag() {
set -- "${remaining_args[@]}"
shift
__crt__args__assign_sequential_prereqs
remaining_args=("${@}")
}
__crt__args__handle_require_flag() {
set -- "${remaining_args[@]}"
local require
local before
while (( $# )) && __crt__args__is_require_flag "${1}"; do
shift; (( $# )) || __crt__error "expected task name after '--require'"
require=(${require[@]} $(__crt__name_index "${1}"))
shift
done
if __crt__args__is_before_all_flag "${1}"; then
shift
__crt__args__get_tasks_not_in 'require'; before=("${__crt__args__fn_result[@]}")
local b
for b in "${before[@]}"; do
declare -g -a "prereqs_${b}=(\${require[@]})"
done
elif __crt__args__is_before_flag "${1}"; then
while (( $# )) && __crt__args__is_before_flag "${1}"; do
shift; (( $# )) || __crt__error "expected task name after '--before'"
before=$(__crt__name_index "${1}")
shift
if __crt__args__is_item_in_array "${before}" "require"; then
__crt__error "task cannot require itself"
fi
declare -g -a "prereqs_${before}=(\${prereqs_${before}[@]} \${require[@]})"
done
else
__crt__error "expected '--before' or '--before-all' after '--require-all'"
fi
remaining_args=("${@}")
}
__crt__args__handle_require_all_flag() {
set -- "${remaining_args[@]}"
local require
local before
shift
if __crt__args__is_before_all_flag "${1}"; then
shift
__crt__args__assign_sequential_prereqs # --require-all --before-all is the same as --sequential
elif __crt__args__is_before_flag "${1}"; then
before=()
while (( $# )) && __crt__args__is_before_flag "${1}"; do
shift; (( $# )) || __crt__error "expected task name after '--before'"
before=(${before[@]} $(__crt__name_index "${1}"))
shift
done
__crt__args__get_tasks_not_in 'before'; require=("${__crt__args__fn_result[@]}")
local b
for b in "${before[@]}"; do
declare -g -a "prereqs_${b}=(\${require[@]})"
done
else
__crt__error "expected '--before' or '--before-all' after '--require-all'"
fi
remaining_args=("${@}")
}
__crt__args__ensure_no_requirement_loops() (
# We will do a lightweight dry-run through all of the tasks and make sure we
# do not get stuck anywhere.
started=()
tasks_started=0
is_task_allowed_to_start() {
local task=${1}
[[ -z "${started[${task}]}" ]] || return 1
local requires
local prereqs="prereqs_${task}[@]"
for requires in "${!prereqs}"; do
[[ -n "${started[${requires}]}" ]] || return 1
done
}
start_allowed_tasks() {
tasks_started=0
local i
for (( i = 0; i < __crt__task_count; i++ )); do
if is_task_allowed_to_start "${i}"; then
start_task "${i}"
(( tasks_started++ )) || :
fi
done
}
start_task() {
started["${1}"]=true
}
while true; do
start_allowed_tasks
[[ "${#started[@]}" != "${__crt__task_count}" ]] || break
[[ "${tasks_started}" -gt 0 ]] || __crt__error "detected requirement loop"
done
)
__crt__args__parse() {
local remaining_args=("${@}")
while (( ${#remaining_args} )); do
if __crt__args__is_task_flag "${remaining_args[0]}"; then
__crt__args__handle_task_flag
elif __crt__args__is_require_flag "${remaining_args[0]}"; then
__crt__args__handle_require_flag
elif __crt__args__is_require_all_flag "${remaining_args[0]}"; then
__crt__args__handle_require_all_flag
elif __crt__args__is_sequential_flag "${remaining_args[0]}"; then
__crt__args__handle_sequential_flag
else
__crt__error "unexpected argument '${remaining_args[0]}'"
fi
done
__crt__args__ensure_no_requirement_loops
__crt__unset 'args'
}
__crt__args__parse "${@}"
#
# Logging
#
__crt__log_dir="${PWD}/.logs/$(date +'%F@%T')"
mkdir -p "${__crt__log_dir}"
#
# Signal Handling/General Cleanup
#
__crt__exit_by_signal() {
# Proper sigint handling: http://www.cons.org/cracauer/sigint.html
local signal=${1}
# shellcheck disable=SC2064
trap "${signal}" # reset the signal
kill "-${signal}" -- $$ # re-raise the signal
exit 255 # don't resume the script
}
__crt__handle_exit() {
rm -rf "${__crt__status_dir}"
__crt__hide_failure __crt__restore_stdin_stream
__crt__hide_failure __crt__enable_echo
}
__crt__handle_sigint() {
# A few things to note at this point:
# - Since the main event loop was (likely) interrupted, fd 0 is still
# pointing at the named pipe instead of stdin. This is great news,
# because we need to finish reading from it to process the remaining
# tasks anyway.
# - Bash should have sent SIGINT to the entire process group, which
# includes all of the background processes. Each task handles the
# signal and writes a message to the event named pipe.
# Make sure to perform the final cleanup steps even if this handler
# unexpectedly explodes.
trap __crt__handle_exit EXIT
__crt__mark_all_running_tasks_as_interrupted
__crt__run_event_loop
__crt__status_cleanup
__crt__stop_animation
__crt__stop_meta_collector
__crt__hide_failure wait
__crt__exit_by_signal INT
}
__crt__disable_echo || __crt__error 'Must be run in the foreground of an interactive shell!'
__crt__status_dir=$(mktemp -d "${TMPDIR:-/tmp}/concurrent.lib.sh.XXXXXXXXXXX")
__crt__event_pipe="${__crt__status_dir}/event-pipe"
mkfifo "${__crt__event_pipe}"
trap __crt__handle_exit EXIT
trap __crt__handle_sigint INT
if __crt__is_dry_run; then
# shellcheck disable=SC2016
echo '>>> DRY RUN (concurrent): The "$CONCURRENT_DRY_RUN" environment variable is set. <<<'
fi
__crt__start_meta_monitor
__crt__start_all_tasks
__crt__wait_for_all_tasks
exit ${__crt__final_status}
)